| Conditions | 13 |
| Total Lines | 56 |
| Code Lines | 44 |
| 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.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 providers |
||
| 34 | func doAction(request *request.CommonRequest, runtime *utils.Runtime) (content []byte, err error) { |
||
| 35 | var urlEncoded string |
||
| 36 | if request.BodyParams != nil { |
||
| 37 | urlEncoded = utils.GetURLFormedMap(request.BodyParams) |
||
| 38 | } |
||
| 39 | httpRequest, err := http.NewRequest(request.Method, request.URL, strings.NewReader(urlEncoded)) |
||
| 40 | if err != nil { |
||
| 41 | return |
||
| 42 | } |
||
| 43 | httpRequest.Proto = "HTTP/1.1" |
||
| 44 | httpRequest.Host = request.Domain |
||
| 45 | debuglog("> %s %s %s", httpRequest.Method, httpRequest.URL.RequestURI(), httpRequest.Proto) |
||
| 46 | debuglog("> Host: %s", httpRequest.Host) |
||
| 47 | for key, value := range request.Headers { |
||
| 48 | if value != "" { |
||
| 49 | debuglog("> %s: %s", key, value) |
||
| 50 | httpRequest.Header[key] = []string{value} |
||
| 51 | } |
||
| 52 | } |
||
| 53 | debuglog(">") |
||
| 54 | httpClient := &http.Client{} |
||
| 55 | httpClient.Timeout = time.Duration(runtime.ReadTimeout) * time.Second |
||
| 56 | proxy := &url.URL{} |
||
| 57 | if runtime.Proxy != "" { |
||
| 58 | proxy, err = url.Parse(runtime.Proxy) |
||
| 59 | if err != nil { |
||
| 60 | return |
||
| 61 | } |
||
| 62 | } |
||
| 63 | trans := &http.Transport{} |
||
| 64 | if proxy != nil && runtime.Proxy != "" { |
||
| 65 | trans.Proxy = http.ProxyURL(proxy) |
||
| 66 | } |
||
| 67 | trans.DialContext = utils.Timeout(time.Duration(runtime.ConnectTimeout) * time.Second) |
||
| 68 | httpClient.Transport = trans |
||
| 69 | httpResponse, err := hookDo(httpClient.Do)(httpRequest) |
||
| 70 | if err != nil { |
||
| 71 | return |
||
| 72 | } |
||
| 73 | debuglog("< %s %s", httpResponse.Proto, httpResponse.Status) |
||
| 74 | for key, value := range httpResponse.Header { |
||
| 75 | debuglog("< %s: %v", key, strings.Join(value, "")) |
||
| 76 | } |
||
| 77 | debuglog("<") |
||
| 78 | |||
| 79 | resp := &response.CommonResponse{} |
||
| 80 | err = hookParse(resp.ParseFromHTTPResponse(httpResponse)) |
||
| 81 | if err != nil { |
||
| 82 | return |
||
| 83 | } |
||
| 84 | debuglog("%s", resp.GetHTTPContentString()) |
||
| 85 | if resp.GetHTTPStatus() != http.StatusOK { |
||
| 86 | err = fmt.Errorf("httpStatus: %d, message = %s", resp.GetHTTPStatus(), resp.GetHTTPContentString()) |
||
| 87 | return |
||
| 88 | } |
||
| 89 | return resp.GetHTTPContentBytes(), nil |
||
| 90 | } |
||
| 91 |