Last active
January 2, 2021 05:56
-
-
Save ilmiawan/87918ce620126fcc3a5bc1e1aadba217 to your computer and use it in GitHub Desktop.
contoh client
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
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