aboutsummaryrefslogtreecommitdiff
path: root/lib/user.go
diff options
context:
space:
mode:
authorBobby <[email protected]>2024-09-08 18:34:58 -0400
committerBobby <[email protected]>2024-09-08 18:34:58 -0400
commite25611bde49fe2db28a006aca3ea49eece046c5f (patch)
treedae68f8784010ff9002fe0e3c5d228879c15a25f /lib/user.go
parent61bc5b38044bc52442415f83390ba72ed3b27491 (diff)
downloadyato-main.tar.xz
yato-main.zip
Image Rendering. Recommendations Thingy. Basic HomeHEADmain
Diffstat (limited to 'lib/user.go')
-rw-r--r--lib/user.go45
1 files changed, 45 insertions, 0 deletions
diff --git a/lib/user.go b/lib/user.go
new file mode 100644
index 0000000..cbfde00
--- /dev/null
+++ b/lib/user.go
@@ -0,0 +1,45 @@
+package lib
+
+import (
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "yato/config"
+)
+
+type MALUser struct {
+ ID int `json:"id"`
+ Name string `json:"name"`
+ Birthday string `json:"birthday"`
+ Location string `json:"location"`
+ JoinedAt string `json:"joined_at"`
+ Picture string `json:"picture"`
+}
+
+func CurrentUser() (*MALUser, error) {
+ var user MALUser
+
+ req, err := http.NewRequest("GET", "https://api.myanimelist.net/v2/users/@me", nil)
+ if err != nil {
+ return nil, fmt.Errorf("failed to create request: %w", err)
+ }
+ req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", config.GetConfig().MyAnimeList.AccessToken))
+
+ client := &http.Client{}
+ resp, err := client.Do(req)
+ if err != nil {
+ return nil, fmt.Errorf("failed to send request: %w", err)
+ }
+
+ defer resp.Body.Close()
+
+ if resp.StatusCode != http.StatusOK {
+ return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
+ }
+
+ if err := json.NewDecoder(resp.Body).Decode(&user); err != nil {
+ return nil, fmt.Errorf("failed to decode response: %w", err)
+ }
+
+ return &user, nil
+}