Skip to content

Commit

Permalink
chore: add more checks
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarAlJarrah committed Jan 30, 2025
1 parent 3feedf8 commit 8c9972b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.expediagroup.sdk.rest.trait.operation.ContentTypeTrait
import com.expediagroup.sdk.rest.trait.operation.HeadersTrait
import com.expediagroup.sdk.rest.trait.operation.HttpMethodTrait
import com.expediagroup.sdk.rest.trait.operation.OperationRequestBodyTrait
import com.expediagroup.sdk.rest.trait.operation.OperationRequestTrait
import com.expediagroup.sdk.rest.trait.operation.OperationTrait
import com.expediagroup.sdk.rest.trait.operation.UrlPathTrait
import com.expediagroup.sdk.rest.trait.operation.UrlQueryParamsTrait
Expand All @@ -34,15 +35,17 @@ import java.net.URL
IllegalStateException::class,
IOException::class
)
fun OperationTrait.parseRequest(
internal fun OperationRequestTrait.parseRequest(
serverUrl: URL,
serializer: SerializeRequestBodyTrait
): Request {
require(this is HttpMethodTrait) { "Operation must implement HttpMethodTrait trait!" }
require(this is HttpMethodTrait && this.getHttpMethod().isNotBlank()) {
"Operation must implement HttpMethodTrait trait!"
}

val builder = Request.builder().method(this.parseMethod())

if (this is HeadersTrait) {
if (this is HeadersTrait && this.getHeaders().entries().isNotEmpty()) {
builder.headers(this.getHeaders())
}

Expand All @@ -51,7 +54,7 @@ fun OperationTrait.parseRequest(
}

builder.url(
if (this is UrlPathTrait) {
if (this is UrlPathTrait && this.getUrlPath().isNotBlank()) {
this.parseURL(serverUrl)
} else {
serverUrl
Expand Down Expand Up @@ -103,7 +106,7 @@ internal fun UrlPathTrait.parseURL(base: URL): URL =
* @throws IllegalArgumentException if the HTTP method is invalid
*/
@Throws(IllegalArgumentException::class)
fun HttpMethodTrait.parseMethod(): Method =
internal fun HttpMethodTrait.parseMethod(): Method =
Method.valueOf(getHttpMethod().uppercase())

/**
Expand All @@ -115,7 +118,7 @@ fun HttpMethodTrait.parseMethod(): Method =
* @throws IllegalArgumentException if the content type is invalid
*/
@Throws(IllegalArgumentException::class)
fun ContentTypeTrait.parseMediaType(): MediaType =
internal fun ContentTypeTrait.parseMediaType(): MediaType =
MediaType.parse(getContentType())

/**
Expand All @@ -132,10 +135,10 @@ fun ContentTypeTrait.parseMediaType(): MediaType =
IllegalStateException::class,
IOException::class
)
fun OperationRequestBodyTrait<*>.parseRequestBody(
internal fun OperationRequestBodyTrait<*>.parseRequestBody(
serializer: SerializeRequestBodyTrait
): RequestBody {
require(getRequestBody() != null) { "Request body is required" }
require(getRequestBody() != null) { "Request body is required!" }

val inputStream = serializer.serialize(getRequestBody()!!)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.expediagroup.sdk.rest.extension

import com.expediagroup.sdk.rest.model.Response
import com.expediagroup.sdk.rest.trait.operation.OperationNoResponseBodyTrait
import com.expediagroup.sdk.core.http.Response as SDKCoreResponse
import com.expediagroup.sdk.rest.trait.operation.OperationNoResponseBodyTrait
import com.expediagroup.sdk.rest.trait.operation.OperationResponseBodyTrait
import com.expediagroup.sdk.rest.trait.serialization.DeserializeResponseBodyTrait
import java.io.IOException

/**
* Extension function to parse the body of an SDKCoreResponse as a specific type.
Expand All @@ -14,6 +15,7 @@ import com.expediagroup.sdk.rest.trait.serialization.DeserializeResponseBodyTrai
* @param deserializer the deserializer to use for parsing the response body
* @return the parsed response body of type T
*/
@Throws(IOException::class)
fun <T> SDKCoreResponse.parseBodyAs(
operation: OperationResponseBodyTrait<T>,
deserializer: DeserializeResponseBodyTrait
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ class DefaultJacksonBasedOperationDataMapper(
IllegalArgumentException::class,
IOException::class
)
override fun <T> deserialize(inputStream: InputStream, operation: OperationResponseBodyTrait<T>): T {
override fun <T> deserialize(
inputStream: InputStream,
operation: OperationResponseBodyTrait<T>
): T {
require(operation is JacksonModelOperationResponseBodyTrait<T>) {
"Operation must implement OperationResponseBodyTrait"
}
Expand Down

0 comments on commit 8c9972b

Please sign in to comment.