aboutsummaryrefslogtreecommitdiff
path: root/lib/user.go
diff options
context:
space:
mode:
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
+}