-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ore.go
106 lines (94 loc) · 2.34 KB
/
Ore.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package OreGo
import (
"time"
"encoding/json"
"bytes"
"github.com/valyala/fasthttp"
)
func New() (ore *Ore){
ore = NewWithClient(fasthttp.Client{ReadTimeout: time.Second * 10})
return
}
func NewWithClient(client fasthttp.Client) (ore *Ore) {
ore = &Ore{
Client:client,
}
return
}
func (ore *Ore) Request(url string) (body []byte) {
stat, body, err := ore.Client.Get(nil, url)
if err != nil || stat != 200 {
return
}
return
}
func (ore *Ore) GetProjects() (projects *[]Project, err error) {
body := ore.Request(projectsUrl)
projects = &[]Project{}
err = json.NewDecoder(bytes.NewReader(body)).Decode(projects)
if err != nil {
panic(err)
}
return
}
func (ore *Ore) GetProject(name string) (project *Project, err error) {
project = &Project{}
body := ore.Request(projectsUrl + name)
err = json.NewDecoder(bytes.NewReader(body)).Decode(project)
if err != nil {
panic(err)
}
return
}
func (ore *Ore) GetProjectVersions(name string) (versions *[]Version, err error) {
body := ore.Request(projectsUrl + name + "/versions")
versions = &[]Version{}
err = json.NewDecoder(bytes.NewReader(body)).Decode(versions)
if err != nil {
panic(err)
}
return
}
func (ore *Ore) GetProjectVersion(name string, ver string) (version *Version, err error) {
version = &Version{}
body := ore.Request(projectsUrl + name + "/versions/" + ver)
err = json.NewDecoder(bytes.NewReader(body)).Decode(version)
if err != nil {
panic(err)
}
return
}
func (ore *Ore) GetRecommendedDownload(name string) string {
project, err := ore.GetProject(name)
if err != nil {
return "An error occurred, could not obtain the url."
}
return project.RecommendedVersion.HRef + "/download"
}
func (ore *Ore) SearchProjects(query string) (projects *[]Project, err error) {
body := ore.Request(projectsUrl + "?q=" + query)
projects = &[]Project{}
err = json.NewDecoder(bytes.NewReader(body)).Decode(projects)
if err != nil {
panic(err)
}
return
}
func (ore *Ore) GetUsers() (users *[]User, err error) {
body := ore.Request(usersUrl)
users = &[]User{}
err = json.NewDecoder(bytes.NewReader(body)).Decode(users)
if err != nil {
panic(err)
}
return
}
func (ore *Ore) GetUser(username string) (user *User, err error) {
user = &User{}
body := ore.Request(usersUrl + username)
err = json.NewDecoder(bytes.NewReader(body)).Decode(user)
if err != nil {
panic(err)
}
return
}