Skip to content

Commit

Permalink
Merge pull request #3 from rubiojr/api-keys
Browse files Browse the repository at this point in the history
Add ApiKeys support
  • Loading branch information
rubiojr authored Mar 23, 2024
2 parents 005fe2e + f8a7e6c commit 2b06cdb
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Supported queries:

- [x] Search
- [x] NewsletterEmails
- [ ] ApiKeys
- [x] ApiKeys
- [ ] Users
- [ ] Labels
- [ ] Integrations
Expand Down
37 changes: 37 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ type SearchOpts struct {
Format string
}

type ApiKey struct {
ID graphql.ID
Key string
CreatedAt time.Time
ExpiresAt string
Name string
Scopes []string
UsedAt time.Time
}

func NewClient(opts Opts) *Omnivore {
return NewClientFor("https://api-prod.omnivore.app/api/graphql", opts)
}
Expand Down Expand Up @@ -215,6 +225,33 @@ func (c *Omnivore) Subscriptions() ([]Subscription, error) {
Url: sub.Url,
})
}
return a, nil
}

func (c *Omnivore) ApiKeys() ([]ApiKey, error) {
a := []ApiKey{}

err := c.graphql.Query(context.Background(), &queries.ApiKeys, nil)
if err != nil {
return nil, err
}

result := queries.ApiKeys.ApiKeys.ApiKeysSuccess
for _, apiKey := range result.ApiKeys {
a = append(a, ApiKey{
ID: apiKey.ID,
Key: apiKey.Key,
CreatedAt: apiKey.CreatedAt,
ExpiresAt: apiKey.ExpiresAt,
Name: apiKey.Name,
Scopes: apiKey.Scopes,
UsedAt: apiKey.UsedAt,
})
}

return a, nil
}

func (a *ApiKey) HasExpiry() bool {
return a.ExpiresAt != "+275760-09-13T00:00:00.000Z"
}
10 changes: 10 additions & 0 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,13 @@ func TestSubcriptions(t *testing.T) {
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()
assert.NoError(t, err, "Failed to get api keys")
assert.Equal(t, len(keys), 1)
assert.Equal(t, keys[0].Name, "omnivore-go-github")
assert.Equal(t, keys[0].ExpiresAt, "+275760-09-13T00:00:00.000Z")
assert.False(t, keys[0].HasExpiry())
}
23 changes: 23 additions & 0 deletions queries/apikeys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package queries

import (
"time"

"github.com/shurcooL/graphql"
)

var ApiKeys struct {
ApiKeys struct {
ApiKeysSuccess struct {
ApiKeys []struct {
ID graphql.ID
Key string
CreatedAt time.Time
UsedAt time.Time
ExpiresAt string
Scopes []string
Name string
}
} `graphql:"... on ApiKeysSuccess"`
}
}

0 comments on commit 2b06cdb

Please sign in to comment.