-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyasn_client.go
120 lines (99 loc) · 2.21 KB
/
yasn_client.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package yasn_client
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)
const (
DefaultBaseURL = "http://localhost:8000/api/v1/"
)
type Note struct {
Id int `json:"id"`
Title string `json:"title"`
Content string `json:"content"`
ContentHTML string `json:"content_html"`
Tags []*Tag `json:"tags"`
}
type Tag struct {
Id int `json:"id"`
Name string `json:"name"`
}
type NotesAPIClient interface {
GetNote(id int) (*Note, error)
AddNote(note *Note) (*Note, error)
// @todo GetAllNotes, EditNote, DeleteNote
}
type Client struct {
// HTTP client used to communicate YASN API
client *http.Client
// Base URL for API request
BaseURL *url.URL
}
func NewClient(httpClient *http.Client) *Client {
if httpClient == nil {
httpClient = http.DefaultClient
}
baseURL, _ := url.Parse(DefaultBaseURL)
return &Client{
client: http.DefaultClient,
BaseURL: baseURL,
}
}
func (c *Client) GetNote(id int) (*Note, error) {
ep := fmt.Sprintf("notes/%d", id)
resp, err := c.request("GET", ep, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
note := new(Note)
err = json.NewDecoder(resp.Body).Decode(note)
if err != nil {
return nil, err
}
return note, nil
}
func (c *Client) AddNote(note *Note) (*Note, error) {
resp, err := c.request("POST", "notes", note)
if err != nil {
return nil, err
}
defer resp.Body.Close()
newNote := new(Note)
err = json.NewDecoder(resp.Body).Decode(newNote)
if err != nil {
return nil, err
}
return newNote, nil
}
func (c *Client) request(method string, endpoint string, body interface{}) (*http.Response, error) {
ep, err := url.Parse(endpoint)
if err != nil {
return nil, err
}
u := c.BaseURL.ResolveReference(ep)
var buf io.ReadWriter
if body != nil {
buf = new(bytes.Buffer)
err = json.NewEncoder(buf).Encode(body)
if err != nil {
return nil, err
}
}
req, err := http.NewRequest(method, u.String(), buf)
if err != nil {
return nil, err
}
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
if s := resp.StatusCode; s < 200 || s > 299 {
// @todo returns error message if there's any
return nil, fmt.Errorf("Unexpected status code %d", s)
}
return resp, nil
}