| Conditions | 6 |
| Total Lines | 56 |
| Code Lines | 35 |
| 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 |
||
| 16 | func GetDiscordAccessToken(code string) (string, error) { |
||
| 17 | reqBody := bytes.NewBuffer([]byte(fmt.Sprintf( |
||
| 18 | "client_id=%s&client_secret=%s&grant_type=authorization_code&redirect_uri=%s&code=%s&scope=identify,email", |
||
| 19 | infrastructures.AppConfig.DiscordConfig.ClientID, |
||
| 20 | infrastructures.AppConfig.DiscordConfig.ClientSecret, |
||
| 21 | config.GetCurrentURL()+"/v2/security/discord_callback", |
||
| 22 | code, |
||
| 23 | ))) |
||
| 24 | |||
| 25 | // POST request to set URL |
||
| 26 | req, reqerr := http.NewRequestWithContext(context.Background(), |
||
| 27 | http.MethodPost, |
||
| 28 | "https://discord.com/api/oauth2/token", |
||
| 29 | reqBody, |
||
| 30 | ) |
||
| 31 | if reqerr != nil { |
||
| 32 | log.Info().Msg("Request failed") |
||
| 33 | } |
||
| 34 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
||
| 35 | req.Header.Set("Accept", "application/json") |
||
| 36 | |||
| 37 | // Get the response |
||
| 38 | resp, resperr := http.DefaultClient.Do(req) |
||
| 39 | if resperr != nil { |
||
| 40 | log.Info().Msg("Response failed") |
||
| 41 | } |
||
| 42 | |||
| 43 | defer func(Body io.ReadCloser) { |
||
| 44 | err := Body.Close() |
||
| 45 | if err != nil { |
||
| 46 | log.Info().Msg("Body close failed") |
||
| 47 | } |
||
| 48 | }(resp.Body) |
||
| 49 | |||
| 50 | // Response body converted to stringified JSON |
||
| 51 | respbody, _ := io.ReadAll(resp.Body) |
||
| 52 | |||
| 53 | // Represents the response received from Github |
||
| 54 | type discordAccessTokenResponse struct { |
||
| 55 | AccessToken string `json:"access_token"` |
||
| 56 | TokenType string `json:"token_type"` |
||
| 57 | Scope string `json:"scope"` |
||
| 58 | Expires int `json:"expires_in"` |
||
| 59 | RefreshToken string `json:"refresh_token"` |
||
| 60 | } |
||
| 61 | |||
| 62 | // Convert stringified JSON to a struct object of type githubAccessTokenResponse |
||
| 63 | var ghresp discordAccessTokenResponse |
||
| 64 | err := config.JSONHelper.Unmarshal(respbody, &ghresp) |
||
| 65 | if err != nil { |
||
| 66 | return "", err |
||
| 67 | } |
||
| 68 | |||
| 69 | // Return the access token (as the rest of the |
||
| 70 | // details are relatively unnecessary for us) |
||
| 71 | return ghresp.AccessToken, nil |
||
| 72 | } |
||
| 104 |