@@ -4,17 +4,19 @@ import com.codacy.client.bitbucket.util.HTTPStatusCodes
4
4
import com .ning .http .client .AsyncHttpClient
5
5
import play .api .libs .json .{JsValue , Json , Reads }
6
6
import play .api .libs .oauth ._
7
- import play .api .libs .ws .WSClient
8
7
import play .api .libs .ws .ning .NingWSClient
9
8
10
9
import scala .concurrent .Await
11
- import scala .concurrent .duration ._
10
+ import scala .concurrent .duration .{ Duration , SECONDS }
12
11
13
12
class BitbucketClient (key : String , secretKey : String , token : String , secretToken : String ) {
14
13
15
14
private lazy val KEY = ConsumerKey (key, secretKey)
16
15
private lazy val TOKEN = RequestToken (token, secretToken)
17
16
17
+ private lazy val requestTimeout = Duration (10 , SECONDS )
18
+ private lazy val requestSigner = OAuthCalculator (KEY , TOKEN )
19
+
18
20
/*
19
21
* Does an API request and parses the json output into a class
20
22
*/
@@ -49,10 +51,13 @@ class BitbucketClient(key: String, secretKey: String, token: String, secretToken
49
51
* Does an API post
50
52
*/
51
53
def post [T ](request : Request [T ], values : JsValue )(implicit reader : Reads [T ]): RequestResponse [T ] = {
52
- val client : WSClient = new NingWSClient (new AsyncHttpClient ().getConfig)
54
+ val client = new NingWSClient (new AsyncHttpClient ().getConfig)
53
55
54
- val jpromise = client.url(request.url).sign(OAuthCalculator (KEY , TOKEN )).withFollowRedirects(follow = true ).post(values)
55
- val result = Await .result(jpromise, Duration (10 , SECONDS ))
56
+ val jpromise = client.url(request.url)
57
+ .sign(requestSigner)
58
+ .withFollowRedirects(follow = true )
59
+ .post(values)
60
+ val result = Await .result(jpromise, requestTimeout)
56
61
57
62
val value = if (Seq (HTTPStatusCodes .OK , HTTPStatusCodes .CREATED ).contains(result.status)) {
58
63
val body = result.body
@@ -67,36 +72,48 @@ class BitbucketClient(key: String, secretKey: String, token: String, secretToken
67
72
} else {
68
73
RequestResponse [T ](None , result.statusText, hasError = true )
69
74
}
75
+
76
+ client.close()
70
77
value
71
78
}
72
79
73
80
/* copy paste from post ... */
74
81
def delete [T ](url : String ): RequestResponse [Boolean ] = {
75
- val client : WSClient = new NingWSClient (new AsyncHttpClient ().getConfig)
82
+ val client = new NingWSClient (new AsyncHttpClient ().getConfig)
76
83
77
- val jpromise = client.url(url).sign(OAuthCalculator (KEY , TOKEN )).withFollowRedirects(follow = true ).delete()
78
- val result = Await .result(jpromise, Duration (10 , SECONDS ))
84
+ val jpromise = client.url(url)
85
+ .sign(requestSigner)
86
+ .withFollowRedirects(follow = true )
87
+ .delete()
88
+ val result = Await .result(jpromise, requestTimeout)
79
89
80
90
val value = if (Seq (HTTPStatusCodes .OK , HTTPStatusCodes .CREATED , HTTPStatusCodes .NO_CONTENT ).contains(result.status)) {
81
91
RequestResponse (Option (true ))
82
92
} else {
83
93
RequestResponse [Boolean ](None , result.statusText, hasError = true )
84
94
}
95
+
96
+ client.close()
85
97
value
86
98
}
87
99
88
100
private def get (url : String ): Either [ResponseError , JsValue ] = {
89
- val client : WSClient = new NingWSClient (new AsyncHttpClient ().getConfig)
101
+ val client = new NingWSClient (new AsyncHttpClient ().getConfig)
90
102
91
- val jpromise = client.url(url).sign(OAuthCalculator (KEY , TOKEN )).withFollowRedirects(follow = true ).get()
92
- val result = Await .result(jpromise, Duration (10 , SECONDS ))
103
+ val jpromise = client.url(url)
104
+ .sign(requestSigner)
105
+ .withFollowRedirects(follow = true )
106
+ .get()
107
+ val result = Await .result(jpromise, requestTimeout)
93
108
94
109
val value = if (Seq (HTTPStatusCodes .OK , HTTPStatusCodes .CREATED ).contains(result.status)) {
95
110
val body = result.body
96
111
parseJson(body)
97
112
} else {
98
113
Left (ResponseError (java.util.UUID .randomUUID().toString, result.statusText, result.statusText))
99
114
}
115
+
116
+ client.close()
100
117
value
101
118
}
102
119
0 commit comments