1
1
package codecheck .github .api
2
2
3
- import com .ning .http .client .AsyncHttpClient
4
- import com .ning .http .client .AsyncCompletionHandler
5
- import com .ning .http .client .Response
6
- import com .ning .http .client .RequestBuilder
7
3
import java .net .URLEncoder
8
4
import scala .concurrent .Promise
9
5
import scala .concurrent .Future
@@ -12,8 +8,9 @@ import org.json4s.DefaultFormats
12
8
import java .util .UUID
13
9
import codecheck .github .models .AccessToken
14
10
import codecheck .github .exceptions .OAuthAPIException
11
+ import codecheck .github .transport ._
15
12
16
- class OAuthAPI (clientId : String , clientSecret : String , redirectUri : String , client : AsyncHttpClient ) {
13
+ class OAuthAPI (clientId : String , clientSecret : String , redirectUri : String , client : Transport ) {
17
14
private implicit val format = DefaultFormats
18
15
19
16
private val accessRequestUri = " https://github.com/login/oauth/authorize"
@@ -38,33 +35,31 @@ class OAuthAPI(clientId: String, clientSecret: String, redirectUri: String, clie
38
35
" code" -> code,
39
36
" redirect_uri" -> redirectUri
40
37
)
41
- val builder : RequestBuilder = new RequestBuilder ( " POST " )
38
+ val request = client.preparePost(tokenRequestUri )
42
39
.setHeader(" Content-Type" , " application/x-www-form-urlencoded" )
43
40
.setHeader(" Accept" , " application/json" )
44
- .setFollowRedirects(true )
45
- .setUrl(tokenRequestUri)
46
- params.foreach { case (k, v) => builder.addFormParam(k, v) }
41
+ .setFollowRedirect(true )
42
+ params.foreach { case (k, v) => request.addFormParam(k, v) }
47
43
48
44
val deferred = Promise [AccessToken ]()
49
- client.prepareRequest(builder.build). execute(new AsyncCompletionHandler [ Response ] () {
45
+ request. execute(new CompletionHandler () {
50
46
def onCompleted (res : Response ) = {
51
- val json = JsonMethods .parse(res.getResponseBody(" utf-8" ))
47
+ val body = res.getResponseBody.getOrElse(" {\" error\" : \" No response\" }" )
48
+ val json = JsonMethods .parse(body)
52
49
(json \ " error" ).toOption match {
53
50
case Some (_) => deferred.failure(new OAuthAPIException (json))
54
51
case None => deferred.success(AccessToken (json))
55
52
}
56
- res
57
53
}
58
- override def onThrowable (t : Throwable ) {
54
+ def onThrowable (t : Throwable ) {
59
55
deferred.failure(t)
60
- super .onThrowable(t)
61
56
}
62
57
})
63
58
deferred.future
64
59
}
65
60
}
66
61
67
62
object OAuthAPI {
68
- def apply (clientId : String , clientSecret : String , redirectUri : String )(implicit client : AsyncHttpClient ) = new OAuthAPI (clientId, clientSecret, redirectUri, client)
63
+ def apply (clientId : String , clientSecret : String , redirectUri : String )(implicit client : Transport ) = new OAuthAPI (clientId, clientSecret, redirectUri, client)
69
64
70
65
}
0 commit comments