Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7db0515e92 | ||
|
|
44f58bcc75 | ||
|
|
3498d0da7f |
@ -1,14 +1,11 @@
|
|||||||
# snappy
|
# snappy
|
||||||
|
|
||||||
<div style="text-align: center">
|
<p align="center">
|
||||||
|
|
||||||
<img src="https://stacker.news/favicon.png" width="64" height="64" />
|
<img src="https://stacker.news/favicon.png" width="64" height="64" />
|
||||||
<img src="https://go.dev/blog/go-brand/Go-Logo/PNG/Go-Logo_Blue.png" width="64" height="64" />
|
<img src="https://go.dev/blog/go-brand/Go-Logo/PNG/Go-Logo_Blue.png" width="64" height="64" />
|
||||||
|
</p>
|
||||||
|
|
||||||
[stacker.news](https://stacker.news) API client package for Go
|
<p align="center">A Go client for the <a href="https://stacker.news" target="_blank">Stacker News</a> GraphQL API</p>
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
## How to use
|
## How to use
|
||||||
|
|
||||||
|
|||||||
19
client.go
19
client.go
@ -107,3 +107,22 @@ func (c *Client) checkForErrors(err []GqlError) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) checkForPayInErrors(payIn PayIn) error {
|
||||||
|
privates := payIn.PayerPrivates
|
||||||
|
if privates.PayInFailureReason != "" {
|
||||||
|
return fmt.Errorf("mutation failed: %s", privates.PayInFailureReason)
|
||||||
|
}
|
||||||
|
|
||||||
|
bolt11 := privates.PayInBolt11
|
||||||
|
if bolt11.Id != 0 {
|
||||||
|
return fmt.Errorf("mutation failed: bolt11 payment required")
|
||||||
|
}
|
||||||
|
|
||||||
|
result := privates.Result
|
||||||
|
if result.Id == 0 {
|
||||||
|
return fmt.Errorf("mutation failed: no result id")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
111
items.go
111
items.go
@ -61,30 +61,37 @@ type ItemsResponse struct {
|
|||||||
} `json:"data"`
|
} `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ItemPaidAction struct {
|
type PayIn struct {
|
||||||
Result Item `json:"result"`
|
Id int `json:"id"`
|
||||||
Invoice Invoice `json:"invoice"`
|
PayerPrivates struct {
|
||||||
PaymentMethod PaymentMethod `json:"paymentMethod"`
|
PayInFailureReason string `json:"payInFailureReason"`
|
||||||
|
PayInBolt11 struct {
|
||||||
|
Id int `json:"id"`
|
||||||
|
} `json:"payInBolt11"`
|
||||||
|
Result struct {
|
||||||
|
Id int `json:"id,string"`
|
||||||
|
} `json:"result"`
|
||||||
|
} `json:"payerPrivates"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type UpsertDiscussionResponse struct {
|
type UpsertDiscussionResponse struct {
|
||||||
Errors []GqlError `json:"errors"`
|
Errors []GqlError `json:"errors"`
|
||||||
Data struct {
|
Data struct {
|
||||||
UpsertDiscussion ItemPaidAction `json:"upsertDiscussion"`
|
UpsertDiscussion PayIn `json:"upsertDiscussion"`
|
||||||
} `json:"data"`
|
} `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type UpsertLinkResponse struct {
|
type UpsertLinkResponse struct {
|
||||||
Errors []GqlError `json:"errors"`
|
Errors []GqlError `json:"errors"`
|
||||||
Data struct {
|
Data struct {
|
||||||
UpsertLink ItemPaidAction `json:"upsertLink"`
|
UpsertLink PayIn `json:"upsertLink"`
|
||||||
} `json:"data"`
|
} `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type UpsertCommentResponse struct {
|
type UpsertCommentResponse struct {
|
||||||
Errors []GqlError `json:"errors"`
|
Errors []GqlError `json:"errors"`
|
||||||
Data struct {
|
Data struct {
|
||||||
UpsertComment ItemPaidAction `json:"upsertComment"`
|
UpsertComment PayIn `json:"upsertComment"`
|
||||||
} `json:"data"`
|
} `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -226,17 +233,18 @@ func (c *Client) PostDiscussion(title string, text string, sub string) (int, err
|
|||||||
Query: `
|
Query: `
|
||||||
mutation upsertDiscussion($title: String!, $text: String, $sub: String) {
|
mutation upsertDiscussion($title: String!, $text: String, $sub: String) {
|
||||||
upsertDiscussion(title: $title, text: $text, sub: $sub) {
|
upsertDiscussion(title: $title, text: $text, sub: $sub) {
|
||||||
result {
|
id
|
||||||
id
|
payerPrivates {
|
||||||
|
payInFailureReason
|
||||||
|
payInBolt11 {
|
||||||
|
id
|
||||||
|
}
|
||||||
|
result {
|
||||||
|
... on Item {
|
||||||
|
id
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
invoice {
|
|
||||||
id
|
|
||||||
hash
|
|
||||||
bolt11
|
|
||||||
satsRequested
|
|
||||||
expiresAt
|
|
||||||
}
|
|
||||||
paymentMethod
|
|
||||||
}
|
}
|
||||||
}`,
|
}`,
|
||||||
Variables: map[string]interface{}{
|
Variables: map[string]interface{}{
|
||||||
@ -264,12 +272,13 @@ func (c *Client) PostDiscussion(title string, text string, sub string) (int, err
|
|||||||
return -1, err
|
return -1, err
|
||||||
}
|
}
|
||||||
|
|
||||||
inv := respBody.Data.UpsertDiscussion.Invoice
|
payIn := respBody.Data.UpsertDiscussion
|
||||||
if inv.Id != 0 {
|
err = c.checkForPayInErrors(payIn)
|
||||||
return -1, fmt.Errorf("mutation requires %d sats as payment", inv.SatsRequested)
|
if err != nil {
|
||||||
|
return -1, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return respBody.Data.UpsertDiscussion.Result.Id, nil
|
return payIn.PayerPrivates.Result.Id, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) PostLink(url string, title string, text string, sub string) (int, error) {
|
func (c *Client) PostLink(url string, title string, text string, sub string) (int, error) {
|
||||||
@ -277,17 +286,18 @@ func (c *Client) PostLink(url string, title string, text string, sub string) (in
|
|||||||
Query: `
|
Query: `
|
||||||
mutation upsertLink($url: String!, $title: String!, $text: String, $sub: String!) {
|
mutation upsertLink($url: String!, $title: String!, $text: String, $sub: String!) {
|
||||||
upsertLink(url: $url, title: $title, text: $text, sub: $sub) {
|
upsertLink(url: $url, title: $title, text: $text, sub: $sub) {
|
||||||
result {
|
id
|
||||||
id
|
payerPrivates {
|
||||||
|
payInFailureReason
|
||||||
|
payInBolt11 {
|
||||||
|
id
|
||||||
|
}
|
||||||
|
result {
|
||||||
|
... on Item {
|
||||||
|
id
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
invoice {
|
|
||||||
id
|
|
||||||
hash
|
|
||||||
bolt11
|
|
||||||
satsRequested
|
|
||||||
expiresAt
|
|
||||||
}
|
|
||||||
paymentMethod
|
|
||||||
}
|
}
|
||||||
}`,
|
}`,
|
||||||
Variables: map[string]interface{}{
|
Variables: map[string]interface{}{
|
||||||
@ -316,12 +326,13 @@ func (c *Client) PostLink(url string, title string, text string, sub string) (in
|
|||||||
return -1, err
|
return -1, err
|
||||||
}
|
}
|
||||||
|
|
||||||
inv := respBody.Data.UpsertLink.Invoice
|
payIn := respBody.Data.UpsertLink
|
||||||
if inv.Id != 0 {
|
err = c.checkForPayInErrors(payIn)
|
||||||
return -1, fmt.Errorf("mutation requires %d sats as payment", inv.SatsRequested)
|
if err != nil {
|
||||||
|
return -1, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return respBody.Data.UpsertLink.Result.Id, nil
|
return payIn.PayerPrivates.Result.Id, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) CreateComment(parentId int, text string) (int, error) {
|
func (c *Client) CreateComment(parentId int, text string) (int, error) {
|
||||||
@ -329,17 +340,18 @@ func (c *Client) CreateComment(parentId int, text string) (int, error) {
|
|||||||
Query: `
|
Query: `
|
||||||
mutation upsertComment($parentId: ID!, $text: String!) {
|
mutation upsertComment($parentId: ID!, $text: String!) {
|
||||||
upsertComment(parentId: $parentId, text: $text) {
|
upsertComment(parentId: $parentId, text: $text) {
|
||||||
result {
|
id
|
||||||
id
|
payerPrivates {
|
||||||
|
payInFailureReason
|
||||||
|
payInBolt11 {
|
||||||
|
id
|
||||||
|
}
|
||||||
|
result {
|
||||||
|
... on Item {
|
||||||
|
id
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
invoice {
|
|
||||||
id
|
|
||||||
hash
|
|
||||||
bolt11
|
|
||||||
satsRequested
|
|
||||||
expiresAt
|
|
||||||
}
|
|
||||||
paymentMethod
|
|
||||||
}
|
}
|
||||||
}`,
|
}`,
|
||||||
Variables: map[string]interface{}{
|
Variables: map[string]interface{}{
|
||||||
@ -366,12 +378,13 @@ func (c *Client) CreateComment(parentId int, text string) (int, error) {
|
|||||||
return -1, err
|
return -1, err
|
||||||
}
|
}
|
||||||
|
|
||||||
inv := respBody.Data.UpsertComment.Invoice
|
payIn := respBody.Data.UpsertComment
|
||||||
if inv.Id != 0 {
|
err = c.checkForPayInErrors(payIn)
|
||||||
return -1, fmt.Errorf("mutation requires %d sats as payment", inv.SatsRequested)
|
if err != nil {
|
||||||
|
return -1, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return respBody.Data.UpsertComment.Result.Id, nil
|
return payIn.PayerPrivates.Result.Id, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Dupes(url string) (*[]Dupe, error) {
|
func (c *Client) Dupes(url string) (*[]Dupe, error) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user