Compare commits
No commits in common. "develop" and "v0.6.1" have entirely different histories.
@ -1,11 +1,14 @@
|
||||
# snappy
|
||||
|
||||
<p align="center">
|
||||
<div style="text-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>
|
||||
|
||||
<p align="center">A Go client for the <a href="https://stacker.news" target="_blank">Stacker News</a> GraphQL API</p>
|
||||
[stacker.news](https://stacker.news) API client package for Go
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
## How to use
|
||||
|
||||
|
||||
30
client.go
30
client.go
@ -23,21 +23,14 @@ func NewClient(options ...func(*Client)) *Client {
|
||||
}
|
||||
|
||||
// set defaults
|
||||
var ok bool
|
||||
if c.BaseUrl == "" {
|
||||
c.BaseUrl, ok = os.LookupEnv("SN_BASE_URL")
|
||||
if !ok {
|
||||
c.BaseUrl = "https://stacker.news"
|
||||
}
|
||||
c.BaseUrl = "https://stacker.news"
|
||||
}
|
||||
if c.ApiKey == "" {
|
||||
c.ApiKey = os.Getenv("SN_API_KEY")
|
||||
}
|
||||
if c.MediaUrl == "" {
|
||||
c.MediaUrl, ok = os.LookupEnv("SN_MEDIA_URL")
|
||||
if !ok {
|
||||
c.MediaUrl = "https://m.stacker.news"
|
||||
}
|
||||
c.MediaUrl = "https://m.stacker.news"
|
||||
}
|
||||
c.ApiUrl = fmt.Sprintf("%s/api/graphql", c.BaseUrl)
|
||||
|
||||
@ -107,22 +100,3 @@ 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
|
||||
}
|
||||
|
||||
84
items.go
84
items.go
@ -61,37 +61,30 @@ type ItemsResponse struct {
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
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 ItemPaidAction struct {
|
||||
Result Item `json:"result"`
|
||||
Invoice Invoice `json:"invoice"`
|
||||
PaymentMethod PaymentMethod `json:"paymentMethod"`
|
||||
}
|
||||
|
||||
type UpsertDiscussionResponse struct {
|
||||
Errors []GqlError `json:"errors"`
|
||||
Data struct {
|
||||
UpsertDiscussion PayIn `json:"upsertDiscussion"`
|
||||
UpsertDiscussion ItemPaidAction `json:"upsertDiscussion"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
type UpsertLinkResponse struct {
|
||||
Errors []GqlError `json:"errors"`
|
||||
Data struct {
|
||||
UpsertLink PayIn `json:"upsertLink"`
|
||||
UpsertLink ItemPaidAction `json:"upsertLink"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
type UpsertCommentResponse struct {
|
||||
Errors []GqlError `json:"errors"`
|
||||
Data struct {
|
||||
UpsertComment PayIn `json:"upsertComment"`
|
||||
UpsertComment ItemPaidAction `json:"upsertComment"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
@ -233,18 +226,7 @@ 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) {
|
||||
id
|
||||
payerPrivates {
|
||||
payInFailureReason
|
||||
payInBolt11 {
|
||||
id
|
||||
}
|
||||
result {
|
||||
... on Item {
|
||||
id
|
||||
}
|
||||
}
|
||||
}
|
||||
result { id }
|
||||
}
|
||||
}`,
|
||||
Variables: map[string]interface{}{
|
||||
@ -272,13 +254,7 @@ func (c *Client) PostDiscussion(title string, text string, sub string) (int, err
|
||||
return -1, err
|
||||
}
|
||||
|
||||
payIn := respBody.Data.UpsertDiscussion
|
||||
err = c.checkForPayInErrors(payIn)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return payIn.PayerPrivates.Result.Id, nil
|
||||
return respBody.Data.UpsertDiscussion.Result.Id, nil
|
||||
}
|
||||
|
||||
func (c *Client) PostLink(url string, title string, text string, sub string) (int, error) {
|
||||
@ -286,18 +262,7 @@ 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) {
|
||||
id
|
||||
payerPrivates {
|
||||
payInFailureReason
|
||||
payInBolt11 {
|
||||
id
|
||||
}
|
||||
result {
|
||||
... on Item {
|
||||
id
|
||||
}
|
||||
}
|
||||
}
|
||||
result { id }
|
||||
}
|
||||
}`,
|
||||
Variables: map[string]interface{}{
|
||||
@ -326,13 +291,7 @@ func (c *Client) PostLink(url string, title string, text string, sub string) (in
|
||||
return -1, err
|
||||
}
|
||||
|
||||
payIn := respBody.Data.UpsertLink
|
||||
err = c.checkForPayInErrors(payIn)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return payIn.PayerPrivates.Result.Id, nil
|
||||
return respBody.Data.UpsertLink.Result.Id, nil
|
||||
}
|
||||
|
||||
func (c *Client) CreateComment(parentId int, text string) (int, error) {
|
||||
@ -340,18 +299,7 @@ func (c *Client) CreateComment(parentId int, text string) (int, error) {
|
||||
Query: `
|
||||
mutation upsertComment($parentId: ID!, $text: String!) {
|
||||
upsertComment(parentId: $parentId, text: $text) {
|
||||
id
|
||||
payerPrivates {
|
||||
payInFailureReason
|
||||
payInBolt11 {
|
||||
id
|
||||
}
|
||||
result {
|
||||
... on Item {
|
||||
id
|
||||
}
|
||||
}
|
||||
}
|
||||
result { id }
|
||||
}
|
||||
}`,
|
||||
Variables: map[string]interface{}{
|
||||
@ -378,13 +326,7 @@ func (c *Client) CreateComment(parentId int, text string) (int, error) {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
payIn := respBody.Data.UpsertComment
|
||||
err = c.checkForPayInErrors(payIn)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return payIn.PayerPrivates.Result.Id, nil
|
||||
return respBody.Data.UpsertComment.Result.Id, nil
|
||||
}
|
||||
|
||||
func (c *Client) Dupes(url string) (*[]Dupe, error) {
|
||||
|
||||
16
upload.go
16
upload.go
@ -26,18 +26,12 @@ type GetSignedPOSTResponse struct {
|
||||
func (c *Client) UploadImage(img *image.RGBA) (string, error) {
|
||||
var (
|
||||
b = img.Bounds()
|
||||
width = b.Dx()
|
||||
height = b.Dy()
|
||||
width = b.Max.X
|
||||
height = b.Max.Y
|
||||
size = width * height
|
||||
type_ = "image/png"
|
||||
size int
|
||||
)
|
||||
|
||||
var imgBuf bytes.Buffer
|
||||
if err := png.Encode(&imgBuf, img); err != nil {
|
||||
return "", err
|
||||
}
|
||||
size = imgBuf.Len()
|
||||
|
||||
// get signed URL for S3 upload
|
||||
body := GqlBody{
|
||||
Query: `
|
||||
@ -105,7 +99,9 @@ func (c *Client) UploadImage(img *image.RGBA) (string, error) {
|
||||
if fw, err = w.CreateFormFile("file", "image.png"); err != nil {
|
||||
return "", err
|
||||
}
|
||||
fw.Write(imgBuf.Bytes())
|
||||
if err = png.Encode(fw, img); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if err = w.Close(); err != nil {
|
||||
return "", err
|
||||
|
||||
55
user.go
55
user.go
@ -1,57 +1,6 @@
|
||||
package sn
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
Id int `json:"id,string"`
|
||||
Name string `json:"name"`
|
||||
Privates UserPrivates `json:"privates"`
|
||||
}
|
||||
|
||||
type UserPrivates struct {
|
||||
Sats int `json:"sats"`
|
||||
}
|
||||
|
||||
type MeResponse struct {
|
||||
Errors []GqlError `json:"errors"`
|
||||
Data struct {
|
||||
Me User `json:"me"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
func (c *Client) Me() (*User, error) {
|
||||
body := GqlBody{
|
||||
Query: `
|
||||
query me {
|
||||
me {
|
||||
id
|
||||
name
|
||||
privates {
|
||||
sats
|
||||
}
|
||||
}
|
||||
}`,
|
||||
}
|
||||
|
||||
resp, err := c.callApi(body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var respBody MeResponse
|
||||
err = json.NewDecoder(resp.Body).Decode(&respBody)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error decoding me: %w", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = c.checkForErrors(respBody.Errors)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &respBody.Data.Me, nil
|
||||
Id int `json:"id,string"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user