Skip to content

Commit

Permalink
API functions take a context
Browse files Browse the repository at this point in the history
  • Loading branch information
rubiojr committed Mar 25, 2024
1 parent 09787e4 commit 3da9a28
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 29 deletions.
28 changes: 14 additions & 14 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func NewClientFor(url string, opts Opts) *Omnivore {
// Search searches for items in Omnivore.
//
// The search is paginated, and it will fetch all the results by default.
func (c *Omnivore) Search(opts SearchOpts) ([]SearchItem, error) {
func (c *Omnivore) Search(ctx context.Context, opts SearchOpts) ([]SearchItem, error) {
afterCursor := ""

a := []SearchItem{}
Expand All @@ -169,7 +169,7 @@ func (c *Omnivore) Search(opts SearchOpts) ([]SearchItem, error) {
"after": graphql.String(afterCursor),
}

err := c.graphql.Query(context.Background(), &queries.Search, variables)
err := c.graphql.Query(ctx, &queries.Search, variables)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -220,10 +220,10 @@ func (c *Omnivore) Search(opts SearchOpts) ([]SearchItem, error) {
}

// NewsletterEmails returns the newsletter emails of the user.
func (c *Omnivore) NewsletterEmails() ([]NewsletterEmail, error) {
func (c *Omnivore) NewsletterEmails(ctx context.Context) ([]NewsletterEmail, error) {
a := []NewsletterEmail{}

err := c.graphql.Query(context.Background(), &queries.NewsletterEmail, nil)
err := c.graphql.Query(ctx, &queries.NewsletterEmail, nil)
if err != nil {
return nil, err
}
Expand All @@ -243,10 +243,10 @@ func (c *Omnivore) NewsletterEmails() ([]NewsletterEmail, error) {
}

// Subscriptions returns the subscriptions of the user.
func (c *Omnivore) Subscriptions() ([]Subscription, error) {
func (c *Omnivore) Subscriptions(ctx context.Context) ([]Subscription, error) {
a := []Subscription{}

err := c.graphql.Query(context.Background(), &queries.Subscriptions, nil)
err := c.graphql.Query(ctx, &queries.Subscriptions, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -274,10 +274,10 @@ func (c *Omnivore) Subscriptions() ([]Subscription, error) {
}

// ApiKeys returns all the API keys in Omnivore.
func (c *Omnivore) ApiKeys() ([]ApiKey, error) {
func (c *Omnivore) ApiKeys(ctx context.Context) ([]ApiKey, error) {
a := []ApiKey{}

err := c.graphql.Query(context.Background(), &queries.ApiKeys, nil)
err := c.graphql.Query(ctx, &queries.ApiKeys, nil)
if err != nil {
return nil, err
}
Expand All @@ -304,10 +304,10 @@ func (a *ApiKey) HasExpiry() bool {
}

// Labels returns all the labels in Omnivore.
func (c *Omnivore) Labels() ([]*Label, error) {
func (c *Omnivore) Labels(ctx context.Context) ([]*Label, error) {
a := []*Label{}

err := c.graphql.Query(context.Background(), &queries.Labels, nil)
err := c.graphql.Query(ctx, &queries.Labels, nil)
if err != nil {
return nil, err
}
Expand All @@ -329,7 +329,7 @@ func (c *SearchItem) IsUnread() bool {
return c.ReadAt.IsZero()
}

func (c *Omnivore) SaveUrl(url string) error {
func (c *Omnivore) SaveUrl(ctx context.Context, url string) error {
input := queries.SaveUrlInput{
Url: graphql.String(url),
ClientRequestId: graphql.ID(uuid.New().String()),
Expand All @@ -340,13 +340,13 @@ func (c *Omnivore) SaveUrl(url string) error {
"input": input,
}

return c.graphql.Mutate(context.Background(), &queries.SaveUrl, variables)
return c.graphql.Mutate(ctx, &queries.SaveUrl, variables)
}

// DeleteArticle deletes an article from the library.
//
// The query name is weird, see https://github.com/omnivore-app/omnivore/issues/2380
func (c *Omnivore) DeleteArticle(id string) error {
func (c *Omnivore) DeleteArticle(ctx context.Context, id string) error {
input := queries.SetBookmarkArticleInput{
ArticleId: graphql.ID(id),
Bookmark: graphql.Boolean(false),
Expand All @@ -356,6 +356,6 @@ func (c *Omnivore) DeleteArticle(id string) error {
"input": input,
}

err := c.graphql.Mutate(context.Background(), &queries.DeleteArticle, variables)
err := c.graphql.Mutate(ctx, &queries.DeleteArticle, variables)
return err
}
28 changes: 16 additions & 12 deletions api_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package omnivore_test

import (
"context"
"fmt"
"os"
"testing"
Expand All @@ -11,48 +12,49 @@ import (

func TestSetup(t *testing.T) {
client := omnivore.NewClient(omnivore.Opts{Token: os.Getenv("OMNIVORE_API_TOKEN")})
articles, err := client.Search(omnivore.SearchOpts{Query: "in:all"})
articles, err := client.Search(context.Background(), omnivore.SearchOpts{Query: "in:all"})
assert.NoError(t, err, "Failed to search")
for _, a := range articles {
err = client.DeleteArticle(a.ID)
err = client.DeleteArticle(context.Background(), a.ID)
assert.NoError(t, err, "Failed to delete article")
}
}

func TestSaveUrl(t *testing.T) {
client := omnivore.NewClient(omnivore.Opts{Token: os.Getenv("OMNIVORE_API_TOKEN")})
wp := "https://en.wikipedia.org/wiki/Leet"
err := client.SaveUrl(wp)
err := client.SaveUrl(context.Background(), wp)
assert.NoError(t, err, "Failed to save article")
}

func TestSearch(t *testing.T) {
client := omnivore.NewClient(omnivore.Opts{Token: os.Getenv("OMNIVORE_API_TOKEN")})
// https://docs.omnivore.app/using/search.html
articles, err := client.Search(omnivore.SearchOpts{Query: "in:all -label:RSS"})
articles, err := client.Search(context.Background(), omnivore.SearchOpts{Query: "in:all -label:RSS"})
assert.NoError(t, err, "Failed to search")
assert.Equal(t, len(articles), 1)
assert.Equal(t, articles[0].Title, "https://en.wikipedia.org/wiki/Leet")

articles, err = client.Search(omnivore.SearchOpts{Query: "in:all label:RSS"})
articles, err = client.Search(context.Background(), omnivore.SearchOpts{Query: "in:all label:RSS"})
assert.NoError(t, err, "Failed to search")
assert.Equal(t, len(articles), 0)

articles, err = client.Search(omnivore.SearchOpts{Query: "title:Leet"})
articles, err = client.Search(context.Background(), omnivore.SearchOpts{Query: "title:Leet"})
assert.NoError(t, err, "Failed to search")
assert.Equal(t, len(articles), 1)
}

func TestSubcriptions(t *testing.T) {
client := omnivore.NewClient(omnivore.Opts{Token: os.Getenv("OMNIVORE_API_TOKEN")})
subscriptions, err := client.Subscriptions()
subscriptions, err := client.Subscriptions(context.Background())
assert.NoError(t, err, "Failed to get subscriptions")
assert.Equal(t, len(subscriptions), 1)
assert.Equal(t, subscriptions[0].Name, "Omnivore Blog")
}

func TestApiKeys(t *testing.T) {
client := omnivore.NewClient(omnivore.Opts{Token: os.Getenv("OMNIVORE_API_TOKEN")})
keys, err := client.ApiKeys()
keys, err := client.ApiKeys(context.Background())
assert.NoError(t, err, "Failed to get api keys")
assert.Equal(t, len(keys), 1)
assert.Equal(t, keys[0].Name, "omnivore-go-github")
Expand All @@ -62,7 +64,7 @@ func TestApiKeys(t *testing.T) {

func TestLabels(t *testing.T) {
client := omnivore.NewClient(omnivore.Opts{Token: os.Getenv("OMNIVORE_API_TOKEN")})
labels, err := client.Labels()
labels, err := client.Labels(context.Background())
assert.NoError(t, err, "Failed to get labels")
assert.Equal(t, len(labels), 1)
assert.Equal(t, labels[0].Name, "RSS")
Expand All @@ -72,11 +74,13 @@ func TestLabels(t *testing.T) {

func TestDeleteArticle(t *testing.T) {
client := omnivore.NewClient(omnivore.Opts{Token: os.Getenv("OMNIVORE_API_TOKEN")})
articles, err := client.Search(omnivore.SearchOpts{Query: fmt.Sprintf(`title:"%s"`, "https://en.wikipedia.org/wiki/Leet")})
articles, err := client.Search(context.Background(), omnivore.SearchOpts{Query: fmt.Sprintf(`title:"%s"`, "https://en.wikipedia.org/wiki/Leet")})
assert.NoError(t, err, "Failed to search")
assert.Equal(t, len(articles), 1)
// Wait a bit, deletes right after saving are ignored otherwise
err = client.DeleteArticle(articles[0].ID)
err = client.DeleteArticle(context.Background(), articles[0].ID)
assert.NoError(t, err, "Failed to delete article")
articles, err = client.Search(omnivore.SearchOpts{Query: fmt.Sprintf(`title:"%s"`, "https://en.wikipedia.org/wiki/Leet")})
articles, err = client.Search(context.Background(), omnivore.SearchOpts{Query: fmt.Sprintf(`title:"%s"`, "https://en.wikipedia.org/wiki/Leet")})
assert.NoError(t, err, "Failed to search")
assert.Equal(t, len(articles), 0)
}
3 changes: 2 additions & 1 deletion examples/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"fmt"
"log"
"os"
Expand All @@ -12,7 +13,7 @@ import (
func main() {
client := omnivore.NewClient(omnivore.Opts{Token: getAPIToken()})
// https://docs.omnivore.app/using/search.html
a, err := client.Search(omnivore.SearchOpts{Query: "in:all sort:saved"})
a, err := client.Search(context.Background(), omnivore.SearchOpts{Query: "in:all sort:saved"})
if err != nil {
log.Fatalf("Failed to search: %v", err)
}
Expand Down
3 changes: 2 additions & 1 deletion examples/newsletter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"fmt"
"log"
"os"
Expand All @@ -10,7 +11,7 @@ import (

func main() {
client := omnivore.NewClient(omnivore.Opts{Token: getAPIToken()})
emails, err := client.NewsletterEmails()
emails, err := client.NewsletterEmails(context.Background())
if err != nil {
log.Fatalf("Failed to search: %v", err)
}
Expand Down
3 changes: 2 additions & 1 deletion examples/subscriptions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"fmt"
"log"
"os"
Expand All @@ -10,7 +11,7 @@ import (

func main() {
client := omnivore.NewClient(omnivore.Opts{Token: getAPIToken()})
subs, err := client.Subscriptions()
subs, err := client.Subscriptions(context.Background())
if err != nil {
log.Fatalf("Failed to search: %v", err)
}
Expand Down

0 comments on commit 3da9a28

Please sign in to comment.