Compare commits

...

3 Commits

Author SHA1 Message Date
112cd5874b Allow full config via environment 2025-02-03 22:54:49 +01:00
117d04912f Add me query 2024-10-19 19:11:14 +02:00
86612a527b Fix EntityTooSmall error from S3 2024-10-16 04:56:50 +02:00
3 changed files with 72 additions and 10 deletions

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)

View File

@ -26,12 +26,18 @@ type GetSignedPOSTResponse struct {
func (c *Client) UploadImage(img *image.RGBA) (string, error) {
var (
b = img.Bounds()
width = b.Max.X
height = b.Max.Y
size = width * height
width = b.Dx()
height = b.Dy()
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: `
@ -99,9 +105,7 @@ func (c *Client) UploadImage(img *image.RGBA) (string, error) {
if fw, err = w.CreateFormFile("file", "image.png"); err != nil {
return "", err
}
if err = png.Encode(fw, img); err != nil {
return "", err
}
fw.Write(imgBuf.Bytes())
if err = w.Close(); err != nil {
return "", err

55
user.go
View File

@ -1,6 +1,57 @@
package sn
import (
"encoding/json"
"fmt"
)
type User struct {
Id int `json:"id,string"`
Name string `json:"name"`
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
}