Compare commits

..

4 Commits

Author SHA1 Message Date
049ea1f1c0 Add tests 2024-07-02 23:47:36 +02:00
c6f926dd8c Update API
* mutations now return { result, invoice, paymentMethod } due to new backend payment optimism
2024-07-02 23:47:35 +02:00
f4c1b57c1f Add createInvoice mutation 2024-06-02 04:28:03 -05:00
f187218532 Add item query 2024-06-02 01:36:39 -05:00
2 changed files with 147 additions and 3 deletions

View File

@ -1,6 +1,10 @@
package sn
import "time"
import (
"encoding/json"
"fmt"
"time"
)
type Invoice struct {
Id int `json:"id,string"`
@ -28,3 +32,66 @@ const (
PaymentMethodOptimistic PaymentMethod = "OPTIMISTIC"
PaymentMethodPessimistic PaymentMethod = "PESSIMISTIC"
)
type CreateInvoiceArgs struct {
Amount int
ExpireSecs int
HodlInvoice bool
}
type CreateInvoiceResponse struct {
Errors []GqlError `json:"errors"`
Data struct {
CreateInvoice Invoice `json:"createInvoice"`
} `json:"data"`
}
func (c *Client) CreateInvoice(args *CreateInvoiceArgs) (*Invoice, error) {
if args == nil {
args = &CreateInvoiceArgs{}
}
body := GqlBody{
// TODO: add createdAt
// when I wrote this code, createdAt returned null but is non-nullable
// so I had to remove it.
Query: `
mutation createInvoice($amount: Int!, $expireSecs: Int, $hodlInvoice: Boolean) {
createInvoice(amount: $amount, expireSecs: $expireSecs, hodlInvoice: $hodlInvoice) {
id
hash
hmac
bolt11
satsRequested
satsReceived
isHeld
comment
confirmedPreimage
expiresAt
confirmedAt
}
}`,
Variables: map[string]interface{}{
"amount": args.Amount,
},
}
resp, err := c.callApi(body)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var respBody CreateInvoiceResponse
err = json.NewDecoder(resp.Body).Decode(&respBody)
if err != nil {
err = fmt.Errorf("error decoding items: %w", err)
return nil, err
}
err = c.checkForErrors(respBody.Errors)
if err != nil {
return nil, err
}
return &respBody.Data.CreateInvoice, nil
}

View File

@ -8,7 +8,7 @@ import (
type Item struct {
Id int `json:"id,string"`
ParentId int `json:"parentId,string"`
ParentId int `json:"parentId"`
Title string `json:"title"`
Url string `json:"url"`
Sats int `json:"sats"`
@ -20,7 +20,7 @@ type Item struct {
type Comment struct {
Id int `json:"id,string"`
ParentId int `json:"parentId,string"`
ParentId int `json:"parentId"`
CreatedAt time.Time `json:"createdAt"`
Text string `json:"text"`
User User `json:"user"`
@ -43,6 +43,13 @@ type ItemsCursor struct {
Cursor string `json:"cursor"`
}
type ItemResponse struct {
Errors []GqlError `json:"errors"`
Data struct {
Item Item `json:"item"`
} `json:"data"`
}
type ItemsResponse struct {
Errors []GqlError `json:"errors"`
Data struct {
@ -103,6 +110,76 @@ func (e *DupesError) Error() string {
return fmt.Sprintf("found %d dupes for %s", len(e.Dupes), e.Url)
}
func (c *Client) Item(id int) (*Item, error) {
body := GqlBody{
Query: `
query item($id: ID!) {
item(id: $id) {
id
parentId
createdAt
deletedAt
title
url
user {
id
name
}
otsHash
position
sats
boost
bounty
bountyPaidTo
path
upvotes
meSats
meDontLikeSats
meBookmark
meSubscription
outlawed
freebie
ncomments
commentSats
lastCommentAt
maxBid
isJob
company
location
remote
subName
pollCost
status
uploadId
mine
position
}
}`,
Variables: map[string]interface{}{
"id": id,
},
}
resp, err := c.callApi(body)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var respBody ItemResponse
err = json.NewDecoder(resp.Body).Decode(&respBody)
if err != nil {
err = fmt.Errorf("error decoding item: %w", err)
return nil, err
}
err = c.checkForErrors(respBody.Errors)
if err != nil {
return nil, err
}
return &respBody.Data.Item, nil
}
func (c *Client) Items(query *ItemsQuery) (*ItemsCursor, error) {
if query == nil {
query = &ItemsQuery{}