diff --git a/README.md b/README.md index 4187429..2fc4baa 100644 --- a/README.md +++ b/README.md @@ -6,20 +6,36 @@ This is a Go client library for the [Omnivore GraphQL API](https://github.com/om ## Work in Progress -Supported queries: +Supported read queries: -- [x] Search -- [x] NewsletterEmails - [x] ApiKeys -- [ ] Users -- [x] Labels -- [ ] Integrations -- [ ] Webhooks - [ ] Feeds -- [ ] Groups - [ ] Filters +- [ ] Groups +- [ ] Integrations +- [x] Labels +- [x] NewsletterEmails - [ ] Rules +- [x] Search - [x] Subscriptions +- [ ] Users +- [ ] Webhooks + +Supported mutation queries: + +- [ ] AddPopularRead +- [ ] BulkAction +- [ ] CreateArticle +- [ ] CreateGroup +- [ ] DeleteAccount +- [x] DeleteArticle (SetBookmarkArticle) +- [ ] DeleteFilter +- [ ] DeleteIntegration +- [ ] DeleteLabel +- [ ] DeleteRule +- [ ] DeleteWebhook +- [x] SaveUrl +- [ ] EmptyTrash ## Usage diff --git a/api.go b/api.go index b8402c4..afbd985 100644 --- a/api.go +++ b/api.go @@ -5,6 +5,7 @@ import ( "net/http" "time" + "github.com/google/uuid" "github.com/rubiojr/omnivore-go/queries" "github.com/shurcooL/graphql" ) @@ -327,3 +328,34 @@ func (c *Omnivore) Labels() ([]*Label, error) { func (c *SearchItem) IsUnread() bool { return c.ReadAt.IsZero() } + +func (c *Omnivore) SaveUrl(url string) error { + input := queries.SaveUrlInput{ + Url: graphql.String(url), + ClientRequestId: graphql.ID(uuid.New().String()), + Source: graphql.String("api"), + } + + variables := map[string]any{ + "input": input, + } + + return c.graphql.Mutate(context.Background(), &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 { + input := queries.SetBookmarkArticleInput{ + ArticleId: graphql.ID(id), + Bookmark: graphql.Boolean(false), + } + + variables := map[string]any{ + "input": input, + } + + err := c.graphql.Mutate(context.Background(), &queries.DeleteArticle, variables) + return err +} diff --git a/api_test.go b/api_test.go index e90a8c6..b5c242f 100644 --- a/api_test.go +++ b/api_test.go @@ -1,8 +1,10 @@ package omnivore_test import ( + "fmt" "os" "testing" + "time" "github.com/rubiojr/omnivore-go" "github.com/stretchr/testify/assert" @@ -50,3 +52,16 @@ func TestLabels(t *testing.T) { assert.Equal(t, labels[0].Color, "#F26522") assert.Equal(t, labels[0].Description, "") } + +func TestAddUrl(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) + assert.NoError(t, err, "Failed to save article") + articles, err := client.Search(omnivore.SearchOpts{Query: fmt.Sprintf(`title:"%s"`, "Leet")}) + assert.Equal(t, len(articles), 1) + // Wait a bit, deletes right after saving are ignored otherwise + time.Sleep(5 * time.Second) + err = client.DeleteArticle(articles[0].ID) + assert.NoError(t, err, "Failed to delete article") +} diff --git a/go.mod b/go.mod index 639b2b5..cd7cbb1 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/rubiojr/omnivore-go go 1.22.1 require ( + github.com/google/uuid v1.6.0 github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466 github.com/stretchr/testify v1.9.0 ) diff --git a/go.sum b/go.sum index dce5396..e66a6d2 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466 h1:17JxqqJY66GmZVHkmAsGEkcIu0oCe3AM420QDgGwZx0= diff --git a/queries/deletearticle.go b/queries/deletearticle.go new file mode 100644 index 0000000..2a5df18 --- /dev/null +++ b/queries/deletearticle.go @@ -0,0 +1,22 @@ +package queries + +import "github.com/shurcooL/graphql" + +var DeleteArticle struct { + SetBookmarkArticle struct { + SetBookmarkArticleSuccess struct { + BookmarkedArticle struct { + ID graphql.ID + LinkId graphql.ID + } + } `graphql:"... on SetBookmarkArticleSuccess"` + SetBookmarkArticleError struct { + ErrorCodes string + } `graphql:"... on SetBookmarkArticleError"` + } `graphql:"setBookmarkArticle(input: $input)"` +} + +type SetBookmarkArticleInput struct { + ArticleId graphql.ID `json:"articleID"` + Bookmark graphql.Boolean `json:"bookmark"` +} diff --git a/queries/saveurl.go b/queries/saveurl.go new file mode 100644 index 0000000..4ac665f --- /dev/null +++ b/queries/saveurl.go @@ -0,0 +1,21 @@ +package queries + +import "github.com/shurcooL/graphql" + +var SaveUrl struct { + SaveUrl struct { + SaveSuccess struct { + ClientRequestID graphql.ID + Url string + } `graphql:"... on SaveSuccess"` + SaveError struct { + ErrorCodes string + } `graphql:"... on SaveError"` + } `graphql:"saveUrl(input: $input)"` +} + +type SaveUrlInput struct { + Url graphql.String `json:"url"` + ClientRequestId graphql.ID `json:"clientRequestId"` + Source graphql.String `json:"source"` +} diff --git a/queries/search.go b/queries/search.go index 6d7f23d..2deef00 100644 --- a/queries/search.go +++ b/queries/search.go @@ -28,7 +28,8 @@ var Search struct { WordsCount int FeedContent string Folder string - Labels []struct { + + Labels []struct { Name string Color string CreatedAt time.Time