Conditions | 16 |
Total Lines | 78 |
Code Lines | 54 |
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 providers.*CloudSSOCredentialsProvider.getCredentials 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 providers |
||
100 | func (provider *CloudSSOCredentialsProvider) getCredentials() (session *sessionCredentials, err error) { |
||
101 | url, err := url.Parse(provider.signInUrl) |
||
102 | if err != nil { |
||
103 | return nil, err |
||
104 | } |
||
105 | |||
106 | req := &httputil.Request{ |
||
107 | Method: "POST", |
||
108 | Protocol: url.Scheme, |
||
109 | Host: url.Host, |
||
110 | Path: "/cloud-credentials", |
||
111 | Headers: map[string]string{}, |
||
112 | } |
||
113 | |||
114 | connectTimeout := 5 * time.Second |
||
115 | readTimeout := 10 * time.Second |
||
116 | |||
117 | if provider.httpOptions != nil && provider.httpOptions.ConnectTimeout > 0 { |
||
118 | connectTimeout = time.Duration(provider.httpOptions.ConnectTimeout) * time.Millisecond |
||
119 | } |
||
120 | if provider.httpOptions != nil && provider.httpOptions.ReadTimeout > 0 { |
||
121 | readTimeout = time.Duration(provider.httpOptions.ReadTimeout) * time.Millisecond |
||
122 | } |
||
123 | if provider.httpOptions != nil && provider.httpOptions.Proxy != "" { |
||
124 | req.Proxy = provider.httpOptions.Proxy |
||
125 | } |
||
126 | req.ConnectTimeout = connectTimeout |
||
127 | req.ReadTimeout = readTimeout |
||
128 | |||
129 | body := cloudCredentialOptions{ |
||
130 | AccountId: provider.accountId, |
||
131 | AccessConfigurationId: provider.accessConfig, |
||
132 | } |
||
133 | |||
134 | bodyBytes, err := json.Marshal(body) |
||
135 | if err != nil { |
||
136 | return nil, fmt.Errorf("failed to marshal options: %w", err) |
||
137 | } |
||
138 | |||
139 | req.Body = bodyBytes |
||
140 | |||
141 | // set headers |
||
142 | req.Headers["Accept"] = "application/json" |
||
143 | req.Headers["Content-Type"] = "application/json" |
||
144 | req.Headers["Authorization"] = fmt.Sprintf("Bearer %s", provider.accessToken) |
||
145 | res, err := httpDo(req) |
||
146 | if err != nil { |
||
147 | return |
||
148 | } |
||
149 | |||
150 | if res.StatusCode != http.StatusOK { |
||
151 | message := "get session token from sso failed: " |
||
152 | err = errors.New(message + string(res.Body)) |
||
153 | return |
||
154 | } |
||
155 | var data cloudCredentialResponse |
||
156 | err = json.Unmarshal(res.Body, &data) |
||
157 | if err != nil { |
||
158 | err = fmt.Errorf("get session token from sso failed, json.Unmarshal fail: %s", err.Error()) |
||
159 | return |
||
160 | } |
||
161 | if data.CloudCredential == nil { |
||
162 | err = fmt.Errorf("get session token from sso failed, fail to get credentials") |
||
163 | return |
||
164 | } |
||
165 | |||
166 | if data.CloudCredential.AccessKeyId == "" || data.CloudCredential.AccessKeySecret == "" || data.CloudCredential.SecurityToken == "" { |
||
167 | err = fmt.Errorf("refresh session token err, fail to get credentials") |
||
168 | return |
||
169 | } |
||
170 | |||
171 | session = &sessionCredentials{ |
||
172 | AccessKeyId: data.CloudCredential.AccessKeyId, |
||
173 | AccessKeySecret: data.CloudCredential.AccessKeySecret, |
||
174 | SecurityToken: data.CloudCredential.SecurityToken, |
||
175 | Expiration: data.CloudCredential.Expiration, |
||
176 | } |
||
177 | return |
||
178 | } |
||
217 |