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

@ -12,96 +12,98 @@ import org.koitharu.kotatsu.parsers.util.await
import org.koitharu.kotatsu.parsers.util.parseJson import org.koitharu.kotatsu.parsers.util.parseJson
class OkHttpWebClient( class OkHttpWebClient(
private val httpClient: OkHttpClient, private val httpClient: OkHttpClient,
private val mangaSource: MangaSource, private val mangaSource: MangaSource,
) : WebClient { ) : WebClient {
override suspend fun httpGet(url: HttpUrl): Response { override suspend fun httpGet(url: HttpUrl): Response {
val request = Request.Builder() val request = Request.Builder()
.get() .get()
.url(url) .url(url)
.addTags() .addTags()
return httpClient.newCall(request.build()).await().ensureSuccess() return httpClient.newCall(request.build()).await().ensureSuccess()
} }
override suspend fun httpHead(url: HttpUrl): Response { override suspend fun httpHead(url: HttpUrl): Response {
val request = Request.Builder() val request = Request.Builder()
.head() .head()
.url(url) .url(url)
.addTags() .addTags()
return httpClient.newCall(request.build()).await().ensureSuccess() return httpClient.newCall(request.build()).await().ensureSuccess()
} }
override suspend fun httpPost(url: HttpUrl, form: Map<String, String>): Response { override suspend fun httpPost(url: HttpUrl, form: Map<String, String>): Response {
val body = FormBody.Builder() val body = FormBody.Builder()
form.forEach { (k, v) -> form.forEach { (k, v) ->
body.addEncoded(k, v) body.addEncoded(k, v)
} }
val request = Request.Builder() val request = Request.Builder()
.post(body.build()) .post(body.build())
.url(url) .url(url)
.addTags() .addTags()
return httpClient.newCall(request.build()).await().ensureSuccess() return httpClient.newCall(request.build()).await().ensureSuccess()
} }
override suspend fun httpPost(url: HttpUrl, payload: String): Response { override suspend fun httpPost(url: HttpUrl, payload: String): Response {
val body = FormBody.Builder() val body = FormBody.Builder()
payload.split('&').forEach { payload.split('&').forEach {
val pos = it.indexOf('=') val pos = it.indexOf('=')
if (pos != -1) { if (pos != -1) {
val k = it.substring(0, pos) val k = it.substring(0, pos)
val v = it.substring(pos + 1) val v = it.substring(pos + 1)
body.addEncoded(k, v) body.addEncoded(k, v)
} }
} }
val request = Request.Builder() val request = Request.Builder()
.post(body.build()) .post(body.build())
.url(url) .url(url)
.addTags() .addTags()
return httpClient.newCall(request.build()).await().ensureSuccess() return httpClient.newCall(request.build()).await().ensureSuccess()
} }
override suspend fun graphQLQuery(endpoint: String, query: String): JSONObject { override suspend fun httpPost(url: HttpUrl, body: JSONObject): Response {
val body = JSONObject() val mediaType = "application/json; charset=utf-8".toMediaType()
body.put("operationName", null as Any?) val requestBody = body.toString().toRequestBody(mediaType)
body.put("variables", JSONObject()) val request = Request.Builder()
body.put("query", "{$query}") .post(requestBody)
val mediaType = "application/json; charset=utf-8".toMediaType() .url(url)
val requestBody = body.toString().toRequestBody(mediaType) .addTags()
val request = Request.Builder() return httpClient.newCall(request.build()).await().ensureSuccess()
.post(requestBody) }
.url(endpoint)
.addTags()
val json = httpClient.newCall(request.build()).await().ensureSuccess().parseJson()
json.optJSONArray("errors")?.let {
if (it.length() != 0) {
throw GraphQLException(it)
}
}
return json
}
private fun Request.Builder.addTags(): Request.Builder { override suspend fun graphQLQuery(endpoint: String, query: String): JSONObject {
if (mangaSource != null) { val body = JSONObject()
tag(MangaSource::class.java, mangaSource) body.put("operationName", null as Any?)
} body.put("variables", JSONObject())
return this body.put("query", "{$query}")
} val json = httpPost(endpoint, body).parseJson()
json.optJSONArray("errors")?.let {
if (it.length() != 0) {
throw GraphQLException(it)
}
}
return json
}
private fun Response.ensureSuccess(): Response { private fun Request.Builder.addTags(): Request.Builder {
val exception: Exception? = when (code) { // Catch some error codes, not all tag(MangaSource::class.java, mangaSource)
404 -> NotFoundException(message, request.url.toString()) return this
in 500..599 -> HttpStatusException(message, code, request.url.toString()) }
else -> null
} private fun Response.ensureSuccess(): Response {
if (exception != null) { val exception: Exception? = when (code) { // Catch some error codes, not all
runCatching { 404 -> NotFoundException(message, request.url.toString())
close() in 500..599 -> HttpStatusException(message, code, request.url.toString())
}.onFailure { else -> null
exception.addSuppressed(it) }
} if (exception != null) {
throw exception runCatching {
} close()
return this }.onFailure {
} exception.addSuppressed(it)
}
throw exception
}
return this
}
} }

@ -7,62 +7,76 @@ import org.json.JSONObject
interface WebClient { interface WebClient {
/** /**
* Do a GET http request to specific url * Do a GET http request to specific url
* @param url * @param url
*/ */
suspend fun httpGet(url: String): Response = httpGet(url.toHttpUrl()) suspend fun httpGet(url: String): Response = httpGet(url.toHttpUrl())
/** /**
* Do a GET http request to specific url * Do a GET http request to specific url
* @param url * @param url
*/ */
suspend fun httpGet(url: HttpUrl): Response suspend fun httpGet(url: HttpUrl): Response
/** /**
* Do a HEAD http request to specific url * Do a HEAD http request to specific url
* @param url * @param url
*/ */
suspend fun httpHead(url: String): Response = httpHead(url.toHttpUrl()) suspend fun httpHead(url: String): Response = httpHead(url.toHttpUrl())
/** /**
* Do a HEAD http request to specific url * Do a HEAD http request to specific url
* @param url * @param url
*/ */
suspend fun httpHead(url: HttpUrl): Response suspend fun httpHead(url: HttpUrl): Response
/** /**
* Do a POST http request to specific url with `multipart/form-data` payload * Do a POST http request to specific url with `multipart/form-data` payload
* @param url * @param url
* @param form payload as key=>value map * @param form payload as key=>value map
*/ */
suspend fun httpPost(url: String, form: Map<String, String>): Response = httpPost(url.toHttpUrl(), form) suspend fun httpPost(url: String, form: Map<String, String>): Response = httpPost(url.toHttpUrl(), form)
/** /**
* Do a POST http request to specific url with `multipart/form-data` payload * Do a POST http request to specific url with `multipart/form-data` payload
* @param url * @param url
* @param form payload as key=>value map * @param form payload as key=>value map
*/ */
suspend fun httpPost(url: HttpUrl, form: Map<String, String>): Response suspend fun httpPost(url: HttpUrl, form: Map<String, String>): Response
/** /**
* Do a POST http request to specific url with `multipart/form-data` payload * Do a POST http request to specific url with `multipart/form-data` payload
* @param url * @param url
* @param payload payload as `key=value` string with `&` separator * @param payload payload as `key=value` string with `&` separator
*/ */
suspend fun httpPost(url: String, payload: String): Response = httpPost(url.toHttpUrl(), payload) suspend fun httpPost(url: String, payload: String): Response = httpPost(url.toHttpUrl(), payload)
/** /**
* Do a POST http request to specific url with `multipart/form-data` payload * Do a POST http request to specific url with `multipart/form-data` payload
* @param url * @param url
* @param payload payload as `key=value` string with `&` separator * @param payload payload as `key=value` string with `&` separator
*/ */
suspend fun httpPost(url: HttpUrl, payload: String): Response suspend fun httpPost(url: HttpUrl, payload: String): Response
/** /**
* Do a GraphQL request to specific url * Do a POST http request to specific url with json payload
* @param endpoint an url * @param url
* @param query GraphQL request payload * @param body
*/ */
suspend fun graphQLQuery(endpoint: String, query: String): JSONObject 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
* @param query GraphQL request payload
*/
suspend fun graphQLQuery(endpoint: String, query: String): JSONObject
} }

Loading…
Cancel
Save