Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Option for Downloading History #27

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (

var outputFile string
var fhirSearchQuery string
var history bool
var usePost bool

type commandStats struct {
Expand Down Expand Up @@ -291,6 +292,15 @@ Examples:
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return resourceTypes, cobra.ShellCompDirectiveNoFileComp
},
PreRunE: func(cmd *cobra.Command, args []string) error {
if history && usePost {
return fmt.Errorf("--history and --post cannot be used together")
}
if history && fhirSearchQuery != "" {
return fmt.Errorf("--history and --query cannot be used together")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
err := createClient()
if err != nil {
Expand Down Expand Up @@ -381,9 +391,17 @@ func downloadResources(client *fhir.Client, resourceType string, fhirSearchQuery
request, err = client.NewPostSearchTypeRequest(resourceType, query)
} else {
if resourceType == "" {
request, err = client.NewSearchSystemRequest(query)
if history {
request, err = client.NewHistorySystemRequest()
} else {
request, err = client.NewSearchSystemRequest(query)
}
} else {
request, err = client.NewSearchTypeRequest(resourceType, query)
if history {
request, err = client.NewHistoryTypeRequest(resourceType)
} else {
request, err = client.NewSearchTypeRequest(resourceType, query)
}
}
}
} else {
Expand Down Expand Up @@ -517,7 +535,7 @@ func writeResources(data *[]byte, sink io.Writer) (int, []*fm.OperationOutcome,

var buf bytes.Buffer
for _, e := range entries {
if *e.Search.Mode == fm.SearchEntryModeOutcome {
if e.Search != nil && *e.Search.Mode == fm.SearchEntryModeOutcome {
outcome, err := fm.UnmarshalOperationOutcome(e.Resource)
if err != nil {
return resources, inlineOutcomes, fmt.Errorf("could not parse an encountered inline outcome from JSON: %v\n", err)
Expand Down Expand Up @@ -575,6 +593,7 @@ func init() {
downloadCmd.Flags().StringVar(&server, "server", "", "the base URL of the server to use")
downloadCmd.Flags().StringVarP(&outputFile, "output-file", "o", "", "write to file instead of stdout")
downloadCmd.Flags().StringVarP(&fhirSearchQuery, "query", "q", "", "FHIR search query")
downloadCmd.Flags().BoolVar(&history, "history", false, "Use _history")
downloadCmd.Flags().BoolVarP(&usePost, "use-post", "p", false, "use POST to execute the search")

_ = downloadCmd.MarkFlagRequired("server")
Expand Down
24 changes: 24 additions & 0 deletions fhir/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,30 @@ func (c *Client) NewSearchSystemRequest(searchQuery url.Values) (*http.Request,
return req, nil
}

// NewHistoryTypeRequest creates a new search type interaction request that will use GET with a
// FHIR search query in the query params of the URL.
func (c *Client) NewHistoryTypeRequest(resourceType string) (*http.Request, error) {
_url := c.baseURL.JoinPath(resourceType, "_history")
req, err := http.NewRequest("GET", _url.String(), nil)
if err != nil {
return nil, err
}
req.Header.Add("Accept", fhirJson)
return req, nil
}

// NewHistorySystemRequest creates a new history system interaction request that will use GET on a
// FHIR history endpoint.
func (c *Client) NewHistorySystemRequest() (*http.Request, error) {
_url := c.baseURL.JoinPath("_history")
req, err := http.NewRequest("GET", _url.String(), nil)
if err != nil {
return nil, err
}
req.Header.Add("Accept", fhirJson)
return req, nil
}

// NewPaginatedRequest creates a new resource interaction request based on
// a pagination link received from a FHIR server. It sets JSON Accept header and is
// otherwise identical to http.NewRequest.
Expand Down