All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.ecwid.apiclient.v3.impl.RequestInfo.kt Maven / Gradle / Ivy
package com.ecwid.apiclient.v3.impl
import com.ecwid.apiclient.v3.dto.common.UploadFileData
import com.ecwid.apiclient.v3.httptransport.HttpBody
import com.ecwid.apiclient.v3.responsefields.ResponseFields
import com.ecwid.apiclient.v3.withResponseFieldsParam
class RequestInfo private constructor(
val pathSegments: List,
val method: HttpMethod,
val params: Map,
val headers: Map,
val httpBody: HttpBody
) {
companion object {
fun createGetRequest(
pathSegments: List,
params: Map = mapOf(),
headers: Map = mapOf(),
responseFields: ResponseFields = ResponseFields.All, // default value will be removed in next releases
) = RequestInfo(
pathSegments = pathSegments,
method = HttpMethod.GET,
params = params.withResponseFieldsParam(responseFields),
headers = headers,
httpBody = HttpBody.EmptyBody,
)
fun createDeleteRequest(
pathSegments: List,
params: Map = mapOf(),
headers: Map = mapOf(),
) = RequestInfo(
pathSegments = pathSegments,
method = HttpMethod.DELETE,
params = params,
headers = headers,
httpBody = HttpBody.EmptyBody
)
fun createPostRequest(
pathSegments: List,
params: Map = mapOf(),
headers: Map = mapOf(),
httpBody: HttpBody
) = RequestInfo(
pathSegments = pathSegments,
method = HttpMethod.POST,
params = params,
headers = headers,
httpBody = httpBody
)
fun createPutRequest(
pathSegments: List,
params: Map = mapOf(),
headers: Map = mapOf(),
httpBody: HttpBody
) = RequestInfo(
pathSegments = pathSegments,
method = HttpMethod.PUT,
params = params,
headers = headers,
httpBody = httpBody
)
fun buildUploadRequestInfo(
pathSegments: List,
commonParams: Map,
fileData: UploadFileData
): RequestInfo {
return when (fileData) {
is UploadFileData.ExternalUrlData -> createPostRequest(
pathSegments = pathSegments,
params = commonParams + mapOf(
"externalUrl" to fileData.externalUrl
),
httpBody = HttpBody.EmptyBody
)
is UploadFileData.ByteArrayData -> createPostRequest(
pathSegments = pathSegments,
params = commonParams,
httpBody = HttpBody.ByteArrayBody(
bytes = fileData.bytes,
mimeType = MIME_TYPE_OCTET_STREAM
)
)
is UploadFileData.LocalFileData -> createPostRequest(
pathSegments = pathSegments,
params = commonParams,
httpBody = HttpBody.LocalFileBody(
file = fileData.file,
mimeType = MIME_TYPE_OCTET_STREAM
)
)
is UploadFileData.InputStreamData -> createPostRequest(
pathSegments = pathSegments,
params = commonParams,
httpBody = HttpBody.InputStreamBody(
stream = fileData.stream,
mimeType = MIME_TYPE_OCTET_STREAM
)
)
}
}
}
}
enum class HttpMethod {
GET,
POST,
PUT,
DELETE
}