-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpManager.kt
More file actions
54 lines (39 loc) · 1.32 KB
/
Copy pathHttpManager.kt
File metadata and controls
54 lines (39 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import khttp.get
import khttp.patch
import khttp.post
import khttp.responses.Response
import org.json.JSONObject
/**
* Created by agreon on 30.05.17.
*/
class HttpManager {
var headers: Map<String, String> = mapOf()
fun setAuthToken(token: String){
headers = mapOf<String, String>("Authorization" to token)
}
fun httpPost(url: String, parameters: JSONObject): Response {
val response = post(url, headers, mapOf(), parameters)
return handleResponse(response)
}
fun httpGet(url: String, parameters: Map<String, String> = mapOf()): Response {
val response = get(url, mapOf(), parameters)
return handleResponse(response)
}
fun httpPatch(url: String, params: JSONObject): Response {
val response = patch(url, headers, mapOf(), params)
return handleResponse(response)
}
/**
* Git-Issue[186]: Add More Error-handling [improvement]
*/
private fun handleResponse(response: Response): Response {
if(response.statusCode >= 400){
val retObj = response.jsonObject
if(response.statusCode == 403){
throw Exception("Sorry, the max request limmit is exceeded..im working on that")
}
throw Exception(retObj.getString("message"))
}
return response
}
}