-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
139 lines (110 loc) · 3.85 KB
/
app.go
File metadata and controls
139 lines (110 loc) · 3.85 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"context"
"encoding/json"
"fmt"
)
// App struct
type App struct {
ctx context.Context
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
// Greet returns a greeting for the given name
func (a *App) Greet(name string) string {
return fmt.Sprintf("Hello %s, It's show time!", name)
}
type APIResponse []interface{}
type Gist struct {
Description string `json:"description"`
Public bool `json:"public"`
Files interface{} `json:"files"`
}
const BaseUrl = "https://api.github.com"
var githubResponse APIResponse
// Retrieves a list of public repositories from the GitHub API via a GET request.
// Since this route does not require authentication, an empty string is passed as the token.
func (a *App) GetPublicRepositories() (APIResponse, error) {
url := fmt.Sprintf("%s/repositories", BaseUrl)
response, err := MakeGetRequest(url, "")
if err != nil {
return nil, err
}
json.Unmarshal(response, &githubResponse)
return githubResponse, nil
}
// Retrieves a list of public Gists from the GitHub API via a GET request.
// Authentication is also not required, hence an empty string is passed as the token.
func (a *App) GetPublicGists() (APIResponse, error) {
url := fmt.Sprintf("%s/gists/public", BaseUrl)
response, err := MakeGetRequest(url, "")
if err != nil {
return nil, err
}
json.Unmarshal(response, &githubResponse)
return githubResponse, nil
}
// Used to get a list of the authenticated user’s private repositories.
// This function takes the token as a parameter.
func (a *App) GetRepositoriesForAuthenticatedUser(token string) (APIResponse, error) {
url := fmt.Sprintf("%s/user/repos?type=private", BaseUrl)
response, err := MakeGetRequest(url, token)
if err != nil {
return nil, err
}
json.Unmarshal(response, &githubResponse)
return githubResponse, nil
}
// Used to retrieve the authenticated user’s Gists.
// This function also takes a token as a parameter.
func (a *App) GetGistsForAuthenticatedUser(token string) (APIResponse, error) {
url := fmt.Sprintf("%s/gists", BaseUrl)
response, err := MakeGetRequest(url, token)
if err != nil {
return nil, err
}
json.Unmarshal(response, &githubResponse)
return githubResponse, nil
}
// Used to get more information on a repository. This information could be the commit history,
// list of contributors, or list of users who have starred the repository. It takes two
// parameters, the url to be called and the authentication token. For public repositories,
// the token will be an empty string.
func (a *App) GetMoreInformationFromURL(url, token string) (APIResponse, error) {
response, err := MakeGetRequest(url, token)
if err != nil {
return nil, err
}
json.Unmarshal(response, &githubResponse)
return githubResponse, nil
}
// Used to get the content of a Gist. This function takes the URL for the Gist’s raw content
// and an authentication token (an empty string for public Gists). It returns a string
// corresponding to the content of the Gist.
func (a *App) GetGistContent(url, token string) (string, error) {
githubResponse, err := MakeGetRequest(url, token)
if err != nil {
return "", err
}
return string(githubResponse), nil
}
// Used to create a new Gist for the authenticated user. This function takes two parameters,
// the Gist to be created as well as the authentication token for the user.
func (a *App) CreateNewGist(gist Gist, token string) (interface{}, error) {
var githubResponse interface{}
requestBody, _ := json.Marshal(gist)
url := fmt.Sprintf("%s/gists", BaseUrl)
response, err := MakePostRequest(url, token, requestBody)
if err != nil {
return nil, err
}
json.Unmarshal(response, &githubResponse)
return githubResponse, nil
}