Skip to content

Commit

Permalink
Split Client into an interface and an implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
gffking committed Jun 21, 2024
1 parent a715a28 commit 839192c
Show file tree
Hide file tree
Showing 46 changed files with 210 additions and 93 deletions.
2 changes: 1 addition & 1 deletion appointment.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type AppointmentServicer interface {
var _ AppointmentServicer = (*AppointmentService)(nil)

type AppointmentService struct {
client *Client
client *HttpClient
}

type AppointmentCreate struct {
Expand Down
10 changes: 5 additions & 5 deletions appointment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestAppointmentService_Create(t *testing.T) {
}))
defer srv.Close()

client := NewClient(srv.Client(), srv.URL+"/token", "", "", srv.URL)
client := NewHttpClient(srv.Client(), srv.URL+"/token", "", "", srv.URL)
svc := AppointmentService{client}

created, res, err := svc.Create(context.Background(), expected)
Expand Down Expand Up @@ -127,7 +127,7 @@ func TestAppointmentService_Find(t *testing.T) {
}))
defer srv.Close()

client := NewClient(srv.Client(), srv.URL+"/token", "", "", srv.URL)
client := NewHttpClient(srv.Client(), srv.URL+"/token", "", "", srv.URL)
svc := AppointmentService{client}

found, res, err := svc.Find(context.Background(), opts)
Expand Down Expand Up @@ -160,7 +160,7 @@ func TestAppointmentService_Get(t *testing.T) {
}))
defer srv.Close()

client := NewClient(srv.Client(), srv.URL+"/token", "", "", srv.URL)
client := NewHttpClient(srv.Client(), srv.URL+"/token", "", "", srv.URL)
svc := AppointmentService{client}

found, res, err := svc.Get(context.Background(), id)
Expand Down Expand Up @@ -210,7 +210,7 @@ func TestAppointmentService_Update(t *testing.T) {
}))
defer srv.Close()

client := NewClient(srv.Client(), srv.URL+"/token", "", "", srv.URL)
client := NewHttpClient(srv.Client(), srv.URL+"/token", "", "", srv.URL)
svc := AppointmentService{client}

found, res, err := svc.Update(context.Background(), id, expected)
Expand All @@ -234,7 +234,7 @@ func TestAppointmentService_Delete(t *testing.T) {
}))
defer srv.Close()

client := NewClient(srv.Client(), srv.URL+"/token", "", "", srv.URL)
client := NewHttpClient(srv.Client(), srv.URL+"/token", "", "", srv.URL)
svc := AppointmentService{client}

res, err := svc.Delete(context.Background(), id)
Expand Down
8 changes: 4 additions & 4 deletions cli/cmd/messaging.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ var (

var findThreadMembersCmd = &cobra.Command{
Use: "find-thread-members",
Run: wrapRunFunc(func(ctx context.Context, client *elation.Client, args []string) error {
response, _, err := client.ThreadMemberSvc.Find(ctx, &elation.FindThreadMembersOptions{
Run: wrapRunFunc(func(ctx context.Context, client elation.Client, args []string) error {
response, _, err := client.ThreadMembers().Find(ctx, &elation.FindThreadMembersOptions{
Patient: findThreadMembersPatients,
User: findThreadMembersUsers,
})
Expand All @@ -34,9 +34,9 @@ var findThreadMembersCmd = &cobra.Command{
var getMessageThreadCmd = &cobra.Command{
Use: "get-message-thread [thread ID]",
Args: cobra.ExactArgs(1),
Run: wrapRunFunc(func(ctx context.Context, client *elation.Client, args []string) error {
Run: wrapRunFunc(func(ctx context.Context, client elation.Client, args []string) error {
threadID, _ := strconv.ParseInt(args[0], 10, 64)
response, _, err := client.MessageThreadSvc.Get(ctx, threadID)
response, _, err := client.MessageThreads().Get(ctx, threadID)
if err != nil {
return err
}
Expand Down
27 changes: 27 additions & 0 deletions cli/cmd/practice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cmd

import (
"context"

"github.com/authorhealth/go-elation"
"github.com/davecgh/go-spew/spew"
"github.com/spf13/cobra"
)

var findPhysiciansCmd = &cobra.Command{
Use: "find-physicians",
Run: wrapRunFunc(func(ctx context.Context, client elation.Client, args []string) error {
response, _, err := client.Physicians().Find(ctx, &elation.FindPhysiciansOptions{})
if err != nil {
return err
}

spew.Dump(response)

return nil
}),
}

func init() {
rootCmd.AddCommand(findPhysiciansCmd)
}
4 changes: 2 additions & 2 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func Execute() {
}
}

type runFunc func(ctx context.Context, client *elation.Client, args []string) error
type runFunc func(ctx context.Context, client elation.Client, args []string) error

func wrapRunFunc(runFunc runFunc) func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
Expand All @@ -36,7 +36,7 @@ func wrapRunFunc(runFunc runFunc) func(cmd *cobra.Command, args []string) {
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
slog.SetDefault(logger)

client := elation.NewClient(
client := elation.NewHttpClient(
&http.Client{
Timeout: 15 * time.Second,
},
Expand Down
98 changes: 94 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,31 @@ const (
WebhookEventActionDeleted string = "deleted"
)

type Client struct {
type Client interface {
Appointments() AppointmentServicer
ClinicalDocuments() ClinicalDocumentServicer
Contacts() ContactServicer
DiscontinuedMedications() DiscontinuedMedicationServicer
HistoryDownloadFills() HistoryDownloadFillServicer
InsuranceCompanies() InsuranceCompanyServicer
InsuranceEligibility() InsuranceEligibilityServicer
InsurancePlans() InsurancePlanServicer
Letters() LetterServicer
Medications() MedicationServicer
MessageThreads() MessageThreadServicer
NonVisitNotes() NonVisitNoteServicer
Patients() PatientServicer
Physicians() PhysicianServicer
Practices() PracticeServicer
PrescriptionFills() PrescriptionFillServicer
Problems() ProblemServicer
RecurringEventGroups() RecurringEventGroupServicer
ServiceLocations() ServiceLocationServicer
Subscriptions() SubscriptionServicer
ThreadMembers() ThreadMemberServicer
}

type HttpClient struct {
httpClient *http.Client
baseURL string
tracer trace.Tracer
Expand Down Expand Up @@ -59,7 +83,9 @@ type Client struct {
ThreadMemberSvc *ThreadMemberService
}

func NewClient(httpClient *http.Client, tokenURL, clientID, clientSecret, baseURL string) *Client {
var _ Client = (*HttpClient)(nil)

func NewHttpClient(httpClient *http.Client, tokenURL, clientID, clientSecret, baseURL string) *HttpClient {
config := clientcredentials.Config{
ClientID: clientID,
ClientSecret: clientSecret,
Expand All @@ -68,7 +94,7 @@ func NewClient(httpClient *http.Client, tokenURL, clientID, clientSecret, baseUR

ctx := context.WithValue(context.Background(), oauth2.HTTPClient, httpClient)

client := &Client{
client := &HttpClient{
httpClient: config.Client(ctx),
baseURL: baseURL,
tracer: otel.GetTracerProvider().Tracer("github.com/authorhealth/go-elation"),
Expand Down Expand Up @@ -99,6 +125,70 @@ func NewClient(httpClient *http.Client, tokenURL, clientID, clientSecret, baseUR
return client
}

func (c *HttpClient) Appointments() AppointmentServicer {
return c.AppointmentSvc
}
func (c *HttpClient) ClinicalDocuments() ClinicalDocumentServicer {
return c.ClinicalDocumentSvc
}
func (c *HttpClient) Contacts() ContactServicer {
return c.ContactSvc
}
func (c *HttpClient) DiscontinuedMedications() DiscontinuedMedicationServicer {
return c.DiscontinuedMedicationSvc
}
func (c *HttpClient) HistoryDownloadFills() HistoryDownloadFillServicer {
return c.HistoryDownloadFillSvc
}
func (c *HttpClient) InsuranceCompanies() InsuranceCompanyServicer {
return c.InsuranceCompanySvc
}
func (c *HttpClient) InsuranceEligibility() InsuranceEligibilityServicer {
return c.InsuranceEligibilitySvc
}
func (c *HttpClient) InsurancePlans() InsurancePlanServicer {
return c.InsurancePlanSvc
}
func (c *HttpClient) Letters() LetterServicer {
return c.LetterSvc
}
func (c *HttpClient) Medications() MedicationServicer {
return c.MedicationSvc
}
func (c *HttpClient) MessageThreads() MessageThreadServicer {
return c.MessageThreadSvc
}
func (c *HttpClient) NonVisitNotes() NonVisitNoteServicer {
return c.NonVisitNoteSvc
}
func (c *HttpClient) Patients() PatientServicer {
return c.PatientSvc
}
func (c *HttpClient) Physicians() PhysicianServicer {
return c.PhysicianSvc
}
func (c *HttpClient) Practices() PracticeServicer {
return c.PracticeSvc
}
func (c *HttpClient) PrescriptionFills() PrescriptionFillServicer {
return c.PrescriptionFillSvc
}
func (c *HttpClient) Problems() ProblemServicer {
return c.ProblemSvc
}
func (c *HttpClient) RecurringEventGroups() RecurringEventGroupServicer {
return c.RecurringEventGroupService
}
func (c *HttpClient) ServiceLocations() ServiceLocationServicer {
return c.ServiceLocationSvc
}
func (c *HttpClient) Subscriptions() SubscriptionServicer {
return c.SubscriptionSvc
}
func (c *HttpClient) ThreadMembers() ThreadMemberServicer {
return c.ThreadMemberSvc
}

type Response[ResultsT any] struct {
Count int `json:"count"`
Next string `json:"next"`
Expand Down Expand Up @@ -149,7 +239,7 @@ func (e Error) Error() string {
return fmt.Sprintf("API error (status code %d)", e.StatusCode)
}

func (c *Client) request(ctx context.Context, method string, path string, query any, body any, out any) (*http.Response, error) {
func (c *HttpClient) request(ctx context.Context, method string, path string, query any, body any, out any) (*http.Response, error) {
span := trace.SpanFromContext(ctx)
span.SetAttributes(semconv.HTTPRequestMethodKey.String(method))

Expand Down
2 changes: 1 addition & 1 deletion clinical_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type ClinicalDocumentServicer interface {
var _ ClinicalDocumentServicer = (*ClinicalDocumentService)(nil)

type ClinicalDocumentService struct {
client *Client
client *HttpClient
}

type ClinicalDocument struct {
Expand Down
4 changes: 2 additions & 2 deletions clinical_document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestClinicalDocumentService_Find(t *testing.T) {
}))
defer srv.Close()

client := NewClient(srv.Client(), srv.URL+"/token", "", "", srv.URL)
client := NewHttpClient(srv.Client(), srv.URL+"/token", "", "", srv.URL)
svc := ClinicalDocumentService{client}

found, res, err := svc.Find(context.Background(), opts)
Expand Down Expand Up @@ -86,7 +86,7 @@ func TestClinicalDocumentService_Get(t *testing.T) {
}))
defer srv.Close()

client := NewClient(srv.Client(), srv.URL+"/token", "", "", srv.URL)
client := NewHttpClient(srv.Client(), srv.URL+"/token", "", "", srv.URL)
svc := ClinicalDocumentService{client}

found, res, err := svc.Get(context.Background(), id)
Expand Down
2 changes: 1 addition & 1 deletion contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type ContactServicer interface {
var _ ContactServicer = (*ContactService)(nil)

type ContactService struct {
client *Client
client *HttpClient
}

type Contact struct {
Expand Down
4 changes: 2 additions & 2 deletions contact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestContactService_List(t *testing.T) {
}))
defer srv.Close()

client := NewClient(srv.Client(), srv.URL+"/token", "", "", srv.URL)
client := NewHttpClient(srv.Client(), srv.URL+"/token", "", "", srv.URL)
svc := ContactService{client}

found, res, err := svc.List(context.Background(), opts)
Expand Down Expand Up @@ -86,7 +86,7 @@ func TestContactService_Get(t *testing.T) {
}))
defer srv.Close()

client := NewClient(srv.Client(), srv.URL+"/token", "", "", srv.URL)
client := NewHttpClient(srv.Client(), srv.URL+"/token", "", "", srv.URL)
svc := ContactService{client}

found, res, err := svc.Get(context.Background(), id)
Expand Down
2 changes: 1 addition & 1 deletion discontinued_medication.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type DiscontinuedMedicationServicer interface {
var _ DiscontinuedMedicationServicer = (*DiscontinuedMedicationService)(nil)

type DiscontinuedMedicationService struct {
client *Client
client *HttpClient
}

type DiscontinuedMedicationCreate struct {
Expand Down
8 changes: 4 additions & 4 deletions discontinued_medication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestDiscontinuedMedicationService_Create(t *testing.T) {
}))
defer srv.Close()

client := NewClient(srv.Client(), srv.URL+"/token", "", "", srv.URL)
client := NewHttpClient(srv.Client(), srv.URL+"/token", "", "", srv.URL)
svc := DiscontinuedMedicationService{client}

created, res, err := svc.Create(context.Background(), testCase.create)
Expand Down Expand Up @@ -136,7 +136,7 @@ func TestDiscontinuedMedicationService_Find(t *testing.T) {
}))
defer srv.Close()

client := NewClient(srv.Client(), srv.URL+"/token", "", "", srv.URL)
client := NewHttpClient(srv.Client(), srv.URL+"/token", "", "", srv.URL)
svc := DiscontinuedMedicationService{client}

found, res, err := svc.Find(context.Background(), opts)
Expand Down Expand Up @@ -169,7 +169,7 @@ func TestDiscontinuedMedicationService_Get(t *testing.T) {
}))
defer srv.Close()

client := NewClient(srv.Client(), srv.URL+"/token", "", "", srv.URL)
client := NewHttpClient(srv.Client(), srv.URL+"/token", "", "", srv.URL)
svc := DiscontinuedMedicationService{client}

found, res, err := svc.Get(context.Background(), id)
Expand Down Expand Up @@ -214,7 +214,7 @@ func TestDiscontinuedMedicationService_Update(t *testing.T) {
}))
defer srv.Close()

client := NewClient(srv.Client(), srv.URL+"/token", "", "", srv.URL)
client := NewHttpClient(srv.Client(), srv.URL+"/token", "", "", srv.URL)
svc := DiscontinuedMedicationService{client}

updated, res, err := svc.Update(context.Background(), id, expected)
Expand Down
2 changes: 1 addition & 1 deletion history_download_fill.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type HistoryDownloadFillServicer interface {
var _ HistoryDownloadFillServicer = (*HistoryDownloadFillService)(nil)

type HistoryDownloadFillService struct {
client *Client
client *HttpClient
}

type HistoryDownloadFill struct {
Expand Down
4 changes: 2 additions & 2 deletions history_download_fill_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestHistoryDownloadFillService_Find(t *testing.T) {
}))
defer srv.Close()

client := NewClient(srv.Client(), srv.URL+"/token", "", "", srv.URL)
client := NewHttpClient(srv.Client(), srv.URL+"/token", "", "", srv.URL)
svc := HistoryDownloadFillService{client}

found, res, err := svc.Find(context.Background(), opts)
Expand Down Expand Up @@ -86,7 +86,7 @@ func TestHistoryDownloadFillService_Get(t *testing.T) {
}))
defer srv.Close()

client := NewClient(srv.Client(), srv.URL+"/token", "", "", srv.URL)
client := NewHttpClient(srv.Client(), srv.URL+"/token", "", "", srv.URL)
svc := HistoryDownloadFillService{client}

found, res, err := svc.Get(context.Background(), id)
Expand Down
2 changes: 1 addition & 1 deletion insurance_company.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type InsuranceCompanyServicer interface {
var _ InsuranceCompanyServicer = (*InsuranceCompanyService)(nil)

type InsuranceCompanyService struct {
client *Client
client *HttpClient
}

type InsuranceCompanyCreate struct {
Expand Down
Loading

0 comments on commit 839192c

Please sign in to comment.