-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpinning_getPinByID.go
60 lines (47 loc) · 1.52 KB
/
pinning_getPinByID.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package pinningcli
import (
"encoding/json"
"fmt"
"io"
"net/http"
log "github.com/sirupsen/logrus"
)
type pinResponse struct {
Data Pin `json:"data"`
Status string `json:"status"`
}
type GetPinByIDCriteria struct {
ID string
}
func (pinning Pinning) getPinByID(ids PinningIdentifiers, criteria GetPinByIDCriteria) (Pin, []error) {
query := fmt.Sprintf("%s/pinning/%s", baseUrl, criteria.ID)
log.Debug("GET ", query)
req, err := http.NewRequest("GET", query, nil)
if err != nil {
return Pin{}, []error{fmt.Errorf("failed to call Pinning server with GET at %s", query), err}
}
req.Header.Add("pinning_api_key", ids.ApiKey)
req.Header.Add("pinning_secret_key", ids.SecretKey)
resp, err := httpClient.Do(req)
if err != nil {
return Pin{}, []error{fmt.Errorf("failed to call Pinning server with GET at %s", query), err}
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return Pin{}, []error{fmt.Errorf("failed to read body while calling Pinning server with GET at %s", query), err}
}
log.Trace("Status code: ", resp.Status)
log.Trace("Response body: ", string(body))
if resp.StatusCode != 200 {
return Pin{}, []error{fmt.Errorf("failed to call Pinning server with GET at %s", query),
fmt.Errorf("response body: %s", string(body)),
}
}
var response pinResponse
err = json.Unmarshal(body, &response)
if err != nil {
return Pin{}, []error{fmt.Errorf("failed to parse JSON response body while calling Pinning server with GET at %s", query), err}
}
return response.Data, nil
}