-
Notifications
You must be signed in to change notification settings - Fork 0
/
github_api.go
43 lines (36 loc) · 1.02 KB
/
github_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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
type GitHubApi struct {
accessToken string
}
type RequestBody struct {
EventType string `json:"event_type"`
}
func NewGitHubApi(env Env) *GitHubApi {
return &GitHubApi{accessToken: env.GitHub.AccessToken}
}
func (api *GitHubApi) CreateRepositoryDispatchEvent() error {
requestBody := RequestBody{EventType: "blog-updated"}
b, err := json.Marshal(requestBody)
if err != nil {
return fmt.Errorf("failed to marshal request body: %w", err)
}
client := &http.Client{}
req, err := http.NewRequest("POST", "https://api.github.com/repos/azujuuuuuun/azujuuuuuun.github.io/dispatches", bytes.NewBuffer(b))
if err != nil {
return fmt.Errorf("failed to construct a request: %w", err)
}
req.Header.Add("Accept", "application/vnd.github+json")
req.Header.Add("Authorization", "Bearer "+api.accessToken)
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("failed to request repository dispatch: %w", err)
}
defer resp.Body.Close()
return nil
}