Skip to content

Commit d2ef8cc

Browse files
committed
Add support for saving URLs and deleting articles
1 parent 392520e commit d2ef8cc

File tree

8 files changed

+119
-9
lines changed

8 files changed

+119
-9
lines changed

README.md

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,36 @@ This is a Go client library for the [Omnivore GraphQL API](https://github.com/om
66

77
## Work in Progress
88

9-
Supported queries:
9+
Supported read queries:
1010

11-
- [x] Search
12-
- [x] NewsletterEmails
1311
- [x] ApiKeys
14-
- [ ] Users
15-
- [x] Labels
16-
- [ ] Integrations
17-
- [ ] Webhooks
1812
- [ ] Feeds
19-
- [ ] Groups
2013
- [ ] Filters
14+
- [ ] Groups
15+
- [ ] Integrations
16+
- [x] Labels
17+
- [x] NewsletterEmails
2118
- [ ] Rules
19+
- [x] Search
2220
- [x] Subscriptions
21+
- [ ] Users
22+
- [ ] Webhooks
23+
24+
Supported mutation queries:
25+
26+
- [ ] AddPopularRead
27+
- [ ] BulkAction
28+
- [ ] CreateArticle
29+
- [ ] CreateGroup
30+
- [ ] DeleteAccount
31+
- [x] DeleteArticle (SetBookmarkArticle)
32+
- [ ] DeleteFilter
33+
- [ ] DeleteIntegration
34+
- [ ] DeleteLabel
35+
- [ ] DeleteRule
36+
- [ ] DeleteWebhook
37+
- [x] SaveUrl
38+
- [ ] EmptyTrash
2339

2440
## Usage
2541

api.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/http"
66
"time"
77

8+
"github.com/google/uuid"
89
"github.com/rubiojr/omnivore-go/queries"
910
"github.com/shurcooL/graphql"
1011
)
@@ -327,3 +328,34 @@ func (c *Omnivore) Labels() ([]*Label, error) {
327328
func (c *SearchItem) IsUnread() bool {
328329
return c.ReadAt.IsZero()
329330
}
331+
332+
func (c *Omnivore) SaveUrl(url string) error {
333+
input := queries.SaveUrlInput{
334+
Url: graphql.String(url),
335+
ClientRequestId: graphql.ID(uuid.New().String()),
336+
Source: graphql.String("api"),
337+
}
338+
339+
variables := map[string]any{
340+
"input": input,
341+
}
342+
343+
return c.graphql.Mutate(context.Background(), &queries.SaveUrl, variables)
344+
}
345+
346+
// DeleteArticle deletes an article from the library.
347+
//
348+
// The query name is weird, see https://github.com/omnivore-app/omnivore/issues/2380
349+
func (c *Omnivore) DeleteArticle(id string) error {
350+
input := queries.SetBookmarkArticleInput{
351+
ArticleId: graphql.ID(id),
352+
Bookmark: graphql.Boolean(false),
353+
}
354+
355+
variables := map[string]any{
356+
"input": input,
357+
}
358+
359+
err := c.graphql.Mutate(context.Background(), &queries.DeleteArticle, variables)
360+
return err
361+
}

api_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package omnivore_test
22

33
import (
4+
"fmt"
45
"os"
56
"testing"
7+
"time"
68

79
"github.com/rubiojr/omnivore-go"
810
"github.com/stretchr/testify/assert"
@@ -50,3 +52,16 @@ func TestLabels(t *testing.T) {
5052
assert.Equal(t, labels[0].Color, "#F26522")
5153
assert.Equal(t, labels[0].Description, "")
5254
}
55+
56+
func TestAddUrl(t *testing.T) {
57+
client := omnivore.NewClient(omnivore.Opts{Token: os.Getenv("OMNIVORE_API_TOKEN")})
58+
wp := "https://en.wikipedia.org/wiki/Leet"
59+
err := client.SaveUrl(wp)
60+
assert.NoError(t, err, "Failed to save article")
61+
articles, err := client.Search(omnivore.SearchOpts{Query: fmt.Sprintf(`title:"%s"`, "Leet")})
62+
assert.Equal(t, len(articles), 1)
63+
// Wait a bit, deletes right after saving are ignored otherwise
64+
time.Sleep(5 * time.Second)
65+
err = client.DeleteArticle(articles[0].ID)
66+
assert.NoError(t, err, "Failed to delete article")
67+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/rubiojr/omnivore-go
33
go 1.22.1
44

55
require (
6+
github.com/google/uuid v1.6.0
67
github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466
78
github.com/stretchr/testify v1.9.0
89
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
22
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
4+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
35
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
46
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
57
github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466 h1:17JxqqJY66GmZVHkmAsGEkcIu0oCe3AM420QDgGwZx0=

queries/deletearticle.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package queries
2+
3+
import "github.com/shurcooL/graphql"
4+
5+
var DeleteArticle struct {
6+
SetBookmarkArticle struct {
7+
SetBookmarkArticleSuccess struct {
8+
BookmarkedArticle struct {
9+
ID graphql.ID
10+
LinkId graphql.ID
11+
}
12+
} `graphql:"... on SetBookmarkArticleSuccess"`
13+
SetBookmarkArticleError struct {
14+
ErrorCodes string
15+
} `graphql:"... on SetBookmarkArticleError"`
16+
} `graphql:"setBookmarkArticle(input: $input)"`
17+
}
18+
19+
type SetBookmarkArticleInput struct {
20+
ArticleId graphql.ID `json:"articleID"`
21+
Bookmark graphql.Boolean `json:"bookmark"`
22+
}

queries/saveurl.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package queries
2+
3+
import "github.com/shurcooL/graphql"
4+
5+
var SaveUrl struct {
6+
SaveUrl struct {
7+
SaveSuccess struct {
8+
ClientRequestID graphql.ID
9+
Url string
10+
} `graphql:"... on SaveSuccess"`
11+
SaveError struct {
12+
ErrorCodes string
13+
} `graphql:"... on SaveError"`
14+
} `graphql:"saveUrl(input: $input)"`
15+
}
16+
17+
type SaveUrlInput struct {
18+
Url graphql.String `json:"url"`
19+
ClientRequestId graphql.ID `json:"clientRequestId"`
20+
Source graphql.String `json:"source"`
21+
}

queries/search.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ var Search struct {
2828
WordsCount int
2929
FeedContent string
3030
Folder string
31-
Labels []struct {
31+
32+
Labels []struct {
3233
Name string
3334
Color string
3435
CreatedAt time.Time

0 commit comments

Comments
 (0)