Skip to content

Commit 732a427

Browse files
authored
Take context.Context in all functions (#153)
Closes #143 #143.
1 parent 4d2cad0 commit 732a427

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+403
-356
lines changed

README.md

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ You can also find some examples in [integration tests](https://github.com/typese
6868
DefaultSortingField: pointer.String("num_employees"),
6969
}
7070

71-
client.Collections().Create(schema)
71+
client.Collections().Create(context.Background(), schema)
7272
```
7373

7474
### Index a document
@@ -86,7 +86,7 @@ You can also find some examples in [integration tests](https://github.com/typese
8686
Country: "USA",
8787
}
8888

89-
client.Collection("companies").Documents().Create(document)
89+
client.Collection("companies").Documents().Create(context.Background(), document)
9090
```
9191

9292
### Upserting a document
@@ -104,7 +104,7 @@ You can also find some examples in [integration tests](https://github.com/typese
104104
Country: "USA",
105105
}
106106

107-
client.Collection("companies").Documents().Upsert(newDocument)
107+
client.Collection("companies").Documents().Upsert(context.Background(), newDocument)
108108
```
109109

110110
### Search a collection
@@ -117,7 +117,7 @@ You can also find some examples in [integration tests](https://github.com/typese
117117
SortBy: &([]string{"num_employees:desc"}),
118118
}
119119

120-
client.Collection("companies").Documents().Search(searchParameters)
120+
client.Collection("companies").Documents().Search(context.Background(), searchParameters)
121121
```
122122

123123
for the supporting multiple `QueryBy` params, you can add `,` after each field
@@ -130,13 +130,13 @@ for the supporting multiple `QueryBy` params, you can add `,` after each field
130130
SortBy: &([]string{"num_employees:desc"}),
131131
}
132132

133-
client.Collection("companies").Documents().Search(searchParameters)
133+
client.Collection("companies").Documents().Search(context.Background(), searchParameters)
134134
```
135135

136136
### Retrieve a document
137137

138138
```go
139-
client.Collection("companies").Document("123").Retrieve()
139+
client.Collection("companies").Document("123").Retrieve(context.Background())
140140
```
141141

142142
### Update a document
@@ -150,32 +150,32 @@ client.Collection("companies").Document("123").Retrieve()
150150
NumEmployees: 5500,
151151
}
152152

153-
client.Collection("companies").Document("123").Update(document)
153+
client.Collection("companies").Document("123").Update(context.Background(), document)
154154
```
155155

156156
### Delete an individual document
157157

158158
```go
159-
client.Collection("companies").Document("123").Delete()
159+
client.Collection("companies").Document("123").Delete(context.Background())
160160
```
161161

162162
### Delete a bunch of documents
163163

164164
```go
165165
filter := &api.DeleteDocumentsParams{FilterBy: "num_employees:>100", BatchSize: 100}
166-
client.Collection("companies").Documents().Delete(filter)
166+
client.Collection("companies").Documents().Delete(context.Background(), filter)
167167
```
168168

169169
### Retrieve a collection
170170

171171
```go
172-
client.Collection("companies").Retrieve()
172+
client.Collection("companies").Retrieve(context.Background())
173173
```
174174

175175
### Export documents from a collection
176176

177177
```go
178-
client.Collection("companies").Documents().Export()
178+
client.Collection("companies").Documents().Export(context.Background())
179179
```
180180

181181
### Import documents into a collection
@@ -203,7 +203,7 @@ Import an array of documents:
203203
BatchSize: pointer.Int(40),
204204
}
205205

206-
client.Collection("companies").Documents().Import(documents, params)
206+
client.Collection("companies").Documents().Import(context.Background(), documents, params)
207207
```
208208

209209
Import a JSONL file:
@@ -216,19 +216,19 @@ Import a JSONL file:
216216
importBody, err := os.Open("documents.jsonl")
217217
// defer close, error handling ...
218218

219-
client.Collection("companies").Documents().ImportJsonl(importBody, params)
219+
client.Collection("companies").Documents().ImportJsonl(context.Background(), importBody, params)
220220
```
221221

222222
### List all collections
223223

224224
```go
225-
client.Collections().Retrieve()
225+
client.Collections().Retrieve(context.Background())
226226
```
227227

228228
### Drop a collection
229229

230230
```go
231-
client.Collection("companies").Delete()
231+
client.Collection("companies").Delete(context.Background())
232232
```
233233

234234
### Create an API Key
@@ -241,25 +241,25 @@ client.Collection("companies").Delete()
241241
ExpiresAt: time.Now().AddDate(0, 6, 0).Unix(),
242242
}
243243

244-
client.Keys().Create(keySchema)
244+
client.Keys().Create(context.Background(), keySchema)
245245
```
246246

247247
### Retrieve an API Key
248248

249249
```go
250-
client.Key(1).Retrieve()
250+
client.Key(1).Retrieve(context.Background())
251251
```
252252

253253
### List all keys
254254

255255
```go
256-
client.Keys().Retrieve()
256+
client.Keys().Retrieve(context.Background())
257257
```
258258

259259
### Delete API Key
260260

261261
```go
262-
client.Key(1).Delete()
262+
client.Key(1).Delete(context.Background())
263263
```
264264

265265
### Create or update an override
@@ -287,19 +287,19 @@ client.Key(1).Delete()
287287
},
288288
}
289289

290-
client.Collection("companies").Overrides().Upsert("customize-apple", override)
290+
client.Collection("companies").Overrides().Upsert(context.Background(), "customize-apple", override)
291291
```
292292

293293
### List all overrides
294294

295295
```go
296-
client.Collection("companies").Overrides().Retrieve()
296+
client.Collection("companies").Overrides().Retrieve(context.Background())
297297
```
298298

299299
### Delete an override
300300

301301
```go
302-
client.Collection("companies").Override("customize-apple").Delete()
302+
client.Collection("companies").Override("customize-apple").Delete(context.Background())
303303
```
304304

305305
### Create or Update an alias
@@ -312,19 +312,19 @@ client.Collection("companies").Override("customize-apple").Delete()
312312
### Retrieve an alias
313313

314314
```go
315-
client.Alias("companies").Retrieve()
315+
client.Alias("companies").Retrieve(context.Background())
316316
```
317317

318318
### List all aliases
319319

320320
```go
321-
client.Aliases().Retrieve()
321+
client.Aliases().Retrieve(context.Background())
322322
```
323323

324324
### Delete an alias
325325

326326
```go
327-
client.Alias("companies").Delete()
327+
client.Alias("companies").Delete(context.Background())
328328
```
329329

330330
### Create or update a multi-way synonym
@@ -333,7 +333,7 @@ client.Alias("companies").Delete()
333333
synonym := &api.SearchSynonymSchema{
334334
Synonyms: []string{"blazer", "coat", "jacket"},
335335
}
336-
client.Collection("products").Synonyms().Upsert("coat-synonyms", synonym)
336+
client.Collection("products").Synonyms().Upsert(context.Background(), "coat-synonyms", synonym)
337337
```
338338

339339
### Create or update a one-way synonym
@@ -343,37 +343,37 @@ client.Alias("companies").Delete()
343343
Root: "blazer",
344344
Synonyms: []string{"blazer", "coat", "jacket"},
345345
}
346-
client.Collection("products").Synonyms().Upsert("coat-synonyms", synonym)
346+
client.Collection("products").Synonyms().Upsert(context.Background(), "coat-synonyms", synonym)
347347
```
348348

349349
### Retrieve a synonym
350350

351351
```go
352-
client.Collection("products").Synonym("coat-synonyms").Retrieve()
352+
client.Collection("products").Synonym("coat-synonyms").Retrieve(context.Background())
353353
```
354354

355355
### List all synonyms
356356

357357
```go
358-
client.Collection("products").Synonyms().Retrieve()
358+
client.Collection("products").Synonyms().Retrieve(context.Background())
359359
```
360360

361361
### Delete a synonym
362362

363363
```go
364-
client.Collection("products").Synonym("coat-synonyms").Delete()
364+
client.Collection("products").Synonym("coat-synonyms").Delete(context.Background())
365365
```
366366

367367
### Create snapshot (for backups)
368368

369369
```go
370-
client.Operations().Snapshot("/tmp/typesense-data-snapshot")
370+
client.Operations().Snapshot(context.Background(), "/tmp/typesense-data-snapshot")
371371
```
372372

373373
### Re-elect Leader
374374

375375
```go
376-
client.Operations().Vote()
376+
client.Operations().Vote(context.Background())
377377
```
378378

379379
## Contributing

typesense/alias.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ import (
88

99
// AliasInterface is a type for Alias API operations
1010
type AliasInterface interface {
11-
Retrieve() (*api.CollectionAlias, error)
12-
Delete() (*api.CollectionAlias, error)
11+
Retrieve(ctx context.Context) (*api.CollectionAlias, error)
12+
Delete(ctx context.Context) (*api.CollectionAlias, error)
1313
}
1414

1515
type alias struct {
1616
apiClient APIClientInterface
1717
name string
1818
}
1919

20-
func (a *alias) Retrieve() (*api.CollectionAlias, error) {
21-
response, err := a.apiClient.GetAliasWithResponse(context.Background(), a.name)
20+
func (a *alias) Retrieve(ctx context.Context) (*api.CollectionAlias, error) {
21+
response, err := a.apiClient.GetAliasWithResponse(ctx, a.name)
2222
if err != nil {
2323
return nil, err
2424
}
@@ -28,8 +28,8 @@ func (a *alias) Retrieve() (*api.CollectionAlias, error) {
2828
return response.JSON200, nil
2929
}
3030

31-
func (a *alias) Delete() (*api.CollectionAlias, error) {
32-
response, err := a.apiClient.DeleteAliasWithResponse(context.Background(), a.name)
31+
func (a *alias) Delete(ctx context.Context) (*api.CollectionAlias, error) {
32+
response, err := a.apiClient.DeleteAliasWithResponse(ctx, a.name)
3333
if err != nil {
3434
return nil, err
3535
}

typesense/alias_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package typesense
22

33
import (
4+
"context"
45
"errors"
56
"net/http"
67
"testing"
@@ -27,7 +28,7 @@ func TestCollectionAliasRetrieve(t *testing.T) {
2728
Times(1)
2829

2930
client := NewClient(WithAPIClient(mockAPIClient))
30-
result, err := client.Alias("collection_alias").Retrieve()
31+
result, err := client.Alias("collection_alias").Retrieve(context.Background())
3132

3233
assert.Nil(t, err)
3334
assert.Equal(t, expectedResult, result)
@@ -44,7 +45,7 @@ func TestCollectionAliasRetrieveOnApiClientErrorReturnsError(t *testing.T) {
4445
Times(1)
4546

4647
client := NewClient(WithAPIClient(mockAPIClient))
47-
_, err := client.Alias("collection_alias").Retrieve()
48+
_, err := client.Alias("collection_alias").Retrieve(context.Background())
4849
assert.NotNil(t, err)
4950
}
5051

@@ -64,7 +65,7 @@ func TestCollectionAliasRetrieveOnHttpStatusErrorCodeReturnsError(t *testing.T)
6465
Times(1)
6566

6667
client := NewClient(WithAPIClient(mockAPIClient))
67-
_, err := client.Alias("collection_alias").Retrieve()
68+
_, err := client.Alias("collection_alias").Retrieve(context.Background())
6869
assert.NotNil(t, err)
6970
}
7071

@@ -84,7 +85,7 @@ func TestCollectionAliasDelete(t *testing.T) {
8485
Times(1)
8586

8687
client := NewClient(WithAPIClient(mockAPIClient))
87-
result, err := client.Alias("collection_alias").Delete()
88+
result, err := client.Alias("collection_alias").Delete(context.Background())
8889

8990
assert.Nil(t, err)
9091
assert.Equal(t, expectedResult, result)
@@ -101,7 +102,7 @@ func TestCollectionAliasDeleteOnApiClientErrorReturnsError(t *testing.T) {
101102
Times(1)
102103

103104
client := NewClient(WithAPIClient(mockAPIClient))
104-
_, err := client.Alias("collection_alias").Delete()
105+
_, err := client.Alias("collection_alias").Delete(context.Background())
105106
assert.NotNil(t, err)
106107
}
107108

@@ -121,6 +122,6 @@ func TestCollectionAliasDeleteOnHttpStatusErrorCodeReturnsError(t *testing.T) {
121122
Times(1)
122123

123124
client := NewClient(WithAPIClient(mockAPIClient))
124-
_, err := client.Alias("collection_alias").Delete()
125+
_, err := client.Alias("collection_alias").Delete(context.Background())
125126
assert.NotNil(t, err)
126127
}

typesense/aliases.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ import (
88

99
// AliasesInterface is a type for Aliases API operations
1010
type AliasesInterface interface {
11-
Upsert(aliasName string, aliasSchema *api.CollectionAliasSchema) (*api.CollectionAlias, error)
12-
Retrieve() ([]*api.CollectionAlias, error)
11+
Upsert(ctx context.Context, aliasName string, aliasSchema *api.CollectionAliasSchema) (*api.CollectionAlias, error)
12+
Retrieve(ctx context.Context) ([]*api.CollectionAlias, error)
1313
}
1414

1515
// aliases is internal implementation of AliasesInterface
1616
type aliases struct {
1717
apiClient APIClientInterface
1818
}
1919

20-
func (a *aliases) Upsert(aliasName string, aliasSchema *api.CollectionAliasSchema) (*api.CollectionAlias, error) {
21-
response, err := a.apiClient.UpsertAliasWithResponse(context.Background(),
20+
func (a *aliases) Upsert(ctx context.Context, aliasName string, aliasSchema *api.CollectionAliasSchema) (*api.CollectionAlias, error) {
21+
response, err := a.apiClient.UpsertAliasWithResponse(ctx,
2222
aliasName, api.UpsertAliasJSONRequestBody(*aliasSchema))
2323
if err != nil {
2424
return nil, err
@@ -29,8 +29,8 @@ func (a *aliases) Upsert(aliasName string, aliasSchema *api.CollectionAliasSchem
2929
return response.JSON200, nil
3030
}
3131

32-
func (a *aliases) Retrieve() ([]*api.CollectionAlias, error) {
33-
response, err := a.apiClient.GetAliasesWithResponse(context.Background())
32+
func (a *aliases) Retrieve(ctx context.Context) ([]*api.CollectionAlias, error) {
33+
response, err := a.apiClient.GetAliasesWithResponse(ctx)
3434
if err != nil {
3535
return nil, err
3636
}

0 commit comments

Comments
 (0)