Add support for POST with json body

Koitharu 3 years ago
parent 749f682ef9
commit 1b6d1456f3
No known key found for this signature in database
GPG Key ID: 8E861F8CE6E7CE27

@ -61,18 +61,22 @@ class OkHttpWebClient(
return httpClient.newCall(request.build()).await().ensureSuccess()
}
override suspend fun graphQLQuery(endpoint: String, query: String): JSONObject {
val body = JSONObject()
body.put("operationName", null as Any?)
body.put("variables", JSONObject())
body.put("query", "{$query}")
override suspend fun httpPost(url: HttpUrl, body: JSONObject): Response {
val mediaType = "application/json; charset=utf-8".toMediaType()
val requestBody = body.toString().toRequestBody(mediaType)
val request = Request.Builder()
.post(requestBody)
.url(endpoint)
.url(url)
.addTags()
val json = httpClient.newCall(request.build()).await().ensureSuccess().parseJson()
return httpClient.newCall(request.build()).await().ensureSuccess()
}
override suspend fun graphQLQuery(endpoint: String, query: String): JSONObject {
val body = JSONObject()
body.put("operationName", null as Any?)
body.put("variables", JSONObject())
body.put("query", "{$query}")
val json = httpPost(endpoint, body).parseJson()
json.optJSONArray("errors")?.let {
if (it.length() != 0) {
throw GraphQLException(it)
@ -82,9 +86,7 @@ class OkHttpWebClient(
}
private fun Request.Builder.addTags(): Request.Builder {
if (mangaSource != null) {
tag(MangaSource::class.java, mangaSource)
}
return this
}

@ -59,6 +59,20 @@ interface WebClient {
*/
suspend fun httpPost(url: HttpUrl, payload: String): Response
/**
* Do a POST http request to specific url with json payload
* @param url
* @param body
*/
suspend fun httpPost(url: String, body: JSONObject): Response = httpPost(url.toHttpUrl(), body)
/**
* Do a POST http request to specific url with json payload
* @param url
* @param body
*/
suspend fun httpPost(url: HttpUrl, body: JSONObject): Response
/**
* Do a GraphQL request to specific url
* @param endpoint an url

Loading…
Cancel
Save