-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.go
45 lines (37 loc) · 1.34 KB
/
fetch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"context"
"encoding/json"
"form3-accountapi-client/uuid"
"io/ioutil"
"net/http"
)
// retrieves an account
func (a *accountClient) Fetch(ctx context.Context, accountId uuid.UUID) (Account, error) {
accountPath := a.baseUrl.String() + endpointAccounts + "/" + accountId.String()
requestMethod := http.MethodGet
resp, err := a.httpClient.Get(accountPath)
if err != nil {
return Account{}, newGenericAccountError(requestMethod, accountPath, err.Error(), resp.StatusCode)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
return Account{}, &ErrAccountNotFound{accountId, -1} // todo tidy this up
}
if resp.StatusCode != http.StatusOK {
return Account{}, newGenericAccountError(requestMethod, accountPath, "invalid response code from api", 0)
}
var accountResponse AccountWrapper
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return Account{}, newGenericAccountError(requestMethod, accountPath, err.Error(), resp.StatusCode)
}
err = json.Unmarshal(body, &accountResponse)
if err != nil {
return Account{}, newGenericAccountError(requestMethod, accountPath, err.Error(), resp.StatusCode)
}
if err := accountResponse.Data.Validate(); err != nil {
return Account{}, newGenericAccountError(requestMethod, accountPath, err.Error(), resp.StatusCode)
}
return accountResponse.Data, nil
}