-
Notifications
You must be signed in to change notification settings - Fork 27
/
client.go
179 lines (146 loc) · 3.89 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package postgrest
import (
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"path"
)
var (
version = "v0.1.1"
)
type Client struct {
ClientError error
session http.Client
Transport *transport
}
// NewClient constructs a new client given a URL to a Postgrest instance.
func NewClient(rawURL, schema string, headers map[string]string) *Client {
// Create URL from rawURL
baseURL, err := url.Parse(rawURL)
if err != nil {
return &Client{ClientError: err}
}
t := transport{
header: http.Header{},
baseURL: *baseURL,
Parent: nil,
}
c := Client{
session: http.Client{Transport: &t},
Transport: &t,
}
if schema == "" {
schema = "public"
}
// Set required headers
c.Transport.header.Set("Accept", "application/json")
c.Transport.header.Set("Content-Type", "application/json")
c.Transport.header.Set("Accept-Profile", schema)
c.Transport.header.Set("Content-Profile", schema)
c.Transport.header.Set("X-Client-Info", "postgrest-go/"+version)
// Set optional headers if they exist
for key, value := range headers {
c.Transport.header.Set(key, value)
}
return &c
}
func (c *Client) Ping() bool {
req, err := http.NewRequest("GET", path.Join(c.Transport.baseURL.Path, ""), nil)
if err != nil {
c.ClientError = err
return false
}
resp, err := c.session.Do(req)
if err != nil {
c.ClientError = err
return false
}
if resp.Status != "200 OK" {
c.ClientError = errors.New("ping failed")
return false
}
return true
}
// SetApiKey sets api key header for subsequent requests.
func (c *Client) SetApiKey(apiKey string) *Client {
c.Transport.header.Set("apikey", apiKey)
return c
}
// SetAuthToken sets authorization header for subsequent requests.
func (c *Client) SetAuthToken(authToken string) *Client {
c.Transport.header.Set("Authorization", "Bearer "+authToken)
return c
}
// ChangeSchema modifies the schema for subsequent requests.
func (c *Client) ChangeSchema(schema string) *Client {
c.Transport.header.Set("Accept-Profile", schema)
c.Transport.header.Set("Content-Profile", schema)
return c
}
// From sets the table to query from.
func (c *Client) From(table string) *QueryBuilder {
return &QueryBuilder{client: c, tableName: table, headers: map[string]string{}, params: map[string]string{}}
}
// Rpc executes a Postgres function (a.k.a., Remote Prodedure Call), given the
// function name and, optionally, a body, returning the result as a string.
func (c *Client) Rpc(name string, count string, rpcBody interface{}) string {
// Get body if it exists
var byteBody []byte = nil
if rpcBody != nil {
jsonBody, err := json.Marshal(rpcBody)
if err != nil {
c.ClientError = err
return ""
}
byteBody = jsonBody
}
readerBody := bytes.NewBuffer(byteBody)
url := path.Join(c.Transport.baseURL.Path, "rpc", name)
req, err := http.NewRequest("POST", url, readerBody)
if err != nil {
c.ClientError = err
return ""
}
if count != "" && (count == `exact` || count == `planned` || count == `estimated`) {
req.Header.Add("Prefer", "count="+count)
}
resp, err := c.session.Do(req)
if err != nil {
c.ClientError = err
return ""
}
body, err := io.ReadAll(resp.Body)
if err != nil {
c.ClientError = err
return ""
}
result := string(body)
err = resp.Body.Close()
if err != nil {
c.ClientError = err
return ""
}
return result
}
type transport struct {
header http.Header
baseURL url.URL
Parent http.RoundTripper
}
func (t transport) RoundTrip(req *http.Request) (*http.Response, error) {
for headerName, values := range t.header {
for _, val := range values {
req.Header.Add(headerName, val)
}
}
req.URL = t.baseURL.ResolveReference(req.URL)
// This is only needed with usage of httpmock in testing. It would be better to initialize
// t.Parent with http.DefaultTransport and then use t.Parent.RoundTrip(req)
if t.Parent != nil {
return t.Parent.RoundTrip(req)
}
return http.DefaultTransport.RoundTrip(req)
}