-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
79 lines (61 loc) · 1.57 KB
/
api.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"log/slog"
"net/http"
)
func GetProjectApi(limit int, offset int, authorizeKey string) (*Response, error) {
url := fmt.Sprintf("https://vote.optimism.io/api/v1/retrofunding/rounds/5/projects?limit=%d&offset=%d", limit, offset)
method := "GET"
client := &http.Client{}
req, err := http.NewRequest(method, url, nil)
if err != nil {
slog.Error("failed to make new request", "error", err)
return nil, err
}
req.Header.Add("accept", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", authorizeKey))
res, err := client.Do(req)
if err != nil {
slog.Error("failed to client do", "error", err)
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
slog.Error("failed to make io readall", "error", err)
return nil, err
}
var parseRes Response
err = json.Unmarshal(body, &parseRes)
if err != nil {
log.Printf("error decoding response: %v", err)
if e, ok := err.(*json.SyntaxError); ok {
log.Printf("syntax error at byte offset %d", e.Offset)
}
log.Printf("response: %q", body)
return nil, err
}
return &parseRes, nil
}
func GetAllProjects(authorizeKey string) ([]Project, error) {
var conditionBreak bool = false
var limit, offset int = 100, 0
var datas []Project
for !conditionBreak {
res, err := GetProjectApi(limit, offset, authorizeKey)
if err != nil {
return datas, err
}
datas = append(datas, res.Data...)
if res.Meta.HasNext {
offset += 100
} else {
conditionBreak = true
}
}
return datas, nil
}