Skip to content

Commit

Permalink
Merge pull request #68 from fastenhealth/wip_source_def_client_lookup_fn
Browse files Browse the repository at this point in the history
adding options parsing to SourceDefinition.
  • Loading branch information
AnalogJ authored Nov 30, 2024
2 parents 3aa68be + a81bec8 commit 7f7fad4
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 31 deletions.
7 changes: 5 additions & 2 deletions catalog/catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func TestCatalog_GetEndpoints_HaveKnownPlatformType_Production(t *testing.T) {
})

for _, endpointId := range endpointPlatformTypes {
_, err := definitions.GetSourceDefinition(definitions.GetSourceConfigOptions{EndpointId: endpointId, Env: pkg.FastenLighthouseEnvProduction})
_, err := definitions.GetSourceDefinition(definitions.WithEndpointId(endpointId), definitions.WithEnv(pkg.FastenLighthouseEnvProduction))
require.NoError(t, err)
}

Expand Down Expand Up @@ -157,7 +157,10 @@ func TestCatalog_GetEndpoints_HaveKnownPlatformType_Sandbox(t *testing.T) {
})

for _, endpointId := range endpointPlatformTypes {
_, err := definitions.GetSourceDefinition(definitions.GetSourceConfigOptions{EndpointId: endpointId, Env: pkg.FastenLighthouseEnvSandbox})
_, err := definitions.GetSourceDefinition(
definitions.WithEndpointId(endpointId),
definitions.WithEnv(pkg.FastenLighthouseEnvSandbox),
)
require.NoError(t, err)
}

Expand Down
24 changes: 6 additions & 18 deletions clients/internal/base/fhir401_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ func TestFHIR401Client_ProcessBundle_Cigna(t *testing.T) {
"type": "test",
})

cignaSandboxDefinition, err := definitions.GetSourceDefinition(definitions.GetSourceConfigOptions{
EndpointId: "6c0454af-1631-4c4d-905d-5710439df983",
})
cignaSandboxDefinition, err := definitions.GetSourceDefinition(definitions.WithEndpointId("6c0454af-1631-4c4d-905d-5710439df983"))
require.NoError(t, err)

client, err := GetSourceClientFHIR401(pkg.FastenLighthouseEnvSandbox, context.Background(), testLogger, sc, cignaSandboxDefinition, models.WithTestHttpClient(&http.Client{}))
Expand Down Expand Up @@ -88,9 +86,7 @@ func TestFHIR401Client_ProcessBundle_Cerner(t *testing.T) {
"type": "test",
})

cernerSandboxDefinition, err := definitions.GetSourceDefinition(definitions.GetSourceConfigOptions{
EndpointId: "3290e5d7-978e-42ad-b661-1cf8a01a989c",
})
cernerSandboxDefinition, err := definitions.GetSourceDefinition(definitions.WithEndpointId("3290e5d7-978e-42ad-b661-1cf8a01a989c"))
require.NoError(t, err)

client, err := GetSourceClientFHIR401(pkg.FastenLighthouseEnvSandbox, context.Background(), testLogger, sc, cernerSandboxDefinition, models.WithTestHttpClient(&http.Client{}))
Expand Down Expand Up @@ -123,9 +119,7 @@ func TestFhir401Client_ProcessResource(t *testing.T) {
"type": "test",
})

cernerSandboxDefinition, err := definitions.GetSourceDefinition(definitions.GetSourceConfigOptions{
EndpointId: "3290e5d7-978e-42ad-b661-1cf8a01a989c",
})
cernerSandboxDefinition, err := definitions.GetSourceDefinition(definitions.WithEndpointId("3290e5d7-978e-42ad-b661-1cf8a01a989c"))
require.NoError(t, err)

client, err := GetSourceClientFHIR401(pkg.FastenLighthouseEnvSandbox, context.Background(), testLogger, sc, cernerSandboxDefinition, models.WithTestHttpClient(&http.Client{}))
Expand Down Expand Up @@ -174,9 +168,7 @@ func TestFhir401Client_ProcessEncounterResource_WhichContainsCapitalizedStatusEn
"type": "test",
})

cernerSandboxDefinition, err := definitions.GetSourceDefinition(definitions.GetSourceConfigOptions{
EndpointId: "3290e5d7-978e-42ad-b661-1cf8a01a989c",
})
cernerSandboxDefinition, err := definitions.GetSourceDefinition(definitions.WithEndpointId("3290e5d7-978e-42ad-b661-1cf8a01a989c"))
require.NoError(t, err)

client, err := GetSourceClientFHIR401(pkg.FastenLighthouseEnvSandbox, context.Background(), testLogger, sc, cernerSandboxDefinition, models.WithTestHttpClient(&http.Client{}))
Expand Down Expand Up @@ -221,9 +213,7 @@ func TestFhir401Client_ProcessObservationResource_WhichContainsUnicodeCharacters
"type": "test",
})

cernerSandboxDefinition, err := definitions.GetSourceDefinition(definitions.GetSourceConfigOptions{
EndpointId: "3290e5d7-978e-42ad-b661-1cf8a01a989c",
})
cernerSandboxDefinition, err := definitions.GetSourceDefinition(definitions.WithEndpointId("3290e5d7-978e-42ad-b661-1cf8a01a989c"))
require.NoError(t, err)

client, err := GetSourceClientFHIR401(pkg.FastenLighthouseEnvSandbox, context.Background(), testLogger, sc, cernerSandboxDefinition, models.WithTestHttpClient(&http.Client{}))
Expand Down Expand Up @@ -267,9 +257,7 @@ func TestFhir401Client_ProcessResourceWithContainedResources(t *testing.T) {
testLogger := logrus.WithFields(logrus.Fields{
"type": "test",
})
medicareSandboxDefinition, err := definitions.GetSourceDefinition(definitions.GetSourceConfigOptions{
EndpointId: "6ae6c14e-b927-4ce0-862f-91123cb8d774",
})
medicareSandboxDefinition, err := definitions.GetSourceDefinition(definitions.WithEndpointId("6ae6c14e-b927-4ce0-862f-91123cb8d774"))
require.NoError(t, err)

client, err := GetSourceClientFHIR401(pkg.FastenLighthouseEnvSandbox, context.Background(), testLogger, sc, medicareSandboxDefinition, models.WithTestHttpClient(&http.Client{}))
Expand Down
8 changes: 4 additions & 4 deletions clients/internal/dynamic_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ type dynamicSourceClient struct {
func GetDynamicSourceClient(env pkg.FastenLighthouseEnvType, ctx context.Context, globalLogger logrus.FieldLogger, sourceCreds models.SourceCredential, clientOptions ...func(options *models.SourceClientOptions)) (models.SourceClient, error) {

//get the endpoint definition
endpointDefinition, err := definitions.GetSourceDefinition(definitions.GetSourceConfigOptions{
EndpointId: sourceCreds.GetEndpointId(),
Env: env,
})
endpointDefinition, err := definitions.GetSourceDefinition(
definitions.WithEndpointId(sourceCreds.GetEndpointId()),
definitions.WithEnv(env),
)
if err != nil {
return nil, err
}
Expand Down
66 changes: 62 additions & 4 deletions definitions/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,75 @@ import (
//go:embed platform/*.yaml
var platformFs embed.FS

type GetSourceConfigOptions struct {
type SourceDefinitionOptions struct {
//One of the following is required: PlatformType or EndpointId
PlatformType pkg.PlatformType
EndpointId string

//Optional - filter's the endpoint id by the environment (sandbox, prod).
Env pkg.FastenLighthouseEnvType
//Optional - sets the Client ID for the SourceConfig
ClientIdLookup map[pkg.PlatformType]string
ClientIdLookupFn func(pkg.PlatformType, string, pkg.FastenLighthouseEnvType) map[pkg.PlatformType]string

//Optional - post-Populate hook can be used to further customize the SourceDefinition
PostPopulateFn func(*models.LighthouseSourceDefinition)
}

func WithPlatformType(platformType pkg.PlatformType) func(*SourceDefinitionOptions) {
return func(o *SourceDefinitionOptions) {
o.PlatformType = platformType
}
}

func WithEndpointId(endpointId string) func(*SourceDefinitionOptions) {
return func(o *SourceDefinitionOptions) {
o.EndpointId = endpointId
}
}

func WithEnv(env pkg.FastenLighthouseEnvType) func(*SourceDefinitionOptions) {
return func(o *SourceDefinitionOptions) {
o.Env = env
}
}

func WithClientIdLookup(lookup map[pkg.PlatformType]string) func(*SourceDefinitionOptions) {
return func(o *SourceDefinitionOptions) {
o.ClientIdLookupFn = func(pkg.PlatformType, string, pkg.FastenLighthouseEnvType) map[pkg.PlatformType]string {
return lookup
}
}
}

func WithClientIdLookupFn(lookup func(pkg.PlatformType, string, pkg.FastenLighthouseEnvType) map[pkg.PlatformType]string) func(*SourceDefinitionOptions) {
return func(o *SourceDefinitionOptions) {
o.ClientIdLookupFn = lookup
}
}

func WithPostPopulateFn(postPopulateFn func(*models.LighthouseSourceDefinition)) func(*SourceDefinitionOptions) {
return func(o *SourceDefinitionOptions) {
o.PostPopulateFn = postPopulateFn
}
}

func GetSourceDefinition(
options GetSourceConfigOptions,
defOptions ...func(options *SourceDefinitionOptions),
// options SourceDefinitionOptions,
) (*models.LighthouseSourceDefinition, error) {

options := &SourceDefinitionOptions{
ClientIdLookupFn: func(pkg.PlatformType, string, pkg.FastenLighthouseEnvType) map[pkg.PlatformType]string {
return nil //noop
},
PostPopulateFn: func(definition *models.LighthouseSourceDefinition) {
return //noop
},
}
for _, o := range defOptions {
o(options)
}

if len(options.PlatformType) > 0 {
//only manual and fasten can be retrieved directly, all other Endpoint configs are retrieved via the catalog (endpointId -> platformType -> platformDefinition)
if options.PlatformType == pkg.PlatformTypeManual || options.PlatformType == pkg.PlatformTypeFasten || options.PlatformType == pkg.PlatformTypeHIE {
Expand Down Expand Up @@ -69,7 +123,11 @@ func GetSourceDefinition(
return nil, fmt.Errorf("error retrieving platform definition (%s): %w", platformType, err)
}
//platform environment specific customizations happen in Populate method
platformDefinition.Populate(&endpoint, options.Env, options.ClientIdLookup)
platformDefinition.Populate(&endpoint, options.Env, options.ClientIdLookupFn(platformType, options.EndpointId, options.Env))

if options.PostPopulateFn != nil {
options.PostPopulateFn(platformDefinition)
}

return platformDefinition, err
} else {
Expand Down
4 changes: 1 addition & 3 deletions tools/test-smart-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,7 @@ func GenerateAuthClient(requestData *ResourceRequest, proxyAddr *string, logger
bgContext := context.WithValue(context.Background(), "AUTH_USERNAME", "temp")

//check if a definition exists for this endpoint/platform
if existingDef, err := definitions.GetSourceDefinition(definitions.GetSourceConfigOptions{
EndpointId: requestData.SourceDefinition.Id,
}); err == nil && existingDef != nil {
if existingDef, err := definitions.GetSourceDefinition(definitions.WithEndpointId(requestData.SourceDefinition.Id)); err == nil && existingDef != nil {
logger.Infof("found existing definition for %s: %v", requestData.SourceDefinition.Id, existingDef)
//an exsting definition was found for this platform type, lets add some of the options to this custom definition
requestData.SourceDefinition.ClientHeaders = existingDef.ClientHeaders
Expand Down

0 comments on commit 7f7fad4

Please sign in to comment.