Compare commits

..

5 Commits

Author SHA1 Message Date
ekzyis
7db0515e92 Fix alignment in README 2025-12-02 21:09:54 +01:00
ekzyis
44f58bcc75 Fix parsing of PayIn response 2025-12-02 20:44:24 +01:00
ekzyis
3498d0da7f Update API to support new PayIn type
I am not sure if this will detect when an item failed because a payment is required, but this brings @nitter back online.
2025-12-02 17:02:06 +01:00
47a35e428e Check if payment required 2025-02-03 23:30:05 +01:00
112cd5874b Allow full config via environment 2025-02-03 22:54:49 +01:00
3 changed files with 102 additions and 21 deletions

View File

@ -1,14 +1,11 @@
# snappy
<div style="text-align: center">
<p align="center">
<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" />
</p>
[stacker.news](https://stacker.news) API client package for Go
</div>
<p align="center">A Go client for the <a href="https://stacker.news" target="_blank">Stacker News</a> GraphQL API</p>
## How to use

View File

@ -23,14 +23,21 @@ func NewClient(options ...func(*Client)) *Client {
}
// set defaults
var ok bool
if c.BaseUrl == "" {
c.BaseUrl = "https://stacker.news"
c.BaseUrl, ok = os.LookupEnv("SN_BASE_URL")
if !ok {
c.BaseUrl = "https://stacker.news"
}
}
if c.ApiKey == "" {
c.ApiKey = os.Getenv("SN_API_KEY")
}
if c.MediaUrl == "" {
c.MediaUrl = "https://m.stacker.news"
c.MediaUrl, ok = os.LookupEnv("SN_MEDIA_URL")
if !ok {
c.MediaUrl = "https://m.stacker.news"
}
}
c.ApiUrl = fmt.Sprintf("%s/api/graphql", c.BaseUrl)
@ -100,3 +107,22 @@ func (c *Client) checkForErrors(err []GqlError) error {
}
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
}

View File

@ -61,30 +61,37 @@ type ItemsResponse struct {
} `json:"data"`
}
type ItemPaidAction struct {
Result Item `json:"result"`
Invoice Invoice `json:"invoice"`
PaymentMethod PaymentMethod `json:"paymentMethod"`
type PayIn struct {
Id int `json:"id"`
PayerPrivates struct {
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 {
Errors []GqlError `json:"errors"`
Data struct {
UpsertDiscussion ItemPaidAction `json:"upsertDiscussion"`
UpsertDiscussion PayIn `json:"upsertDiscussion"`
} `json:"data"`
}
type UpsertLinkResponse struct {
Errors []GqlError `json:"errors"`
Data struct {
UpsertLink ItemPaidAction `json:"upsertLink"`
UpsertLink PayIn `json:"upsertLink"`
} `json:"data"`
}
type UpsertCommentResponse struct {
Errors []GqlError `json:"errors"`
Data struct {
UpsertComment ItemPaidAction `json:"upsertComment"`
UpsertComment PayIn `json:"upsertComment"`
} `json:"data"`
}
@ -226,7 +233,18 @@ func (c *Client) PostDiscussion(title string, text string, sub string) (int, err
Query: `
mutation upsertDiscussion($title: String!, $text: String, $sub: String) {
upsertDiscussion(title: $title, text: $text, sub: $sub) {
result { id }
id
payerPrivates {
payInFailureReason
payInBolt11 {
id
}
result {
... on Item {
id
}
}
}
}
}`,
Variables: map[string]interface{}{
@ -254,7 +272,13 @@ func (c *Client) PostDiscussion(title string, text string, sub string) (int, err
return -1, err
}
return respBody.Data.UpsertDiscussion.Result.Id, nil
payIn := respBody.Data.UpsertDiscussion
err = c.checkForPayInErrors(payIn)
if err != nil {
return -1, err
}
return payIn.PayerPrivates.Result.Id, nil
}
func (c *Client) PostLink(url string, title string, text string, sub string) (int, error) {
@ -262,7 +286,18 @@ func (c *Client) PostLink(url string, title string, text string, sub string) (in
Query: `
mutation upsertLink($url: String!, $title: String!, $text: String, $sub: String!) {
upsertLink(url: $url, title: $title, text: $text, sub: $sub) {
result { id }
id
payerPrivates {
payInFailureReason
payInBolt11 {
id
}
result {
... on Item {
id
}
}
}
}
}`,
Variables: map[string]interface{}{
@ -291,7 +326,13 @@ func (c *Client) PostLink(url string, title string, text string, sub string) (in
return -1, err
}
return respBody.Data.UpsertLink.Result.Id, nil
payIn := respBody.Data.UpsertLink
err = c.checkForPayInErrors(payIn)
if err != nil {
return -1, err
}
return payIn.PayerPrivates.Result.Id, nil
}
func (c *Client) CreateComment(parentId int, text string) (int, error) {
@ -299,7 +340,18 @@ func (c *Client) CreateComment(parentId int, text string) (int, error) {
Query: `
mutation upsertComment($parentId: ID!, $text: String!) {
upsertComment(parentId: $parentId, text: $text) {
result { id }
id
payerPrivates {
payInFailureReason
payInBolt11 {
id
}
result {
... on Item {
id
}
}
}
}
}`,
Variables: map[string]interface{}{
@ -326,7 +378,13 @@ func (c *Client) CreateComment(parentId int, text string) (int, error) {
return -1, err
}
return respBody.Data.UpsertComment.Result.Id, nil
payIn := respBody.Data.UpsertComment
err = c.checkForPayInErrors(payIn)
if err != nil {
return -1, err
}
return payIn.PayerPrivates.Result.Id, nil
}
func (c *Client) Dupes(url string) (*[]Dupe, error) {