From e937974119b9fb3ec57470412cf1440263bbba82 Mon Sep 17 00:00:00 2001 From: Kamigen <46357922+Edouard127@users.noreply.github.com> Date: Sat, 11 Nov 2023 12:14:13 -0500 Subject: [PATCH] Refactor: Auth --- auth/data/auth.go | 89 ++++++++++++++--------------------------------- 1 file changed, 27 insertions(+), 62 deletions(-) diff --git a/auth/data/auth.go b/auth/data/auth.go index c6834148..2acc2419 100644 --- a/auth/data/auth.go +++ b/auth/data/auth.go @@ -25,6 +25,22 @@ type Auth struct { KeyPair } +func Do(req *http.Request) (*http.Response, error) { + req.Header.Set("User-Agent", "Minecraft") + resp, err := http.DefaultClient.Do(req) + switch resp.StatusCode { + case 401: + err = fmt.Errorf("invalid access token") + case 403: + err = fmt.Errorf("name is unavailable") + case 429: + err = fmt.Errorf("too many requests") + case 500: + err = fmt.Errorf("internal server error") + } + return resp, err +} + func (a *Auth) createRequest(method, path string, body io.Reader) *http.Request { req, err := http.NewRequest(method, MojangAPI+path, body) if err != nil { @@ -43,15 +59,11 @@ func (a *Auth) createRequest(method, path string, body io.Reader) *http.Request } func (a *Auth) NameAvailable(name string) (bool, error) { - resp, err := http.DefaultClient.Do(a.createRequest("GET", "/minecraft/profile/name/"+name+"/available", nil)) + resp, err := Do(a.createRequest("GET", "/minecraft/profile/name/"+name+"/available", nil)) if err != nil { return false, err } - if resp.StatusCode == 401 { - return false, fmt.Errorf("invalid access token") - } - var response struct { Status string `json:"status"` } @@ -62,75 +74,28 @@ func (a *Auth) NameAvailable(name string) (bool, error) { } func (a *Auth) ChangeName(name string) error { - resp, err := http.DefaultClient.Do(a.createRequest("PUT", "/minecraft/profile/name/"+name, nil)) - if err != nil { - return err - } - - switch resp.StatusCode { - case 401: - return fmt.Errorf("invalid access token") - case 403: - return fmt.Errorf("name is unavailable") - case 429: - return fmt.Errorf("too many requests") - case 500: - return fmt.Errorf("internal server error") - default: - return nil - } + _, err := Do(a.createRequest("PUT", "/minecraft/profile/name/"+name, nil)) + return err } func (a *Auth) ChangeSkin(variant, skinURL string) error { - resp, err := http.DefaultClient.Do(a.createRequest("POST", "/minecraft/profile/skins", strings.NewReader(fmt.Sprintf(`{"variant": "%s"', "url":"%s"}`, variant, skinURL)))) - if err != nil { - return err - } - - if resp.StatusCode == 401 { - return fmt.Errorf("invalid access token") - } - - return nil + _, err := Do(a.createRequest("POST", "/minecraft/profile/skins", strings.NewReader(fmt.Sprintf(`{"variant": "%s"', "url":"%s"}`, variant, skinURL)))) + return err } func (a *Auth) ResetSkin() error { - resp, err := http.DefaultClient.Do(a.createRequest("DELETE", "/minecraft/profile/skins/active", nil)) - if err != nil { - return err - } - - if resp.StatusCode == 401 { - return fmt.Errorf("invalid access token") - } - - return nil + _, err := Do(a.createRequest("DELETE", "/minecraft/profile/skins/active", nil)) + return err } func (a *Auth) HideCape() error { - resp, err := http.DefaultClient.Do(a.createRequest("DELETE", "/minecraft/profile/capes/active", nil)) - if err != nil { - return err - } - - if resp.StatusCode == 401 { - return fmt.Errorf("invalid access token") - } - - return nil + _, err := Do(a.createRequest("DELETE", "/minecraft/profile/capes/active", nil)) + return err } func (a *Auth) ShowCape(capeid string) error { - resp, err := http.DefaultClient.Do(a.createRequest("POST", "/minecraft/profile/capes", strings.NewReader(fmt.Sprintf(`{"capeId":"%s"}`, capeid)))) - if err != nil { - return err - } - - if resp.StatusCode == 401 { - return fmt.Errorf("invalid access token") - } - - return nil + _, err := http.DefaultClient.Do(a.createRequest("POST", "/minecraft/profile/capes", strings.NewReader(fmt.Sprintf(`{"capeId":"%s"}`, capeid)))) + return err } func (a *Auth) SessionID() string {