-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwwdc-api.go
103 lines (71 loc) · 1.77 KB
/
wwdc-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
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
const URL_ASCII_WWDC_API = "http://asciiwwdc.com"
type SessionResponse struct {
Title string `json:"title"`
Description string `json:"description"`
Year float64 `json:"year"`
Number float64 `json:"number"`
}
type SearchResponse struct {
Results []ResultsData `json:"results"`
}
type ResultsData struct {
Title string `json:"title"`
Description string `json:"description"`
Year float64 `json:"year"`
Number float64 `json:"number"`
}
func getSession(sessionID string, year string) (*SessionResponse, error) {
url := fmt.Sprintf(URL_ASCII_WWDC_API+"/%s/sessions/%s", year, sessionID)
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
req.Header.Add("Accept", `application/json`)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
defer resp.Body.Close()
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var result SessionResponse
err = json.Unmarshal(body, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func search(query string) (*SearchResponse, error) {
url := fmt.Sprintf(URL_ASCII_WWDC_API+"/search?q=%s", query)
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
req.Header.Add("Accept", `application/json`)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
defer resp.Body.Close()
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var result SearchResponse
err = json.Unmarshal(body, &result)
if err != nil {
return nil, err
}
return &result, nil
}