Skip to content

Commit

Permalink
Take context.Context in all functions (#153)
Browse files Browse the repository at this point in the history
Closes #143 #143.
  • Loading branch information
vegarsti authored Dec 22, 2023
1 parent 4d2cad0 commit 732a427
Show file tree
Hide file tree
Showing 51 changed files with 403 additions and 356 deletions.
64 changes: 32 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ You can also find some examples in [integration tests](https://github.com/typese
DefaultSortingField: pointer.String("num_employees"),
}

client.Collections().Create(schema)
client.Collections().Create(context.Background(), schema)
```

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

client.Collection("companies").Documents().Create(document)
client.Collection("companies").Documents().Create(context.Background(), document)
```

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

client.Collection("companies").Documents().Upsert(newDocument)
client.Collection("companies").Documents().Upsert(context.Background(), newDocument)
```

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

client.Collection("companies").Documents().Search(searchParameters)
client.Collection("companies").Documents().Search(context.Background(), searchParameters)
```

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

client.Collection("companies").Documents().Search(searchParameters)
client.Collection("companies").Documents().Search(context.Background(), searchParameters)
```

### Retrieve a document

```go
client.Collection("companies").Document("123").Retrieve()
client.Collection("companies").Document("123").Retrieve(context.Background())
```

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

client.Collection("companies").Document("123").Update(document)
client.Collection("companies").Document("123").Update(context.Background(), document)
```

### Delete an individual document

```go
client.Collection("companies").Document("123").Delete()
client.Collection("companies").Document("123").Delete(context.Background())
```

### Delete a bunch of documents

```go
filter := &api.DeleteDocumentsParams{FilterBy: "num_employees:>100", BatchSize: 100}
client.Collection("companies").Documents().Delete(filter)
client.Collection("companies").Documents().Delete(context.Background(), filter)
```

### Retrieve a collection

```go
client.Collection("companies").Retrieve()
client.Collection("companies").Retrieve(context.Background())
```

### Export documents from a collection

```go
client.Collection("companies").Documents().Export()
client.Collection("companies").Documents().Export(context.Background())
```

### Import documents into a collection
Expand Down Expand Up @@ -203,7 +203,7 @@ Import an array of documents:
BatchSize: pointer.Int(40),
}

client.Collection("companies").Documents().Import(documents, params)
client.Collection("companies").Documents().Import(context.Background(), documents, params)
```

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

client.Collection("companies").Documents().ImportJsonl(importBody, params)
client.Collection("companies").Documents().ImportJsonl(context.Background(), importBody, params)
```

### List all collections

```go
client.Collections().Retrieve()
client.Collections().Retrieve(context.Background())
```

### Drop a collection

```go
client.Collection("companies").Delete()
client.Collection("companies").Delete(context.Background())
```

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

client.Keys().Create(keySchema)
client.Keys().Create(context.Background(), keySchema)
```

### Retrieve an API Key

```go
client.Key(1).Retrieve()
client.Key(1).Retrieve(context.Background())
```

### List all keys

```go
client.Keys().Retrieve()
client.Keys().Retrieve(context.Background())
```

### Delete API Key

```go
client.Key(1).Delete()
client.Key(1).Delete(context.Background())
```

### Create or update an override
Expand Down Expand Up @@ -287,19 +287,19 @@ client.Key(1).Delete()
},
}

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

### List all overrides

```go
client.Collection("companies").Overrides().Retrieve()
client.Collection("companies").Overrides().Retrieve(context.Background())
```

### Delete an override

```go
client.Collection("companies").Override("customize-apple").Delete()
client.Collection("companies").Override("customize-apple").Delete(context.Background())
```

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

```go
client.Alias("companies").Retrieve()
client.Alias("companies").Retrieve(context.Background())
```

### List all aliases

```go
client.Aliases().Retrieve()
client.Aliases().Retrieve(context.Background())
```

### Delete an alias

```go
client.Alias("companies").Delete()
client.Alias("companies").Delete(context.Background())
```

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

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

### Retrieve a synonym

```go
client.Collection("products").Synonym("coat-synonyms").Retrieve()
client.Collection("products").Synonym("coat-synonyms").Retrieve(context.Background())
```

### List all synonyms

```go
client.Collection("products").Synonyms().Retrieve()
client.Collection("products").Synonyms().Retrieve(context.Background())
```

### Delete a synonym

```go
client.Collection("products").Synonym("coat-synonyms").Delete()
client.Collection("products").Synonym("coat-synonyms").Delete(context.Background())
```

### Create snapshot (for backups)

```go
client.Operations().Snapshot("/tmp/typesense-data-snapshot")
client.Operations().Snapshot(context.Background(), "/tmp/typesense-data-snapshot")
```

### Re-elect Leader

```go
client.Operations().Vote()
client.Operations().Vote(context.Background())
```

## Contributing
Expand Down
12 changes: 6 additions & 6 deletions typesense/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import (

// AliasInterface is a type for Alias API operations
type AliasInterface interface {
Retrieve() (*api.CollectionAlias, error)
Delete() (*api.CollectionAlias, error)
Retrieve(ctx context.Context) (*api.CollectionAlias, error)
Delete(ctx context.Context) (*api.CollectionAlias, error)
}

type alias struct {
apiClient APIClientInterface
name string
}

func (a *alias) Retrieve() (*api.CollectionAlias, error) {
response, err := a.apiClient.GetAliasWithResponse(context.Background(), a.name)
func (a *alias) Retrieve(ctx context.Context) (*api.CollectionAlias, error) {
response, err := a.apiClient.GetAliasWithResponse(ctx, a.name)
if err != nil {
return nil, err
}
Expand All @@ -28,8 +28,8 @@ func (a *alias) Retrieve() (*api.CollectionAlias, error) {
return response.JSON200, nil
}

func (a *alias) Delete() (*api.CollectionAlias, error) {
response, err := a.apiClient.DeleteAliasWithResponse(context.Background(), a.name)
func (a *alias) Delete(ctx context.Context) (*api.CollectionAlias, error) {
response, err := a.apiClient.DeleteAliasWithResponse(ctx, a.name)
if err != nil {
return nil, err
}
Expand Down
13 changes: 7 additions & 6 deletions typesense/alias_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package typesense

import (
"context"
"errors"
"net/http"
"testing"
Expand All @@ -27,7 +28,7 @@ func TestCollectionAliasRetrieve(t *testing.T) {
Times(1)

client := NewClient(WithAPIClient(mockAPIClient))
result, err := client.Alias("collection_alias").Retrieve()
result, err := client.Alias("collection_alias").Retrieve(context.Background())

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

client := NewClient(WithAPIClient(mockAPIClient))
_, err := client.Alias("collection_alias").Retrieve()
_, err := client.Alias("collection_alias").Retrieve(context.Background())
assert.NotNil(t, err)
}

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

client := NewClient(WithAPIClient(mockAPIClient))
_, err := client.Alias("collection_alias").Retrieve()
_, err := client.Alias("collection_alias").Retrieve(context.Background())
assert.NotNil(t, err)
}

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

client := NewClient(WithAPIClient(mockAPIClient))
result, err := client.Alias("collection_alias").Delete()
result, err := client.Alias("collection_alias").Delete(context.Background())

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

client := NewClient(WithAPIClient(mockAPIClient))
_, err := client.Alias("collection_alias").Delete()
_, err := client.Alias("collection_alias").Delete(context.Background())
assert.NotNil(t, err)
}

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

client := NewClient(WithAPIClient(mockAPIClient))
_, err := client.Alias("collection_alias").Delete()
_, err := client.Alias("collection_alias").Delete(context.Background())
assert.NotNil(t, err)
}
12 changes: 6 additions & 6 deletions typesense/aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import (

// AliasesInterface is a type for Aliases API operations
type AliasesInterface interface {
Upsert(aliasName string, aliasSchema *api.CollectionAliasSchema) (*api.CollectionAlias, error)
Retrieve() ([]*api.CollectionAlias, error)
Upsert(ctx context.Context, aliasName string, aliasSchema *api.CollectionAliasSchema) (*api.CollectionAlias, error)
Retrieve(ctx context.Context) ([]*api.CollectionAlias, error)
}

// aliases is internal implementation of AliasesInterface
type aliases struct {
apiClient APIClientInterface
}

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

func (a *aliases) Retrieve() ([]*api.CollectionAlias, error) {
response, err := a.apiClient.GetAliasesWithResponse(context.Background())
func (a *aliases) Retrieve(ctx context.Context) ([]*api.CollectionAlias, error) {
response, err := a.apiClient.GetAliasesWithResponse(ctx)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 732a427

Please sign in to comment.