Conditions | 11 |
Total Lines | 63 |
Code Lines | 39 |
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:
Complex classes like oauth.GetDiscordAccessToken often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | package oauth |
||
35 | func GetDiscordAccessToken(ctx context.Context, code string) (string, error) { |
||
36 | _, span := infrastructures.GetFiberTracer().Start(ctx, "GetDiscordAccessToken") |
||
37 | defer span.End() |
||
38 | |||
39 | reqBody := bytes.NewBuffer([]byte(fmt.Sprintf( |
||
40 | "client_id=%s&client_secret=%s&grant_type=authorization_code&redirect_uri=%s&code=%s&scope=identify,email", |
||
41 | infrastructures.AppConfig.DiscordConfig.ClientID, |
||
42 | infrastructures.AppConfig.DiscordConfig.ClientSecret, |
||
43 | config.GetCurrentURL()+"/v2/security/discord_callback", |
||
44 | code, |
||
45 | ))) |
||
46 | |||
47 | // POST request to set URL |
||
48 | req, err := http.NewRequestWithContext(ctx, |
||
49 | http.MethodPost, |
||
50 | discordTokenURL, |
||
51 | reqBody, |
||
52 | ) |
||
53 | if err != nil { |
||
54 | otelzap.Ctx(ctx).Error("Failed to get Discord access token", zap.Error(err)) |
||
55 | return "", errors.Wrap(err, views.RequestFailed) |
||
56 | } |
||
57 | |||
58 | if req == nil || req.Body == nil || req.Header == nil { |
||
59 | otelzap.Ctx(ctx).Error("Failed to get Discord access token", zap.Error(err)) |
||
60 | return "", errors.New(views.RequestFailed) |
||
61 | } |
||
62 | |||
63 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
||
64 | req.Header.Set("Accept", "application/json") |
||
65 | |||
66 | // Get the response |
||
67 | resp, resperr := http.DefaultClient.Do(req) |
||
68 | if resperr != nil { |
||
69 | otelzap.Ctx(ctx).Error("Failed to get Discord access token", zap.Error(resperr)) |
||
70 | return "", errors.Wrap(resperr, views.ResponseFailed) |
||
71 | } |
||
72 | |||
73 | defer func(resp *http.Response) { |
||
74 | if resp != nil && resp.Body != nil { |
||
75 | resp.Body.Close() |
||
76 | } |
||
77 | }(resp) |
||
78 | |||
79 | // Response body converted to stringified JSON |
||
80 | respbody, err := io.ReadAll(resp.Body) |
||
81 | if err != nil { |
||
82 | otelzap.Ctx(ctx).Error("failed to read resp.body", zap.Error(err)) |
||
83 | return "", err |
||
84 | } |
||
85 | |||
86 | // Convert stringified JSON to a struct object of type githubAccessTokenResponse |
||
87 | var ghresp discordAccessTokenResponse |
||
88 | err = config.JSONHelper.Unmarshal(respbody, &ghresp) |
||
89 | if err != nil { |
||
90 | return "", err |
||
91 | } |
||
92 | |||
93 | span.AddEvent("Discord access token received", trace.WithAttributes(attribute.String("access_token", ghresp.AccessToken))) |
||
94 | |||
95 | // Return the access token (as the rest of the |
||
96 | // details are relatively unnecessary for us) |
||
97 | return ghresp.AccessToken, nil |
||
98 | } |
||
134 |