support for request methods

This commit is contained in:
Minecon724 2024-08-06 15:10:59 +02:00
parent 72e0e2a912
commit 6da971ef21
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8

View file

@ -2,6 +2,7 @@ package eu.m724.vastapp.vastai
import eu.m724.vastapp.BuildConfig import eu.m724.vastapp.BuildConfig
import org.chromium.net.CronetEngine import org.chromium.net.CronetEngine
import org.chromium.net.UploadDataProvider
import org.chromium.net.UrlRequest import org.chromium.net.UrlRequest
import java.util.concurrent.Executor import java.util.concurrent.Executor
@ -16,15 +17,28 @@ class VastApi(
* *
* @param endpoint the endpoint path starting with a slash like /users/current * @param endpoint the endpoint path starting with a slash like /users/current
* @param callback any callback for example [UserUrlRequestCallback] * @param callback any callback for example [UserUrlRequestCallback]
* @param method request method, default GET
* @param uploadDataProvider [UploadDataProvider] if request sends data
* @return an [UrlRequest] you must .start() yourself * @return an [UrlRequest] you must .start() yourself
*/ */
fun buildRequest(endpoint: String, callback: UrlRequest.Callback): UrlRequest { fun buildRequest(
val requestBuilder = cronetEngine.newUrlRequestBuilder( endpoint: String,
callback: UrlRequest.Callback,
method: String = "GET",
uploadDataProvider: UploadDataProvider?
): UrlRequest {
var requestBuilder = cronetEngine.newUrlRequestBuilder(
BuildConfig.VASIAI_API_ENDPOINT + endpoint, BuildConfig.VASIAI_API_ENDPOINT + endpoint,
callback, callback,
executor executor
).addHeader("Authorization", "Bearer $apiKey") ).addHeader("Authorization", "Bearer $apiKey")
requestBuilder = requestBuilder.setHttpMethod(method)
if (uploadDataProvider != null) {
requestBuilder = requestBuilder.setUploadDataProvider(uploadDataProvider, executor)
}
return requestBuilder.build() return requestBuilder.build()
} }
@ -34,9 +48,10 @@ class VastApi(
* *
* @param apiRoute the api route * @param apiRoute the api route
* @param callback any callback for example [UserUrlRequestCallback] * @param callback any callback for example [UserUrlRequestCallback]
* @param uploadDataProvider [UploadDataProvider] if request sends data
* @return an [UrlRequest] you must .start() yourself * @return an [UrlRequest] you must .start() yourself
*/ */
fun buildRequest(apiRoute: ApiRoute, callback: UrlRequest.Callback): UrlRequest { fun buildRequest(apiRoute: ApiRoute, callback: UrlRequest.Callback, uploadDataProvider: UploadDataProvider? = null): UrlRequest {
return buildRequest(apiRoute.path, callback) return buildRequest(apiRoute.path, callback, apiRoute.method, uploadDataProvider)
} }
} }