-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
80 lines (63 loc) · 1.48 KB
/
utils.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
80
package sarufi
import (
"fmt"
"io"
"net/http"
"os"
"time"
)
const baseURL = "https://developers.sarufi.io/"
var token = ""
// A helper function to check if a file exists. It
// accepts a filename string and returns a bool.
func fileChecker(fileName string) bool {
if len(fileName) > 50 {
return false
}
_, error := os.Stat(fileName)
// check if error is "file not exists"
if !os.IsNotExist(error) {
return true
} else {
return false
}
}
func checkToken(token string) bool {
return token != ""
}
// A helper function to make requests easier
func makeRequest(method, url string, data io.Reader) (int, []byte, error) {
req, err := http.NewRequest(method, url, data)
if err != nil {
return 0, nil, err
}
req.Header.Set("Content-Type", "application/json")
bearer := fmt.Sprintf("Bearer %s", token)
req.Header.Set("Authorization", bearer)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return 0, nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return 0, nil, err
}
return resp.StatusCode, body, nil
}
// ToDo;
// Make custom way of parsing time
type CustomTime struct {
time.Time
}
func (m *CustomTime) UnmarshalJSON(data []byte) error {
// Ignore null, like in the main JSON package.
if string(data) == "null" || string(data) == `""` {
return nil
}
// Fractional seconds are handled implicitly by Parse.
tt, err := time.Parse(`"`+time.RFC3339+`"`, string(data))
*m = CustomTime{tt}
return err
}