All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ecwid.apiclient.v3.impl.RequestInfo.kt Maven / Gradle / Ivy

There is a newer version: 0.347.0
Show newest version
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
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy