-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
85 lines (69 loc) · 1.68 KB
/
client.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
81
82
83
84
85
package gopddikti
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
)
type Client struct {
clientConfig ClientConfig
}
// InitClient is used to init the PDDikti SDK Client
func InitClient(cfg *ClientConfig) (*Client, error) {
var (
config ClientConfig
err error
)
if cfg == nil {
config = DefaultConfig()
} else {
config, err = CustomConfig(cfg)
}
go func() {
initRegex()
}()
return &Client{
clientConfig: config,
}, err
}
func (c *Client) createRequest(method, path string) (*http.Request, error) {
urlPath := c.clientConfig.baseURL + path
req, err := http.NewRequest(method, urlPath, nil)
if err != nil {
return req, err
}
req.Header.Set("Accept", "application/json, text/plain, */*")
req.Header.Set("Content-Type", "application/json; charset=utf-8")
return req, err
}
func (c *Client) doRequest(req *http.Request, v any) error {
res, err := c.clientConfig.httpClient.Do(req)
if err != nil {
return err
}
if res != nil {
defer res.Body.Close()
} else {
return errors.New("response is nil")
}
if res.StatusCode != http.StatusOK {
return handleSpecialStatusCodeResponse(res.StatusCode)
}
return doDecode(res, v)
}
func doDecode(res *http.Response, v any) error {
b, err := io.ReadAll(res.Body)
if err != nil {
return nil
}
return json.Unmarshal(b, v)
}
func handleSpecialStatusCodeResponse(code int) error {
// 403 error code is the most common error code returned by PDDikti
// it is caused by expired certificate, but sometimes will be fixed in several days
if code == http.StatusForbidden {
return fmt.Errorf("%d: forbidden, could be caused by expired certificate", code)
}
return fmt.Errorf("%d: error happened", code)
}