|
8 | 8 | "net/http" |
9 | 9 | "net/url" |
10 | 10 | "path" |
| 11 | + "sync" |
11 | 12 | ) |
12 | 13 |
|
13 | 14 | var ( |
@@ -44,16 +45,15 @@ func NewClient(rawURL, schema string, headers map[string]string) *Client { |
44 | 45 | } |
45 | 46 |
|
46 | 47 | // Set required headers |
47 | | - c.Transport.header.Set("Accept", "application/json") |
48 | | - c.Transport.header.Set("Content-Type", "application/json") |
49 | | - c.Transport.header.Set("Accept-Profile", schema) |
50 | | - c.Transport.header.Set("Content-Profile", schema) |
51 | | - c.Transport.header.Set("X-Client-Info", "postgrest-go/"+version) |
52 | | - |
| 48 | + c.Transport.SetHeaders(map[string]string{ |
| 49 | + "Accept": "application/json", |
| 50 | + "Content-Type": "application/json", |
| 51 | + "Accept-Profile": schema, |
| 52 | + "Content-Profile": schema, |
| 53 | + "X-Client-Info": "postgrest-go/" + version, |
| 54 | + }) |
53 | 55 | // Set optional headers if they exist |
54 | | - for key, value := range headers { |
55 | | - c.Transport.header.Set(key, value) |
56 | | - } |
| 56 | + c.Transport.SetHeaders(headers) |
57 | 57 |
|
58 | 58 | return &c |
59 | 59 | } |
@@ -84,20 +84,22 @@ func (c *Client) Ping() bool { |
84 | 84 |
|
85 | 85 | // SetApiKey sets api key header for subsequent requests. |
86 | 86 | func (c *Client) SetApiKey(apiKey string) *Client { |
87 | | - c.Transport.header.Set("apikey", apiKey) |
| 87 | + c.Transport.SetHeader("apikey", apiKey) |
88 | 88 | return c |
89 | 89 | } |
90 | 90 |
|
91 | 91 | // SetAuthToken sets authorization header for subsequent requests. |
92 | 92 | func (c *Client) SetAuthToken(authToken string) *Client { |
93 | | - c.Transport.header.Set("Authorization", "Bearer "+authToken) |
| 93 | + c.Transport.SetHeader("Authorization", "Bearer "+authToken) |
94 | 94 | return c |
95 | 95 | } |
96 | 96 |
|
97 | 97 | // ChangeSchema modifies the schema for subsequent requests. |
98 | 98 | func (c *Client) ChangeSchema(schema string) *Client { |
99 | | - c.Transport.header.Set("Accept-Profile", schema) |
100 | | - c.Transport.header.Set("Content-Profile", schema) |
| 99 | + c.Transport.SetHeaders(map[string]string{ |
| 100 | + "Accept-Profile": schema, |
| 101 | + "Content-Profile": schema, |
| 102 | + }) |
101 | 103 | return c |
102 | 104 | } |
103 | 105 |
|
@@ -156,17 +158,35 @@ func (c *Client) Rpc(name string, count string, rpcBody interface{}) string { |
156 | 158 | } |
157 | 159 |
|
158 | 160 | type transport struct { |
159 | | - header http.Header |
160 | 161 | baseURL url.URL |
161 | 162 | Parent http.RoundTripper |
| 163 | + |
| 164 | + mu sync.RWMutex |
| 165 | + header http.Header |
| 166 | +} |
| 167 | + |
| 168 | +func (t *transport) SetHeader(key, value string) { |
| 169 | + t.mu.Lock() |
| 170 | + defer t.mu.Unlock() |
| 171 | + t.header.Set(key, value) |
| 172 | +} |
| 173 | + |
| 174 | +func (t *transport) SetHeaders(headers map[string]string) { |
| 175 | + t.mu.Lock() |
| 176 | + defer t.mu.Unlock() |
| 177 | + for key, value := range headers { |
| 178 | + t.header.Set(key, value) |
| 179 | + } |
162 | 180 | } |
163 | 181 |
|
164 | | -func (t transport) RoundTrip(req *http.Request) (*http.Response, error) { |
| 182 | +func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) { |
| 183 | + t.mu.RLock() |
165 | 184 | for headerName, values := range t.header { |
166 | 185 | for _, val := range values { |
167 | 186 | req.Header.Add(headerName, val) |
168 | 187 | } |
169 | 188 | } |
| 189 | + t.mu.RUnlock() |
170 | 190 |
|
171 | 191 | req.URL = t.baseURL.ResolveReference(req.URL) |
172 | 192 |
|
|
0 commit comments