-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from savannahghi/onboarding-isc-client
feat: add onboarding isc client
- Loading branch information
Showing
2 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package interserviceclient | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
"github.com/savannahghi/profileutils" | ||
) | ||
|
||
// application endpoints | ||
const ( | ||
registerUser = "internal/register_user" | ||
) | ||
|
||
// OnboardingISC is a representation of an ISC client | ||
type OnboardingISC interface { | ||
RegisterUser(ctx context.Context, payload interface{}) (*profileutils.UserProfile, error) | ||
} | ||
|
||
//OnboardingISCImpl represents the implemented methods in this ISC | ||
type OnboardingISCImpl struct { | ||
isc *InterServiceClient | ||
} | ||
|
||
//NewOnboardingISC initializes a new instance of OnboardingISC | ||
func NewOnboardingISC(isc *InterServiceClient) OnboardingISC { | ||
return &OnboardingISCImpl{ | ||
isc: isc, | ||
} | ||
} | ||
|
||
//RegisterUser makes the request to register a user | ||
func (o *OnboardingISCImpl) RegisterUser(ctx context.Context, payload interface{}) (*profileutils.UserProfile, error) { | ||
res, err := o.isc.MakeRequest(ctx, http.MethodPost, registerUser, payload) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to send request, error: %v", err) | ||
} | ||
|
||
if res.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf("register user failed with status code: %v", res.StatusCode) | ||
} | ||
|
||
data, err := ioutil.ReadAll(res.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to read response body: %w", err) | ||
} | ||
|
||
userprofile := profileutils.UserProfile{} | ||
|
||
err = json.Unmarshal(data, &userprofile) | ||
if err != nil { | ||
return nil, fmt.Errorf("error parsing response body, %v", err) | ||
} | ||
|
||
return &userprofile, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package interserviceclient_test | ||
|
||
// TODO enable this test to run once onboarding repository is updated | ||
// func TestOnboardingISCImpl_RegisterUser(t *testing.T) { | ||
// ctx := context.Background() | ||
// isc, _ := interserviceclient.NewInterserviceClient(interserviceclient.ISCService{ | ||
// Name: "onboarding", | ||
// RootDomain: "https://profile-staging.healthcloud.co.ke", | ||
// }) | ||
|
||
// onboarding := interserviceclient.NewOnboardingISC(isc) | ||
|
||
// input := struct { | ||
// FirstName string | ||
// LastName string | ||
// PhoneNumber string | ||
// DateOfBirth scalarutils.Date | ||
// }{ | ||
// FirstName: "Test", | ||
// LastName: "Test2", | ||
// PhoneNumber: interserviceclient.TestUserPhoneNumber, | ||
// DateOfBirth: scalarutils.Date{ | ||
// Day: 1, | ||
// Month: 1, | ||
// Year: 2020, | ||
// }, | ||
// } | ||
|
||
// type args struct { | ||
// ctx context.Context | ||
// payload interface{} | ||
// } | ||
// tests := []struct { | ||
// name string | ||
// args args | ||
// want *profileutils.UserProfile | ||
// wantErr bool | ||
// }{ | ||
// { | ||
// name: "happy registered a user", | ||
// args: args{ | ||
// ctx: ctx, | ||
// payload: input, | ||
// }, | ||
// want: &profileutils.UserProfile{}, | ||
// wantErr: false, | ||
// }, | ||
// } | ||
// for _, tt := range tests { | ||
// t.Run(tt.name, func(t *testing.T) { | ||
// got, err := onboarding.RegisterUser(tt.args.ctx, tt.args.payload) | ||
// if (err != nil) != tt.wantErr { | ||
// t.Errorf("OnboardingISCImpl.RegisterUser() error = %v, wantErr %v", err, tt.wantErr) | ||
// return | ||
// } | ||
// if !reflect.DeepEqual(got, tt.want) { | ||
// t.Errorf("OnboardingISCImpl.RegisterUser() = %v, want %v", got, tt.want) | ||
// } | ||
// }) | ||
// } | ||
// } |