Compare commits

..

2 Commits

Author SHA1 Message Date
21c2bfd630 Add tests 2024-07-02 23:43:02 +02:00
b2a4dcbd19 Update API
* mutations now return { result, invoice, paymentMethod } due to new backend payment optimism
2024-07-02 23:42:02 +02:00
2 changed files with 3 additions and 147 deletions

View File

@ -1,10 +1,6 @@
package sn package sn
import ( import "time"
"encoding/json"
"fmt"
"time"
)
type Invoice struct { type Invoice struct {
Id int `json:"id,string"` Id int `json:"id,string"`
@ -32,66 +28,3 @@ const (
PaymentMethodOptimistic PaymentMethod = "OPTIMISTIC" PaymentMethodOptimistic PaymentMethod = "OPTIMISTIC"
PaymentMethodPessimistic PaymentMethod = "PESSIMISTIC" 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 { type Item struct {
Id int `json:"id,string"` Id int `json:"id,string"`
ParentId int `json:"parentId"` ParentId int `json:"parentId,string"`
Title string `json:"title"` Title string `json:"title"`
Url string `json:"url"` Url string `json:"url"`
Sats int `json:"sats"` Sats int `json:"sats"`
@ -20,7 +20,7 @@ type Item struct {
type Comment struct { type Comment struct {
Id int `json:"id,string"` Id int `json:"id,string"`
ParentId int `json:"parentId"` ParentId int `json:"parentId,string"`
CreatedAt time.Time `json:"createdAt"` CreatedAt time.Time `json:"createdAt"`
Text string `json:"text"` Text string `json:"text"`
User User `json:"user"` User User `json:"user"`
@ -43,13 +43,6 @@ type ItemsCursor struct {
Cursor string `json:"cursor"` Cursor string `json:"cursor"`
} }
type ItemResponse struct {
Errors []GqlError `json:"errors"`
Data struct {
Item Item `json:"item"`
} `json:"data"`
}
type ItemsResponse struct { type ItemsResponse struct {
Errors []GqlError `json:"errors"` Errors []GqlError `json:"errors"`
Data struct { Data struct {
@ -110,76 +103,6 @@ func (e *DupesError) Error() string {
return fmt.Sprintf("found %d dupes for %s", len(e.Dupes), e.Url) 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) { func (c *Client) Items(query *ItemsQuery) (*ItemsCursor, error) {
if query == nil { if query == nil {
query = &ItemsQuery{} query = &ItemsQuery{}