-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
42 lines (33 loc) · 950 Bytes
/
api.go
File metadata and controls
42 lines (33 loc) · 950 Bytes
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
package main
import (
"bytes"
"fmt"
"io"
"net/http"
)
func makeRequest(requestType, url, token string, payload []byte) ([]byte, error) {
client := &http.Client{}
var request *http.Request
if payload != nil {
requestBody := bytes.NewReader(payload)
request, _ = http.NewRequest(requestType, url, requestBody)
} else {
request, _ = http.NewRequest(requestType, url, nil)
}
request.Header.Set("Accept", "application/vnd.github+json")
if token != "" {
request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
}
response, err := client.Do(request)
if err != nil {
return nil, fmt.Errorf("request failed: %w", err)
}
body, _ := io.ReadAll(response.Body)
return body, nil
}
func MakeGetRequest(url string, token string) ([]byte, error) {
return makeRequest("GET", url, token, nil)
}
func MakePostRequest(url, token string, payload []byte) ([]byte, error) {
return makeRequest("POST", url, token, payload)
}