Add item query

This commit is contained in:
ekzyis 2024-06-02 01:36:39 -05:00
parent 17878b2f32
commit f187218532
1 changed files with 79 additions and 2 deletions

View File

@ -8,7 +8,7 @@ import (
type Item struct {
Id int `json:"id,string"`
ParentId int `json:"parentId,string"`
ParentId int `json:"parentId"`
Title string `json:"title"`
Url string `json:"url"`
Sats int `json:"sats"`
@ -20,7 +20,7 @@ type Item struct {
type Comment struct {
Id int `json:"id,string"`
ParentId int `json:"parentId,string"`
ParentId int `json:"parentId"`
CreatedAt time.Time `json:"createdAt"`
Text string `json:"text"`
User User `json:"user"`
@ -43,6 +43,13 @@ type ItemsCursor struct {
Cursor string `json:"cursor"`
}
type ItemResponse struct {
Errors []GqlError `json:"errors"`
Data struct {
Item Item `json:"item"`
} `json:"data"`
}
type ItemsResponse struct {
Errors []GqlError `json:"errors"`
Data struct {
@ -97,6 +104,76 @@ func (e *DupesError) Error() string {
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) {
if query == nil {
query = &ItemsQuery{}