From 6da971ef211312d072585eb116287f1a953592b0 Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Tue, 6 Aug 2024 15:10:59 +0200 Subject: [PATCH] support for request methods --- .../java/eu/m724/vastapp/vastai/VastApi.kt | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/eu/m724/vastapp/vastai/VastApi.kt b/app/src/main/java/eu/m724/vastapp/vastai/VastApi.kt index 920a174..4fe4d5a 100644 --- a/app/src/main/java/eu/m724/vastapp/vastai/VastApi.kt +++ b/app/src/main/java/eu/m724/vastapp/vastai/VastApi.kt @@ -2,6 +2,7 @@ package eu.m724.vastapp.vastai import eu.m724.vastapp.BuildConfig import org.chromium.net.CronetEngine +import org.chromium.net.UploadDataProvider import org.chromium.net.UrlRequest import java.util.concurrent.Executor @@ -16,15 +17,28 @@ class VastApi( * * @param endpoint the endpoint path starting with a slash like /users/current * @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 */ - fun buildRequest(endpoint: String, callback: UrlRequest.Callback): UrlRequest { - val requestBuilder = cronetEngine.newUrlRequestBuilder( + fun buildRequest( + endpoint: String, + callback: UrlRequest.Callback, + method: String = "GET", + uploadDataProvider: UploadDataProvider? + ): UrlRequest { + var requestBuilder = cronetEngine.newUrlRequestBuilder( BuildConfig.VASIAI_API_ENDPOINT + endpoint, callback, executor ).addHeader("Authorization", "Bearer $apiKey") + requestBuilder = requestBuilder.setHttpMethod(method) + + if (uploadDataProvider != null) { + requestBuilder = requestBuilder.setUploadDataProvider(uploadDataProvider, executor) + } + return requestBuilder.build() } @@ -34,9 +48,10 @@ class VastApi( * * @param apiRoute the api route * @param callback any callback for example [UserUrlRequestCallback] + * @param uploadDataProvider [UploadDataProvider] if request sends data * @return an [UrlRequest] you must .start() yourself */ - fun buildRequest(apiRoute: ApiRoute, callback: UrlRequest.Callback): UrlRequest { - return buildRequest(apiRoute.path, callback) + fun buildRequest(apiRoute: ApiRoute, callback: UrlRequest.Callback, uploadDataProvider: UploadDataProvider? = null): UrlRequest { + return buildRequest(apiRoute.path, callback, apiRoute.method, uploadDataProvider) } } \ No newline at end of file