From 117d04912f467b9e685f993d2cb42c40286d182d Mon Sep 17 00:00:00 2001 From: ekzyis Date: Sat, 19 Oct 2024 19:11:14 +0200 Subject: [PATCH] Add me query --- user.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/user.go b/user.go index a5e9b70..ef9bf30 100644 --- a/user.go +++ b/user.go @@ -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 }