diff --git a/backend/pkg/hub/internal/fhir/base/base_client.go b/backend/pkg/hub/internal/fhir/base/base_client.go new file mode 100644 index 00000000..8e7dbcec --- /dev/null +++ b/backend/pkg/hub/internal/fhir/base/base_client.go @@ -0,0 +1,68 @@ +package base + +import ( + "context" + "encoding/json" + "fmt" + "github.com/fastenhealth/fastenhealth-onprem/backend/pkg/config" + "github.com/fastenhealth/fastenhealth-onprem/backend/pkg/models" + "github.com/sirupsen/logrus" + "golang.org/x/oauth2" + "net/http" + "strings" + "time" +) + +type BaseClient struct { + AppConfig config.Interface + Logger logrus.FieldLogger + + OauthClient *http.Client + Credential models.ProviderCredential +} + +func NewBaseClient(appConfig config.Interface, globalLogger logrus.FieldLogger, credentials models.ProviderCredential, testHttpClient ...*http.Client) BaseClient { + var httpClient *http.Client + if len(testHttpClient) == 0 { + httpClient = oauth2.NewClient(context.Background(), oauth2.StaticTokenSource( + &oauth2.Token{ + TokenType: "Bearer", + RefreshToken: credentials.RefreshToken, + AccessToken: credentials.AccessToken, + })) + } else { + //Testing mode. + httpClient = testHttpClient[0] + } + + httpClient.Timeout = 10 * time.Second + return BaseClient{ + AppConfig: appConfig, + Logger: globalLogger, + OauthClient: httpClient, + Credential: credentials, + } +} + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// HttpClient +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +func (c *BaseClient) GetRequest(resourceSubpath string, decodeModelPtr interface{}) error { + url := fmt.Sprintf("%s/%s", strings.TrimRight(c.Credential.ApiEndpointBaseUrl, "/"), strings.TrimLeft(resourceSubpath, "/")) + resp, err := c.OauthClient.Get(url) + + if err != nil { + return err + } + defer resp.Body.Close() + + if resp.StatusCode >= 300 || resp.StatusCode < 200 { + return fmt.Errorf("An error occurred during request - %d - %s", resp.StatusCode, resp.Status) + } + + err = json.NewDecoder(resp.Body).Decode(decodeModelPtr) + if err != nil { + return err + } + return err +} diff --git a/backend/pkg/hub/internal/fhir/base/fhir401_client.go b/backend/pkg/hub/internal/fhir/base/fhir401_client.go index 76ae23f9..413d101f 100644 --- a/backend/pkg/hub/internal/fhir/base/fhir401_client.go +++ b/backend/pkg/hub/internal/fhir/base/fhir401_client.go @@ -1,48 +1,21 @@ package base import ( - "context" - "encoding/json" "fmt" "github.com/fastenhealth/fastenhealth-onprem/backend/pkg/config" "github.com/fastenhealth/fastenhealth-onprem/backend/pkg/models" "github.com/fastenhealth/gofhir-models/fhir401" "github.com/sirupsen/logrus" - "golang.org/x/oauth2" "net/http" - "strings" - "time" ) type FHIR401Client struct { - AppConfig config.Interface - Logger logrus.FieldLogger - - OauthClient *http.Client - Credential models.ProviderCredential + BaseClient } func NewFHIR401Client(appConfig config.Interface, globalLogger logrus.FieldLogger, credentials models.ProviderCredential, testHttpClient ...*http.Client) (FHIR401Client, error) { - - var httpClient *http.Client - if len(testHttpClient) == 0 { - httpClient = oauth2.NewClient(context.Background(), oauth2.StaticTokenSource( - &oauth2.Token{ - TokenType: "Bearer", - RefreshToken: credentials.RefreshToken, - AccessToken: credentials.AccessToken, - })) - } else { - //Testing mode. - httpClient = testHttpClient[0] - } - - httpClient.Timeout = 10 * time.Second return FHIR401Client{ - AppConfig: appConfig, - Logger: globalLogger, - OauthClient: httpClient, - Credential: credentials, + NewBaseClient(appConfig, globalLogger, credentials, testHttpClient...), }, nil } @@ -64,22 +37,3 @@ func (c *FHIR401Client) GetPatient(patientId string) (*fhir401.Patient, error) { err := c.GetRequest(fmt.Sprintf("Patient/%s", patientId), &patient) return &patient, err } - -//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -// HttpClient -//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -func (c *FHIR401Client) GetRequest(resourceSubpath string, decodeModelPtr interface{}) error { - url := fmt.Sprintf("%s/%s", strings.TrimRight(c.Credential.ApiEndpointBaseUrl, "/"), strings.TrimLeft(resourceSubpath, "/")) - resp, err := c.OauthClient.Get(url) - - if err != nil { - return err - } - defer resp.Body.Close() - - err = json.NewDecoder(resp.Body).Decode(decodeModelPtr) - if err != nil { - return err - } - return err -} diff --git a/backend/pkg/hub/internal/fhir/base/fhir430_client.go b/backend/pkg/hub/internal/fhir/base/fhir430_client.go index 6514a3e7..b7650ad1 100644 --- a/backend/pkg/hub/internal/fhir/base/fhir430_client.go +++ b/backend/pkg/hub/internal/fhir/base/fhir430_client.go @@ -1,48 +1,21 @@ package base import ( - "context" - "encoding/json" "fmt" "github.com/fastenhealth/fastenhealth-onprem/backend/pkg/config" "github.com/fastenhealth/fastenhealth-onprem/backend/pkg/models" "github.com/fastenhealth/gofhir-models/fhir430" "github.com/sirupsen/logrus" - "golang.org/x/oauth2" "net/http" - "strings" - "time" ) type FHIR430Client struct { - AppConfig config.Interface - Logger logrus.FieldLogger - - OauthClient *http.Client - Credential models.ProviderCredential + BaseClient } func NewFHIR430Client(appConfig config.Interface, globalLogger logrus.FieldLogger, credentials models.ProviderCredential, testHttpClient ...*http.Client) (FHIR430Client, error) { - - var httpClient *http.Client - if len(testHttpClient) == 0 { - httpClient = oauth2.NewClient(context.Background(), oauth2.StaticTokenSource( - &oauth2.Token{ - TokenType: "Bearer", - RefreshToken: credentials.RefreshToken, - AccessToken: credentials.AccessToken, - })) - } else { - //Testing mode. - httpClient = testHttpClient[0] - } - - httpClient.Timeout = 10 * time.Second return FHIR430Client{ - AppConfig: appConfig, - Logger: globalLogger, - OauthClient: httpClient, - Credential: credentials, + NewBaseClient(appConfig, globalLogger, credentials, testHttpClient...), }, nil } @@ -64,22 +37,3 @@ func (c *FHIR430Client) GetPatient(patientId string) (*fhir430.Patient, error) { return &patient, err } - -//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -// HttpClient -//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -func (c *FHIR430Client) GetRequest(resourceSubpath string, decodeModelPtr interface{}) error { - url := fmt.Sprintf("%s/%s", strings.TrimRight(c.Credential.ApiEndpointBaseUrl, "/"), strings.TrimLeft(resourceSubpath, "/")) - resp, err := c.OauthClient.Get(url) - - if err != nil { - return err - } - defer resp.Body.Close() - - err = json.NewDecoder(resp.Body).Decode(decodeModelPtr) - if err != nil { - return err - } - return err -} diff --git a/backend/pkg/hub/internal/fhir/cigna/client.go b/backend/pkg/hub/internal/fhir/cigna/client.go index 7f60afc8..6ddbeb5f 100644 --- a/backend/pkg/hub/internal/fhir/cigna/client.go +++ b/backend/pkg/hub/internal/fhir/cigna/client.go @@ -18,15 +18,9 @@ func NewClient(appConfig config.Interface, globalLogger logrus.FieldLogger, cred return CignaClient{ baseClient, }, err - } func (c CignaClient) SyncAll() error { - patient, err := c.GetPatient(c.Credential.PatientId) - if err != nil { - return err - } - c.Logger.Infof("patient: %v", patient) bundle, err := c.GetPatientEverything(c.Credential.PatientId) if err != nil { @@ -37,14 +31,6 @@ func (c CignaClient) SyncAll() error { if err != nil { return err } - c.Logger.Infof("bundle lenght: ", string(bundleJson)) + c.Logger.Infof("bundle lenght: %v", string(bundleJson)) return nil } - -//func (c CignaClient) PatientProfile() (models.PatientProfile, error) { -// patient, err := c.GetPatientEverything(fmt.Sprintf("Patient/%s/", c.Credential.PatientId)) -// -// -// -// return nil -//}