diff --git a/backend/pkg/hub/internal/fhir/base/client.go b/backend/pkg/hub/internal/fhir/base/fhir401_client.go similarity index 56% rename from backend/pkg/hub/internal/fhir/base/client.go rename to backend/pkg/hub/internal/fhir/base/fhir401_client.go index fdc72cd9..76ae23f9 100644 --- a/backend/pkg/hub/internal/fhir/base/client.go +++ b/backend/pkg/hub/internal/fhir/base/fhir401_client.go @@ -3,11 +3,10 @@ package base import ( "context" "encoding/json" - "errors" "fmt" "github.com/fastenhealth/fastenhealth-onprem/backend/pkg/config" "github.com/fastenhealth/fastenhealth-onprem/backend/pkg/models" - gofhir "github.com/fastenhealth/gofhir-client/models" + "github.com/fastenhealth/gofhir-models/fhir401" "github.com/sirupsen/logrus" "golang.org/x/oauth2" "net/http" @@ -15,7 +14,7 @@ import ( "time" ) -type FhirBaseClient struct { +type FHIR401Client struct { AppConfig config.Interface Logger logrus.FieldLogger @@ -23,7 +22,7 @@ type FhirBaseClient struct { Credential models.ProviderCredential } -func NewBaseClient(appConfig config.Interface, globalLogger logrus.FieldLogger, credentials models.ProviderCredential, testHttpClient ...*http.Client) (FhirBaseClient, error) { +func NewFHIR401Client(appConfig config.Interface, globalLogger logrus.FieldLogger, credentials models.ProviderCredential, testHttpClient ...*http.Client) (FHIR401Client, error) { var httpClient *http.Client if len(testHttpClient) == 0 { @@ -39,7 +38,7 @@ func NewBaseClient(appConfig config.Interface, globalLogger logrus.FieldLogger, } httpClient.Timeout = 10 * time.Second - return FhirBaseClient{ + return FHIR401Client{ AppConfig: appConfig, Logger: globalLogger, OauthClient: httpClient, @@ -50,53 +49,37 @@ func NewBaseClient(appConfig config.Interface, globalLogger logrus.FieldLogger, //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // FHIR //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -func (c *FhirBaseClient) GetPatientEverything(patientId string) (*gofhir.Bundle, error) { +func (c *FHIR401Client) GetPatientEverything(patientId string) (*fhir401.Bundle, error) { // https://www.hl7.org/fhir/patient-operation-everything.html - data, err := c.GetRequest(fmt.Sprintf("Patient/%s/$everything", patientId)) - if err != nil { - return nil, err - } - if bundle, isOk := data.(*gofhir.Bundle); isOk { - return bundle, nil - } else { - return nil, errors.New("could not cast Patient") - } + bundle := fhir401.Bundle{} + err := c.GetRequest(fmt.Sprintf("Patient/%s/$everything", patientId), &bundle) + return &bundle, err + } -func (c *FhirBaseClient) GetPatient(patientId string) (*gofhir.Patient, error) { +func (c *FHIR401Client) GetPatient(patientId string) (*fhir401.Patient, error) { - data, err := c.GetRequest(fmt.Sprintf("Patient/%s", patientId)) - if err != nil { - return nil, err - } - if patient, isOk := data.(*gofhir.Patient); isOk { - return patient, nil - } else { - return nil, errors.New("could not cast Patient") - } + patient := fhir401.Patient{} + err := c.GetRequest(fmt.Sprintf("Patient/%s", patientId), &patient) + return &patient, err } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // HttpClient //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -func (c *FhirBaseClient) GetRequest(resourceSubpath string) (interface{}, error) { +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 nil, err + return err } defer resp.Body.Close() - //log.Printf("%v resp code", resp.StatusCode) - //body, err := ioutil.ReadAll(resp.Body) - //log.Fatalf("JSON BODY: %v", string(body)) - - var jsonResp map[string]interface{} - err = json.NewDecoder(resp.Body).Decode(&jsonResp) + err = json.NewDecoder(resp.Body).Decode(decodeModelPtr) if err != nil { - return nil, err + return err } - return gofhir.MapToResource(jsonResp, true) + return err } diff --git a/backend/pkg/hub/internal/fhir/base/fhir430_client.go b/backend/pkg/hub/internal/fhir/base/fhir430_client.go new file mode 100644 index 00000000..6514a3e7 --- /dev/null +++ b/backend/pkg/hub/internal/fhir/base/fhir430_client.go @@ -0,0 +1,85 @@ +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 +} + +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, + }, nil +} + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// FHIR +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +func (c *FHIR430Client) GetPatientEverything(patientId string) (*fhir430.Bundle, error) { + + // https://www.hl7.org/fhir/patient-operation-everything.html + bundle := fhir430.Bundle{} + err := c.GetRequest(fmt.Sprintf("Patient/%s/$everything", patientId), &bundle) + return &bundle, err +} + +func (c *FHIR430Client) GetPatient(patientId string) (*fhir430.Patient, error) { + + patient := fhir430.Patient{} + err := c.GetRequest(fmt.Sprintf("Patient/%s", patientId), &patient) + 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 1835a972..7f60afc8 100644 --- a/backend/pkg/hub/internal/fhir/cigna/client.go +++ b/backend/pkg/hub/internal/fhir/cigna/client.go @@ -1,6 +1,7 @@ package cigna import ( + "encoding/json" "github.com/fastenhealth/fastenhealth-onprem/backend/pkg/config" "github.com/fastenhealth/fastenhealth-onprem/backend/pkg/hub/internal/fhir/base" "github.com/fastenhealth/fastenhealth-onprem/backend/pkg/models" @@ -9,11 +10,11 @@ import ( ) type CignaClient struct { - base.FhirBaseClient + base.FHIR401Client } func NewClient(appConfig config.Interface, globalLogger logrus.FieldLogger, credentials models.ProviderCredential, testHttpClient ...*http.Client) (base.Client, error) { - baseClient, err := base.NewBaseClient(appConfig, globalLogger, credentials, testHttpClient...) + baseClient, err := base.NewFHIR401Client(appConfig, globalLogger, credentials, testHttpClient...) return CignaClient{ baseClient, }, err @@ -27,11 +28,16 @@ func (c CignaClient) SyncAll() error { } c.Logger.Infof("patient: %v", patient) - //bundle, err := c.GetPatientEverything(c.Credential.PatientId) - //if err != nil { - // return err - //} - //c.Logger.Infof("bundle lenght: ", bundle.Total) + bundle, err := c.GetPatientEverything(c.Credential.PatientId) + if err != nil { + return err + } + + bundleJson, err := json.Marshal(bundle) + if err != nil { + return err + } + c.Logger.Infof("bundle lenght: ", string(bundleJson)) return nil } diff --git a/go.mod b/go.mod index ba096799..5e1ce32e 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.18 require ( github.com/analogj/go-util v0.0.0-20210417161720-39b497cca03b - github.com/fastenhealth/gofhir-client v0.0.0 + github.com/fastenhealth/gofhir-models v0.0.1 github.com/gin-gonic/gin v1.8.1 github.com/glebarez/sqlite v1.4.6 github.com/sirupsen/logrus v1.9.0 @@ -41,7 +41,6 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml v1.9.5 // indirect github.com/pelletier/go-toml/v2 v2.0.1 // indirect - github.com/pkg/errors v0.9.1 // indirect github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/spf13/afero v1.8.2 // indirect @@ -59,7 +58,6 @@ require ( google.golang.org/appengine v1.6.7 // indirect google.golang.org/protobuf v1.28.0 // indirect gopkg.in/ini.v1 v1.66.4 // indirect - gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect modernc.org/libc v1.16.8 // indirect @@ -67,5 +65,3 @@ require ( modernc.org/memory v1.1.1 // indirect modernc.org/sqlite v1.17.3 // indirect ) - -replace github.com/fastenhealth/gofhir-client v0.0.0 => /Users/jason/repos/gopath/src/github.com/fastenhealth/gofhir-client