Conditions | 9 |
Total Lines | 51 |
Code Lines | 32 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | package oauth |
||
17 | func GetDiscordAccessToken(code string) (string, error) { |
||
18 | reqBody := bytes.NewBuffer([]byte(fmt.Sprintf( |
||
19 | "client_id=%s&client_secret=%s&grant_type=authorization_code&redirect_uri=%s&code=%s&scope=identify,email", |
||
20 | infrastructures.AppConfig.DiscordConfig.ClientID, |
||
21 | infrastructures.AppConfig.DiscordConfig.ClientSecret, |
||
22 | config.GetCurrentURL()+"/v2/security/discord_callback", |
||
23 | code, |
||
24 | ))) |
||
25 | |||
26 | // POST request to set URL |
||
27 | req, reqerr := http.NewRequestWithContext(context.Background(), |
||
28 | http.MethodPost, |
||
29 | "https://discord.com/api/oauth2/token", |
||
30 | reqBody, |
||
31 | ) |
||
32 | if reqerr != nil || req == nil || req.Body == nil || req.Header == nil { |
||
33 | log.Debug().Err(reqerr).Msg("discord.go: GetDiscordAccessToken: Request failed (reqerr != nil || req == nil || req.Body == nil || req.Header == nil)") |
||
34 | return "", errors.Wrap(reqerr, "get discord access token request failed") |
||
35 | } |
||
36 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
||
37 | req.Header.Set("Accept", "application/json") |
||
38 | |||
39 | // Get the response |
||
40 | resp, resperr := http.DefaultClient.Do(req) |
||
41 | if resperr != nil || resp == nil || resp.Body == nil { |
||
42 | log.Debug().Err(resperr).Msg("discord.go: GetDiscordAccessToken: Response failed (resperr != nil || resp == nil || resp.Body == nil)") |
||
43 | return "", errors.Wrap(resperr, "get discord access token response failed") |
||
44 | } |
||
45 | |||
46 | // Response body converted to stringified JSON |
||
47 | respbody, _ := io.ReadAll(resp.Body) |
||
48 | |||
49 | // Represents the response received from Github |
||
50 | type discordAccessTokenResponse struct { |
||
51 | AccessToken string `json:"access_token"` |
||
52 | TokenType string `json:"token_type"` |
||
53 | Scope string `json:"scope"` |
||
54 | Expires int `json:"expires_in"` |
||
55 | RefreshToken string `json:"refresh_token"` |
||
56 | } |
||
57 | |||
58 | // Convert stringified JSON to a struct object of type githubAccessTokenResponse |
||
59 | var ghresp discordAccessTokenResponse |
||
60 | err := config.JSONHelper.Unmarshal(respbody, &ghresp) |
||
61 | if err != nil { |
||
62 | return "", err |
||
63 | } |
||
64 | |||
65 | // Return the access token (as the rest of the |
||
66 | // details are relatively unnecessary for us) |
||
67 | return ghresp.AccessToken, nil |
||
68 | } |
||
96 |