|
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 NewClientWithError(rawURL, schema string, headers map[string]string) (*Clie |
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, nil |
59 | 59 | } |
@@ -96,20 +96,22 @@ func (c *Client) Ping() bool { |
96 | 96 |
|
97 | 97 | // SetApiKey sets api key header for subsequent requests. |
98 | 98 | func (c *Client) SetApiKey(apiKey string) *Client { |
99 | | - c.Transport.header.Set("apikey", apiKey) |
| 99 | + c.Transport.SetHeader("apikey", apiKey) |
100 | 100 | return c |
101 | 101 | } |
102 | 102 |
|
103 | 103 | // SetAuthToken sets authorization header for subsequent requests. |
104 | 104 | func (c *Client) SetAuthToken(authToken string) *Client { |
105 | | - c.Transport.header.Set("Authorization", "Bearer "+authToken) |
| 105 | + c.Transport.SetHeader("Authorization", "Bearer "+authToken) |
106 | 106 | return c |
107 | 107 | } |
108 | 108 |
|
109 | 109 | // ChangeSchema modifies the schema for subsequent requests. |
110 | 110 | func (c *Client) ChangeSchema(schema string) *Client { |
111 | | - c.Transport.header.Set("Accept-Profile", schema) |
112 | | - c.Transport.header.Set("Content-Profile", schema) |
| 111 | + c.Transport.SetHeaders(map[string]string{ |
| 112 | + "Accept-Profile": schema, |
| 113 | + "Content-Profile": schema, |
| 114 | + }) |
113 | 115 | return c |
114 | 116 | } |
115 | 117 |
|
@@ -174,17 +176,35 @@ func (c *Client) Rpc(name string, count string, rpcBody interface{}) string { |
174 | 176 | } |
175 | 177 |
|
176 | 178 | type transport struct { |
177 | | - header http.Header |
178 | 179 | baseURL url.URL |
179 | 180 | Parent http.RoundTripper |
| 181 | + |
| 182 | + mu sync.RWMutex |
| 183 | + header http.Header |
| 184 | +} |
| 185 | + |
| 186 | +func (t *transport) SetHeader(key, value string) { |
| 187 | + t.mu.Lock() |
| 188 | + defer t.mu.Unlock() |
| 189 | + t.header.Set(key, value) |
| 190 | +} |
| 191 | + |
| 192 | +func (t *transport) SetHeaders(headers map[string]string) { |
| 193 | + t.mu.Lock() |
| 194 | + defer t.mu.Unlock() |
| 195 | + for key, value := range headers { |
| 196 | + t.header.Set(key, value) |
| 197 | + } |
180 | 198 | } |
181 | 199 |
|
182 | | -func (t transport) RoundTrip(req *http.Request) (*http.Response, error) { |
| 200 | +func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) { |
| 201 | + t.mu.RLock() |
183 | 202 | for headerName, values := range t.header { |
184 | 203 | for _, val := range values { |
185 | 204 | req.Header.Add(headerName, val) |
186 | 205 | } |
187 | 206 | } |
| 207 | + t.mu.RUnlock() |
188 | 208 |
|
189 | 209 | req.URL = t.baseURL.ResolveReference(req.URL) |
190 | 210 |
|
|
0 commit comments