Skip to content

Commit

Permalink
Add support for saving URLs and deleting articles
Browse files Browse the repository at this point in the history
  • Loading branch information
rubiojr committed Mar 25, 2024
1 parent 392520e commit d2ef8cc
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 9 deletions.
32 changes: 24 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
32 changes: 32 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"time"

"github.com/google/uuid"
"github.com/rubiojr/omnivore-go/queries"
"github.com/shurcooL/graphql"
)
Expand Down Expand Up @@ -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
}
15 changes: 15 additions & 0 deletions api_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package omnivore_test

import (
"fmt"
"os"
"testing"
"time"

"github.com/rubiojr/omnivore-go"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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")
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
22 changes: 22 additions & 0 deletions queries/deletearticle.go
Original file line number Diff line number Diff line change
@@ -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"`
}
21 changes: 21 additions & 0 deletions queries/saveurl.go
Original file line number Diff line number Diff line change
@@ -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"`
}
3 changes: 2 additions & 1 deletion queries/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ var Search struct {
WordsCount int
FeedContent string
Folder string
Labels []struct {

Labels []struct {
Name string
Color string
CreatedAt time.Time
Expand Down

0 comments on commit d2ef8cc

Please sign in to comment.