Skip to content

Instantly share code, notes, and snippets.

@ilmiawan
Last active January 2, 2021 05:56
Show Gist options
  • Save ilmiawan/87918ce620126fcc3a5bc1e1aadba217 to your computer and use it in GitHub Desktop.
Save ilmiawan/87918ce620126fcc3a5bc1e1aadba217 to your computer and use it in GitHub Desktop.
contoh client
package main
type client struct {
httpClient *http.Client
address string
}
// Client interface
type Client interface {
FetchUserDetailsByID(id int) (DetailsResponse, error)
}
// NewClient initial function
func NewClient(httpClient *http.Client, address string) Client {
return &client{httpClient, address}
}
// FetchUserDetailsByID method to details
func (c *client) FetchUserDetailsByID(id int) (DetailsResponse, error) {
details := DetailsResponse{}
address := fmt.Sprintf(`%s/user/details/%id`, c.address, id)
request := api.Request{
Method: "GET",
URI: address
}
res, err := api.SendHTTP(c.httpClient, request)
if err != nil {
logrus.Errorf("[ERROR] failed fetching user details: %v", err)
return details, err
}
result := UserDetails{}
err = json.Unmarshal(res, &result)
if err != nil {
logrus.Errorf("[ERROR] failed fetching user details: %v", err)
return details, err
}
return result, err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment