| Conditions | 12 |
| Total Lines | 52 |
| Code Lines | 41 |
| 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 credentials.doAction 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 credentials |
||
| 170 | func doAction(request *request.CommonRequest, runtime *utils.Runtime) (content []byte, err error) { |
||
| 171 | httpRequest, err := http.NewRequest(request.Method, request.URL, strings.NewReader("")) |
||
| 172 | if err != nil { |
||
| 173 | return |
||
| 174 | } |
||
| 175 | httpRequest.Proto = "HTTP/1.1" |
||
| 176 | httpRequest.Host = request.Domain |
||
| 177 | debuglog("> %s %s %s", httpRequest.Method, httpRequest.URL.RequestURI(), httpRequest.Proto) |
||
| 178 | debuglog("> Host: %s", httpRequest.Host) |
||
| 179 | for key, value := range request.Headers { |
||
| 180 | if value != "" { |
||
| 181 | debuglog("> %s: %s", key, value) |
||
| 182 | httpRequest.Header[key] = []string{value} |
||
| 183 | } |
||
| 184 | } |
||
| 185 | debuglog(">") |
||
| 186 | httpClient := &http.Client{} |
||
| 187 | httpClient.Timeout = time.Duration(runtime.ReadTimeout) * time.Second |
||
| 188 | proxy := &url.URL{} |
||
| 189 | if runtime.Proxy != "" { |
||
| 190 | proxy, err = url.Parse(runtime.Proxy) |
||
| 191 | if err != nil { |
||
| 192 | return |
||
| 193 | } |
||
| 194 | } |
||
| 195 | trans := &http.Transport{} |
||
| 196 | if proxy != nil && runtime.Proxy != "" { |
||
| 197 | trans.Proxy = http.ProxyURL(proxy) |
||
| 198 | } |
||
| 199 | trans.DialContext = utils.Timeout(time.Duration(runtime.ConnectTimeout) * time.Second) |
||
| 200 | httpClient.Transport = trans |
||
| 201 | httpResponse, err := hookDo(httpClient.Do)(httpRequest) |
||
| 202 | if err != nil { |
||
| 203 | return |
||
| 204 | } |
||
| 205 | debuglog("< %s %s", httpResponse.Proto, httpResponse.Status) |
||
| 206 | for key, value := range httpResponse.Header { |
||
| 207 | debuglog("< %s: %v", key, strings.Join(value, "")) |
||
| 208 | } |
||
| 209 | debuglog("<") |
||
| 210 | |||
| 211 | resp := &response.CommonResponse{} |
||
| 212 | err = hookParse(resp.ParseFromHTTPResponse(httpResponse)) |
||
| 213 | if err != nil { |
||
| 214 | return |
||
| 215 | } |
||
| 216 | debuglog("%s", resp.GetHTTPContentString()) |
||
| 217 | if resp.GetHTTPStatus() != http.StatusOK { |
||
| 218 | err = fmt.Errorf("httpStatus: %d, message = %s", resp.GetHTTPStatus(), resp.GetHTTPContentString()) |
||
| 219 | return |
||
| 220 | } |
||
| 221 | return resp.GetHTTPContentBytes(), nil |
||
| 222 | } |
||
| 223 |