@@ -2,20 +2,22 @@ package scommons.api.http
2
2
3
3
import play .api .libs .json ._
4
4
import scommons .api .http .ApiHttpClient ._
5
+ import scommons .api .http .ApiHttpMethod ._
5
6
6
- import scala .concurrent .{ExecutionContext , Future }
7
7
import scala .concurrent .duration ._
8
+ import scala .concurrent .{ExecutionContext , Future }
8
9
9
10
abstract class ApiHttpClient (baseUrl : String ,
10
- defaultTimeout : FiniteDuration = 30 .seconds)(implicit ec : ExecutionContext ) {
11
+ defaultTimeout : FiniteDuration = 30 .seconds
12
+ )(implicit ec : ExecutionContext ) {
11
13
12
14
def execGet [R ](url : String ,
13
15
params : List [(String , String )] = Nil ,
14
16
headers : List [(String , String )] = Nil ,
15
17
timeout : FiniteDuration = defaultTimeout
16
18
)(implicit jsonReads : Reads [R ]): Future [R ] = {
17
19
18
- exec[String , R ]( " GET" , url, params, headers, None , timeout)
20
+ exec[String ]( GET , url, params, headers, None , timeout).map(parseResponse[ R ] )
19
21
}
20
22
21
23
def execPost [D , R ](url : String ,
@@ -25,7 +27,7 @@ abstract class ApiHttpClient(baseUrl: String,
25
27
timeout : FiniteDuration = defaultTimeout
26
28
)(implicit jsonWrites : Writes [D ], jsonReads : Reads [R ]): Future [R ] = {
27
29
28
- exec(" POST" , url, params, headers, Some (data), timeout)
30
+ exec(POST , url, params, headers, Some (data), timeout).map(parseResponse[ R ] )
29
31
}
30
32
31
33
def execPut [D , R ](url : String ,
@@ -35,7 +37,7 @@ abstract class ApiHttpClient(baseUrl: String,
35
37
timeout : FiniteDuration = defaultTimeout
36
38
)(implicit jsonWrites : Writes [D ], jsonReads : Reads [R ]): Future [R ] = {
37
39
38
- exec(" PUT" , url, params, headers, Some (data), timeout)
40
+ exec(PUT , url, params, headers, Some (data), timeout).map(parseResponse[ R ] )
39
41
}
40
42
41
43
def execDelete [D , R ](url : String ,
@@ -45,29 +47,32 @@ abstract class ApiHttpClient(baseUrl: String,
45
47
timeout : FiniteDuration = defaultTimeout
46
48
)(implicit jsonWrites : Writes [D ], jsonReads : Reads [R ]): Future [R ] = {
47
49
48
- exec(" DELETE" , url, params, headers, data, timeout)
50
+ exec(DELETE , url, params, headers, data, timeout).map(parseResponse[ R ] )
49
51
}
50
52
51
- private def exec [T , R ](method : String ,
52
- url : String ,
53
- params : List [(String , String )],
54
- headers : List [(String , String )],
55
- data : Option [T ],
56
- timeout : FiniteDuration
57
- )(implicit jsonWrites : Writes [T ], jsonReads : Reads [ R ] ): Future [R ] = {
53
+ private def exec [T ](method : ApiHttpMethod ,
54
+ url : String ,
55
+ params : List [(String , String )],
56
+ headers : List [(String , String )],
57
+ data : Option [T ],
58
+ timeout : FiniteDuration
59
+ )(implicit jsonWrites : Writes [T ]): Future [ApiHttpResponse ] = {
58
60
59
61
val targetUrl = getTargetUrl(baseUrl, url)
60
62
61
63
execute(
62
- method = method,
64
+ method = method.toString ,
63
65
targetUrl = targetUrl,
64
66
params = params,
65
67
headers = headers,
66
68
jsonBody = data.map { d =>
67
69
Json .stringify(Json .toJson(d))
68
70
},
69
71
timeout = timeout
70
- ).map(parseResponse[R ](targetUrl, _))
72
+ ).map {
73
+ case None => throw ApiHttpTimeoutException (targetUrl)
74
+ case Some (resp) => resp
75
+ }
71
76
}
72
77
73
78
protected def execute (method : String ,
@@ -81,21 +86,17 @@ abstract class ApiHttpClient(baseUrl: String,
81
86
82
87
object ApiHttpClient {
83
88
84
- def parseResponse [R ](url : String , response : Option [ ApiHttpResponse ])
85
- ( implicit jsonReads : Reads [ R ]) : R = response match {
89
+ def parseResponse [R ](resp : ApiHttpResponse )( implicit jsonReads : Reads [ R ]) : R = {
90
+ val body = resp.body
86
91
87
- case None => throw ApiHttpTimeoutException (url)
88
-
89
- case Some (res) if res.status <= 299 =>
90
- val body = res.body
92
+ if (resp.status <= 299 ) {
91
93
Json .parse(body).validate[R ] match {
92
94
case JsSuccess (data, _) => data
93
95
case JsError (error) =>
94
- throw ApiHttpStatusException (s " Fail to parse http response, error: $error" , res )
96
+ throw ApiHttpStatusException (s " Fail to parse http response, error: $error" , resp )
95
97
}
96
-
97
- case Some (other) =>
98
- val body = other.body
98
+ }
99
+ else {
99
100
val maybeData =
100
101
if (body.trim.startsWith(" {" )) {
101
102
Json .parse(body).validate[R ] match {
@@ -107,8 +108,9 @@ object ApiHttpClient {
107
108
108
109
maybeData match {
109
110
case Some (data) => data
110
- case None => throw ApiHttpStatusException (" Received error response" , other )
111
+ case None => throw ApiHttpStatusException (" Received error response" , resp )
111
112
}
113
+ }
112
114
}
113
115
114
116
def queryParams (params : (String , Option [_])* ): List [(String , String )] = params.collect {
0 commit comments