From 011acac55fda5c785dd40196f3139511c083eb54 Mon Sep 17 00:00:00 2001 From: BiKodes Date: Wed, 17 Apr 2024 09:34:24 +0300 Subject: [PATCH] chore(CPL-319): Closing open server requests and code refactor --- healthcrm.go | 14 +++++ input.go | 96 ++++++++++++++--------------- output.go | 169 +++++++++++++++++++++++++-------------------------- 3 files changed, 146 insertions(+), 133 deletions(-) diff --git a/healthcrm.go b/healthcrm.go index c9d3401..2012d36 100644 --- a/healthcrm.go +++ b/healthcrm.go @@ -48,6 +48,8 @@ func (h *HealthCRMLib) CreateFacility(ctx context.Context, facility *Facility) ( return nil, err } + defer response.Body.Close() + respBytes, err := io.ReadAll(response.Body) if err != nil { return nil, fmt.Errorf("could not read response: %w", err) @@ -75,6 +77,8 @@ func (h *HealthCRMLib) GetFacilityByID(ctx context.Context, id string) (*Facilit return nil, err } + defer response.Body.Close() + respBytes, err := io.ReadAll(response.Body) if err != nil { return nil, fmt.Errorf("could not read response: %w", err) @@ -102,6 +106,8 @@ func (h *HealthCRMLib) UpdateFacility(ctx context.Context, id string, updatePayl return nil, err } + defer response.Body.Close() + respBytes, err := io.ReadAll(response.Body) if err != nil { return nil, fmt.Errorf("could not read response: %w", err) @@ -177,6 +183,8 @@ func (h *HealthCRMLib) GetFacilitiesOfferingAService(ctx context.Context, servic return nil, err } + defer response.Body.Close() + respBytes, err := io.ReadAll(response.Body) if err != nil { return nil, fmt.Errorf("could not read response: %w", err) @@ -205,6 +213,8 @@ func (h *HealthCRMLib) CreateService(ctx context.Context, input FacilityServiceI return nil, err } + defer response.Body.Close() + respBytes, err := io.ReadAll(response.Body) if err != nil { return nil, fmt.Errorf("could not read response: %w", err) @@ -233,6 +243,8 @@ func (h *HealthCRMLib) LinkServiceToFacility(ctx context.Context, facilityID str return nil, err } + defer response.Body.Close() + respBytes, err := io.ReadAll(response.Body) if err != nil { return nil, fmt.Errorf("could not read response: %w", err) @@ -319,6 +331,8 @@ func (h *HealthCRMLib) GetFacilities(ctx context.Context, location *Coordinates, return nil, err } + defer response.Body.Close() + respBytes, err := io.ReadAll(response.Body) if err != nil { return nil, fmt.Errorf("could not read response: %w", err) diff --git a/input.go b/input.go index afb7104..083d680 100644 --- a/input.go +++ b/input.go @@ -4,26 +4,26 @@ import "fmt" // Facility is the hospitals data class type Facility struct { - ID string `json:"id,omitempty"` - Name string `json:"name,omitempty"` - Description string `json:"description,omitempty"` - FacilityType string `json:"facility_type,omitempty"` - County string `json:"county,omitempty"` - Country string `json:"country,omitempty"` - Address string `json:"address,omitempty"` - Coordinates *Coordinates `json:"coordinates,omitempty"` - Contacts []Contacts `json:"contacts,omitempty"` - Identifiers []Identifiers `json:"identifiers,omitempty"` - BusinessHours []BusinessHours `json:"businesshours,omitempty"` + ID string `json:"id,omitempty"` + Name string `json:"name,omitempty"` + Description string `json:"description,omitempty"` + FacilityType string `json:"facility_type,omitempty"` + County string `json:"county,omitempty"` + Country string `json:"country,omitempty"` + Address string `json:"address,omitempty"` + Coordinates *Coordinates `json:"coordinates,omitempty"` + Contacts []Contacts `json:"contacts,omitempty"` + Identifiers []Identifiers `json:"identifiers,omitempty"` + BusinessHours []BusinessHours `json:"businesshours,omitempty"` } // Coordinates represents geographical coordinates using latitude and longitude. // Latitude measures the north-south position, while longitude measures // the east-west position. type Coordinates struct { - Latitude string `json:"latitude,omitempty"` - Longitude string `json:"longitude,omitempty"` - Radius string `json:"radius,omitempty"` + Latitude string `json:"latitude,omitempty"` + Longitude string `json:"longitude,omitempty"` + Radius string `json:"radius,omitempty"` } // ToString returns the location in comma-separated values format. @@ -40,67 +40,67 @@ func (c Coordinates) ToString() (string, error) { // Contacts models facility's model data class type Contacts struct { - ContactType string `json:"contact_type,omitempty"` - ContactValue string `json:"contact_value,omitempty"` - Role string `json:"role,omitempty"` -} + ContactType string `json:"contact_type,omitempty"` + ContactValue string `json:"contact_value,omitempty"` + Role string `json:"role,omitempty"` +} // Identifiers models facility's identifiers; can be MFL Code, Slade Code etc... type Identifiers struct { - IdentifierType string `json:"identifier_type,omitempty"` - IdentifierValue string `json:"identifier_value,omitempty"` - ValidFrom string `json:"valid_from,omitempty"` - ValidTo string `json:"valid_to,omitempty"` + IdentifierType string `json:"identifier_type,omitempty"` + IdentifierValue string `json:"identifier_value,omitempty"` + ValidFrom string `json:"valid_from,omitempty"` + ValidTo string `json:"valid_to,omitempty"` } // BusinessHours models data to store business hours type BusinessHours struct { - Day string `json:"day"` - OpeningTime string `json:"opening_time"` - ClosingTime string `json:"closing_time"` + Day string `json:"day"` + OpeningTime string `json:"opening_time"` + ClosingTime string `json:"closing_time"` } // Pagination is used to hold pagination values type Pagination struct { - Page string `json:"page"` - PageSize string `json:"page_size"` + Page string `json:"page"` + PageSize string `json:"page_size"` } // FacilityServiceInput models is used to create a new service type FacilityServiceInput struct { - Name string `json:"name"` - Description string `json:"description"` - Identifiers []*ServiceIdentifierInput `json:"identifiers"` + Name string `json:"name"` + Description string `json:"description"` + Identifiers []*ServiceIdentifierInput `json:"identifiers"` } // ServiceIdentifierInput is used to create an identifier type ServiceIdentifierInput struct { - IdentifierType string `json:"identifier_type"` - IdentifierValue string `json:"identifier_value"` + IdentifierType string `json:"identifier_type"` + IdentifierValue string `json:"identifier_value"` } // ProfileInput is the host of users data or a brief description of a person type ProfileInput struct { - FirstName string `json:"first_name"` - LastName string `json:"last_name"` - OtherName string `json:"other_name"` - DateOfBirth string `json:"date_of_birth"` - Gender string `json:"gender"` - EnrolmentDate string `json:"enrolment_date"` - SladeCode string `json:"slade_code"` - ServiceCode string `json:"service_code"` - Contacts []*ProfileContactInput `json:"contacts,omitempty"` - Identifiers []*ProfileIdentifierInput `json:"identifiers,omitempty"` + FirstName string `json:"first_name"` + LastName string `json:"last_name"` + OtherName string `json:"other_name"` + DateOfBirth string `json:"date_of_birth"` + Gender string `json:"gender"` + EnrolmentDate string `json:"enrolment_date"` + SladeCode string `json:"slade_code"` + ServiceCode string `json:"service_code"` + Contacts []*ProfileContactInput `json:"contacts,omitempty"` + Identifiers []*ProfileIdentifierInput `json:"identifiers,omitempty"` } -// ProfileIdentifierInput is used to create profile(s) identifier(s) +// ProfileIdentifierInput is used to create profile(s) identifier(s) type ProfileIdentifierInput struct { - IdentifierValue string `json:"identifier_value"` - IdentifierType string `json:"identifier_type"` + IdentifierValue string `json:"identifier_value"` + IdentifierType string `json:"identifier_type"` } -// ProfileContanctInput is used to create profile(s) contact(s) +// ProfileContanctInput is used to create profile(s) contact(s) type ProfileContactInput struct { - ContactType string `json:"contact_type"` - ContactValue string `json:"contact_value"` + ContactType string `json:"contact_type"` + ContactValue string `json:"contact_value"` } diff --git a/output.go b/output.go index 571c404..afb005d 100644 --- a/output.go +++ b/output.go @@ -4,132 +4,131 @@ import "time" // FacilityPage is the hospitals model used to show facility details type FacilityPage struct { - Count int `json:"count"` - Next string `json:"next"` - Previous any `json:"previous"` - PageSize int `json:"page_size"` - CurrentPage int `json:"current_page"` - TotalPages int `json:"total_pages"` - StartIndex int `json:"start_index"` - EndIndex int `json:"end_index"` - Results []FacilityOutput `json:"results"` + Count int `json:"count"` + Next string `json:"next"` + Previous any `json:"previous"` + PageSize int `json:"page_size"` + CurrentPage int `json:"current_page"` + TotalPages int `json:"total_pages"` + StartIndex int `json:"start_index"` + EndIndex int `json:"end_index"` + Results []FacilityOutput `json:"results"` } // CoordinatesOutput is used to show geographical coordinates type CoordinatesOutput struct { - Latitude float64 `json:"latitude"` - Longitude float64 `json:"longitude"` + Latitude float64 `json:"latitude"` + Longitude float64 `json:"longitude"` } // ContactsOutput is used to show facility contacts type ContactsOutput struct { - ID string `json:"id"` - ContactType string `json:"contact_type"` - ContactValue string `json:"contact_value"` - Active bool `json:"active"` - Role string `json:"role"` - FacilityID string `json:"facility_id"` + ID string `json:"id"` + ContactType string `json:"contact_type"` + ContactValue string `json:"contact_value"` + Active bool `json:"active"` + Role string `json:"role"` + FacilityID string `json:"facility_id"` } // IdentifiersOutput is used to display facility identifiers type IdentifiersOutput struct { - ID string `json:"id"` - IdentifierType string `json:"identifier_type"` - IdentifierValue string `json:"identifier_value"` - ValidFrom string `json:"valid_from"` - ValidTo string `json:"valid_to"` - FacilityID string `json:"facility_id"` + ID string `json:"id"` + IdentifierType string `json:"identifier_type"` + IdentifierValue string `json:"identifier_value"` + ValidFrom string `json:"valid_from"` + ValidTo string `json:"valid_to"` + FacilityID string `json:"facility_id"` } // FacilityOutput is used to display facility(ies) type FacilityOutput struct { - ID string `json:"id,omitempty"` - Created time.Time `json:"created,omitempty"` - Name string `json:"name,omitempty"` - Description string `json:"description,omitempty"` - FacilityType string `json:"facility_type,omitempty"` - County string `json:"county,omitempty"` - Country string `json:"country,omitempty"` - Coordinates CoordinatesOutput `json:"coordinates,omitempty"` - Distance float64 `json:"distance,omitempty"` - Status string `json:"status,omitempty"` - Address string `json:"address,omitempty"` - Contacts []ContactsOutput `json:"contacts,omitempty"` - Identifiers []IdentifiersOutput `json:"identifiers,omitempty"` - BusinessHours []BusinessHoursOutput `json:"businesshours,omitempty"` - Services []FacilityService `json:"services,omitempty"` + ID string `json:"id,omitempty"` + Created time.Time `json:"created,omitempty"` + Name string `json:"name,omitempty"` + Description string `json:"description,omitempty"` + FacilityType string `json:"facility_type,omitempty"` + County string `json:"county,omitempty"` + Country string `json:"country,omitempty"` + Coordinates CoordinatesOutput `json:"coordinates,omitempty"` + Distance float64 `json:"distance,omitempty"` + Status string `json:"status,omitempty"` + Address string `json:"address,omitempty"` + Contacts []ContactsOutput `json:"contacts,omitempty"` + Identifiers []IdentifiersOutput `json:"identifiers,omitempty"` + BusinessHours []BusinessHoursOutput `json:"businesshours,omitempty"` + Services []FacilityService `json:"services,omitempty"` } // BusinessHoursOutput models data that show facility's operational hours type BusinessHoursOutput struct { - ID string `json:"id"` - Day string `json:"day"` - OpeningTime string `json:"opening_time"` - ClosingTime string `json:"closing_time"` - FacilityID string `json:"facility_id"` + ID string `json:"id"` + Day string `json:"day"` + OpeningTime string `json:"opening_time"` + ClosingTime string `json:"closing_time"` + FacilityID string `json:"facility_id"` } // FacilityServicePage models the services offered in a facility type FacilityServicePage struct { - Results []FacilityService `json:"results"` - Count int `json:"count"` - Next string `json:"next"` - Previous string `json:"previous"` - PageSize int `json:"page_size"` - CurrentPage int `json:"current_page"` - TotalPages int `json:"total_pages"` - StartIndex int `json:"start_index"` - EndIndex int `json:"end_index"` + Results []FacilityService `json:"results"` + Count int `json:"count"` + Next string `json:"next"` + Previous string `json:"previous"` + PageSize int `json:"page_size"` + CurrentPage int `json:"current_page"` + TotalPages int `json:"total_pages"` + StartIndex int `json:"start_index"` + EndIndex int `json:"end_index"` } // FacilityService models the data class that is used to show facility services type FacilityService struct { - ID string `json:"id"` - Name string `json:"name"` - Description string `json:"description"` - Identifiers []*ServiceIdentifier `json:"identifiers"` + ID string `json:"id"` + Name string `json:"name"` + Description string `json:"description"` + Identifiers []*ServiceIdentifier `json:"identifiers"` } // ServiceIdentifier models the structure of facility's service identifiers type ServiceIdentifier struct { - ID string `json:"id"` - IdentifierType string `json:"identifier_type"` - IdentifierValue string `json:"identifier_value"` - ServiceID string `json:"service_id"` + ID string `json:"id"` + IdentifierType string `json:"identifier_type"` + IdentifierValue string `json:"identifier_value"` + ServiceID string `json:"service_id"` } -// // ProfileOutput is used to display profile(s) type ProfileOutput struct { - ID string `json:"id,omitempty"` - Created string `json:"created,omitempty"` - Active bool `json:"active"` - FirstName string `json:"first_name"` - LastName string `json:"last_name"` - OtherName string `json:"other_name"` - DateOfBirth string `json:"date_of_birth"` - Gender string `json:"gender"` - EnrolmentDate string `json:"enrolment_date"` - SladeCode string `json:"slade_code"` - ServiceCode string `json:"service_code"` - Contacts []*ProfileContactOutput `json:"contacts,omitempty"` - Identifiers []*ProfileIdentifierOutput `json:"identifiers,omitempty"` + ID string `json:"id,omitempty"` + Created string `json:"created,omitempty"` + Active bool `json:"active"` + FirstName string `json:"first_name"` + LastName string `json:"last_name"` + OtherName string `json:"other_name"` + DateOfBirth string `json:"date_of_birth"` + Gender string `json:"gender"` + EnrolmentDate string `json:"enrolment_date"` + SladeCode string `json:"slade_code"` + ServiceCode string `json:"service_code"` + Contacts []*ProfileContactOutput `json:"contacts,omitempty"` + Identifiers []*ProfileIdentifierOutput `json:"identifiers,omitempty"` } // ProfileContactOutput is used to show profile contacts type ProfileContactOutput struct { - ID string `json:"id"` - ContactType string `json:"contact_type"` - ContactValue string `json:"contact_value"` - DateVerified string `json:"date_verified"` - Verified bool `json:"verifed,omitempty"` + ID string `json:"id"` + ContactType string `json:"contact_type"` + ContactValue string `json:"contact_value"` + DateVerified string `json:"date_verified"` + Verified bool `json:"verifed,omitempty"` } // ProfileIndentifierOutput is used to display profile identifiers type ProfileIdentifierOutput struct { - ID string `json:"id"` - IdentifierType string `json:"identifier_type"` - IdentifierValue string `json:"identifier_value"` - ValidFrom string `json:"valid_from"` - ValidTo string `json:"valid_to"` -} \ No newline at end of file + ID string `json:"id"` + IdentifierType string `json:"identifier_type"` + IdentifierValue string `json:"identifier_value"` + ValidFrom string `json:"valid_from"` + ValidTo string `json:"valid_to"` +}