-
Notifications
You must be signed in to change notification settings - Fork 3
/
Requests.go
64 lines (53 loc) · 1.5 KB
/
Requests.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
61
62
63
64
package main
import (
"encoding/json"
"errors"
"fmt"
"net/http"
)
func Requests(cid string, status string, offset string) (RequestsResponse, error) {
jwt, err := findToken()
if err != nil {
return RequestsResponse{}, err
}
host := GetHost()
url := fmt.Sprintf("https://%s/pinning/pinJobs?sort=DESC", host)
if cid != "null" {
url += "&ipfs_pin_hash=" + cid
}
if offset != "null" {
url += "&offset=" + offset
}
if status != "null" {
url += "&status=" + status
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return RequestsResponse{}, errors.Join(err, errors.New("failed to create the request"))
}
req.Header.Set("Authorization", "Bearer "+string(jwt))
req.Header.Set("content-type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return RequestsResponse{}, errors.Join(err, errors.New("failed to send the request"))
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return RequestsResponse{}, fmt.Errorf("server Returned an error %d", resp.StatusCode)
}
var response RequestsResponse
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return RequestsResponse{}, err
}
for i := 0; i < len(response.Rows); i++ {
fmt.Println("Request ID:", response.Rows[i].Id)
fmt.Println("CID:", response.Rows[i].CID)
fmt.Println("Date Started:", response.Rows[i].StartDate)
fmt.Println("Name:", response.Rows[i].Name)
fmt.Println("Status:", response.Rows[i].Status)
fmt.Println()
}
return response, nil
}