| Conditions | 13 |
| Total Lines | 61 |
| Code Lines | 50 |
| 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.*RsaKeyPairCredential.updateCredential 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 |
||
| 82 | func (r *RsaKeyPairCredential) updateCredential() (err error) { |
||
| 83 | if r.runtime == nil { |
||
| 84 | r.runtime = new(utils.Runtime) |
||
| 85 | } |
||
| 86 | request := request.NewCommonRequest() |
||
| 87 | request.Domain = "sts.aliyuncs.com" |
||
| 88 | if r.runtime.Host != "" { |
||
| 89 | request.Domain = r.runtime.Host |
||
| 90 | } |
||
| 91 | request.Scheme = "HTTPS" |
||
| 92 | request.Method = "GET" |
||
| 93 | request.QueryParams["AccessKeyId"] = r.PublicKeyID |
||
| 94 | request.QueryParams["Action"] = "GenerateSessionAccessKey" |
||
| 95 | request.QueryParams["Format"] = "JSON" |
||
| 96 | if r.SessionExpiration > 0 { |
||
| 97 | if r.SessionExpiration >= 900 && r.SessionExpiration <= 3600 { |
||
| 98 | request.QueryParams["DurationSeconds"] = strconv.Itoa(r.SessionExpiration) |
||
| 99 | } else { |
||
| 100 | err = errors.New("[InvalidParam]:Key Pair session duration should be in the range of 15min - 1Hr") |
||
| 101 | return |
||
| 102 | } |
||
| 103 | } else { |
||
| 104 | request.QueryParams["DurationSeconds"] = strconv.Itoa(defaultDurationSeconds) |
||
| 105 | } |
||
| 106 | request.QueryParams["SignatureMethod"] = "SHA256withRSA" |
||
| 107 | request.QueryParams["SignatureType"] = "PRIVATEKEY" |
||
| 108 | request.QueryParams["SignatureVersion"] = "1.0" |
||
| 109 | request.QueryParams["Version"] = "2015-04-01" |
||
| 110 | request.QueryParams["Timestamp"] = utils.GetTimeInFormatISO8601() |
||
| 111 | request.QueryParams["SignatureNonce"] = utils.GetUUID() |
||
| 112 | signature := utils.Sha256WithRsa(request.BuildStringToSign(), r.PrivateKey) |
||
| 113 | request.QueryParams["Signature"] = signature |
||
| 114 | request.Headers["Host"] = request.Domain |
||
| 115 | request.Headers["Accept-Encoding"] = "identity" |
||
| 116 | request.Url = request.BuildUrl() |
||
| 117 | content, err := doAction(request, r.runtime) |
||
| 118 | if err != nil { |
||
| 119 | return fmt.Errorf("refresh KeyPair err: %s", err.Error()) |
||
| 120 | } |
||
| 121 | var resp *RsaKeyPairResponse |
||
| 122 | err = json.Unmarshal(content, &resp) |
||
| 123 | if err != nil { |
||
| 124 | return fmt.Errorf("refresh KeyPair err: Json Unmarshal fail: %s", err.Error()) |
||
| 125 | } |
||
| 126 | if resp == nil || resp.SessionAccessKey == nil { |
||
| 127 | return fmt.Errorf("refresh KeyPair err: SessionAccessKey is empty") |
||
| 128 | } |
||
| 129 | sessionAccessKey := resp.SessionAccessKey |
||
| 130 | if sessionAccessKey.SessionAccessKeyID == "" || sessionAccessKey.SessionAccessKeySecret == "" || sessionAccessKey.Expiration == "" { |
||
| 131 | return fmt.Errorf("refresh KeyPair err: SessionAccessKeyID: %v, SessionAccessKeySecret: %v, Expiration: %v", sessionAccessKey.SessionAccessKeyID, sessionAccessKey.SessionAccessKeySecret, sessionAccessKey.Expiration) |
||
| 132 | } |
||
| 133 | |||
| 134 | expirationTime, err := time.Parse("2006-01-02T15:04:05Z", sessionAccessKey.Expiration) |
||
| 135 | r.lastUpdateTimestamp = time.Now().Unix() |
||
| 136 | r.credentialExpiration = int(expirationTime.Unix() - time.Now().Unix()) |
||
| 137 | r.sessionCredential = &sessionCredential{ |
||
| 138 | AccessKeyID: sessionAccessKey.SessionAccessKeyID, |
||
| 139 | AccessKeySecret: sessionAccessKey.SessionAccessKeySecret, |
||
| 140 | } |
||
| 141 | |||
| 142 | return |
||
| 143 | } |
||
| 144 |