-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathclient.go
95 lines (79 loc) · 1.75 KB
/
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
// Copyright 2016 Muhammad Shulhan <[email protected]>. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file.
package gonduit
import (
"crypto/tls"
"encoding/json"
"net/http"
)
//
// Client contains all information for HTTP client to connect to conduti server,
// including reusable request and response object.
//
type Client struct {
http *http.Client
url string
token string
request *Request
respon *Response
}
//
// Cursor contains the value to limit search or information returned by search.
//
type Cursor struct {
Limit int `json:"limit"`
After string `json:"after"`
Before string `json:"before"`
Order string `json:"order"`
}
//
// SearchResult contain generic result from querying to server.
//
type SearchResult struct {
Data json.RawMessage `json:"data"`
Maps json.RawMessage `json:"maps"`
Query json.RawMessage `json:"query"`
Cursor Cursor `json:"cursor"`
}
//
// NewClient will create new HTTP client for connecting to conduit API server at
// `URL` using user `token`.
//
func NewClient(URL, token string, disableTls bool) *Client {
cl := &Client{
http: nil,
url: URL,
token: token,
request: nil,
respon: nil,
}
tr := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: disableTls,
},
}
cl.http = &http.Client{
Transport: tr,
}
return cl
}
//
// Post will sent gonduit Request and parse their response.
//
func (cl *Client) Post() (e error) {
var httpResp *http.Response
httpResp, e = cl.request.Post(cl)
if e != nil {
return e
}
e = cl.NewResponse(httpResp)
return e
}
//
// Ping check if server is up.
//
func (cl *Client) Ping() error {
cl.NewRequest(APIPing)
return cl.Post()
}