fasten-onprem/backend/pkg/hub/internal/fhir/base/base_client.go

146 lines
4.4 KiB
Go
Raw Normal View History

2022-08-28 22:32:13 -06:00
package base
import (
"context"
"encoding/json"
"fmt"
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/config"
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/database"
2022-08-28 22:32:13 -06:00
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/models"
"github.com/sirupsen/logrus"
"golang.org/x/oauth2"
"io"
2022-08-28 22:32:13 -06:00
"net/http"
"net/url"
"os"
2022-08-28 22:32:13 -06:00
"strings"
"time"
)
type BaseClient struct {
Context context.Context
2022-08-28 22:32:13 -06:00
AppConfig config.Interface
Logger logrus.FieldLogger
OauthClient *http.Client
Source models.Source
2022-08-28 22:32:13 -06:00
}
func (c *BaseClient) SyncAllBundle(db database.DatabaseRepository, bundleFile *os.File) error {
panic("SyncAllBundle functionality is not available on this client")
}
func NewBaseClient(ctx context.Context, appConfig config.Interface, globalLogger logrus.FieldLogger, source models.Source, testHttpClient ...*http.Client) (*BaseClient, *models.Source, error) {
2022-08-28 22:32:13 -06:00
var httpClient *http.Client
var updatedSource *models.Source
2022-08-28 22:32:13 -06:00
if len(testHttpClient) == 0 {
//check if we need to refresh the access token
//https://github.com/golang/oauth2/issues/84#issuecomment-520099526
// https://chromium.googlesource.com/external/github.com/golang/oauth2/+/8f816d62a2652f705144857bbbcc26f2c166af9e/oauth2.go#239
conf := &oauth2.Config{
ClientID: source.ClientId,
ClientSecret: "",
Endpoint: oauth2.Endpoint{
AuthURL: source.OauthAuthorizationEndpoint,
TokenURL: source.OauthTokenEndpointAuthMethods,
},
//RedirectURL: "",
//Scopes: nil,
}
token := &oauth2.Token{
TokenType: "Bearer",
RefreshToken: source.RefreshToken,
AccessToken: source.AccessToken,
Expiry: time.Unix(source.ExpiresAt, 0),
}
if token.Expiry.Before(time.Now()) { // expired so let's update it
src := conf.TokenSource(ctx, token)
newToken, err := src.Token() // this actually goes and renews the tokens
if err != nil {
return nil, nil, err
}
if newToken.AccessToken != token.AccessToken {
token = newToken
// update the "source" credential with new data (which will need to be sent
updatedSource = &source
updatedSource.AccessToken = newToken.AccessToken
updatedSource.ExpiresAt = newToken.Expiry.Unix()
// Don't overwrite `RefreshToken` with an empty value
// if this was a token refreshing request.
if newToken.RefreshToken != "" {
updatedSource.RefreshToken = newToken.RefreshToken
}
}
}
// OLD CODE
httpClient = oauth2.NewClient(ctx, oauth2.StaticTokenSource(token))
2022-08-28 22:32:13 -06:00
} else {
//Testing mode.
httpClient = testHttpClient[0]
}
httpClient.Timeout = 10 * time.Second
return &BaseClient{
Context: ctx,
2022-08-28 22:32:13 -06:00
AppConfig: appConfig,
Logger: globalLogger,
OauthClient: httpClient,
Source: source,
}, updatedSource, nil
2022-08-28 22:32:13 -06:00
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// HttpClient
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func (c *BaseClient) GetRequest(resourceSubpathOrNext string, decodeModelPtr interface{}) error {
resourceUrl, err := url.Parse(resourceSubpathOrNext)
if err != nil {
return err
}
if !resourceUrl.IsAbs() {
resourceUrl, err = url.Parse(fmt.Sprintf("%s/%s", strings.TrimRight(c.Source.ApiEndpointBaseUrl, "/"), strings.TrimLeft(resourceSubpathOrNext, "/")))
}
if err != nil {
return err
}
req, err := http.NewRequest(http.MethodGet, resourceUrl.String(), nil)
if err != nil {
return err
}
req.Header.Add("Accept", "application/json+fhir")
//resp, err := c.OauthClient.Get(url)
resp, err := c.OauthClient.Do(req)
2022-08-28 22:32:13 -06:00
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode >= 300 || resp.StatusCode < 200 {
return fmt.Errorf("An error occurred during request %s - %d - %s", resourceUrl, resp.StatusCode, resp.Status)
2022-08-28 22:32:13 -06:00
}
err = ParseBundle(resp.Body, decodeModelPtr)
2022-08-28 22:32:13 -06:00
return err
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Helper Functions
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func ParseBundle(r io.Reader, decodeModelPtr interface{}) error {
decoder := json.NewDecoder(r)
//decoder.DisallowUnknownFields() //make sure we throw an error if unknown fields are present.
err := decoder.Decode(decodeModelPtr)
if err != nil {
return err
}
return err
}