Skip to content

Commit

Permalink
Merge pull request #21 from savannahghi/onboarding-isc-client
Browse files Browse the repository at this point in the history
feat: add onboarding isc client
  • Loading branch information
NYARAS committed Aug 27, 2021
2 parents eb6ae1f + f7a56b1 commit 9d5b1f0
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
59 changes: 59 additions & 0 deletions onboarding_isc.go
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
}
61 changes: 61 additions & 0 deletions onboarding_isc_test.go
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)
// }
// })
// }
// }

0 comments on commit 9d5b1f0

Please sign in to comment.