Skip to content

Commit

Permalink
check baseUrl in setter method
Browse files Browse the repository at this point in the history
  • Loading branch information
Foso committed Aug 31, 2022
1 parent 02496aa commit f756146
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fun getPathsText(params: List<ParameterData>): String {
val pathPath = pathAnnotation?.value ?: ""
val pathEncoded = pathAnnotation?.encoded ?: false

"${pathDataClass.name}(\"$pathPath\",$pathEncoded,\"\$$paramName\")"
"${pathDataClass.name}(\"$pathPath\",\"\$$paramName\",$pathEncoded)"
}.joinToString { it }.surroundIfNotEmpty("paths = listOf(", ")")

return pathData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ interface TestService {
val requestData = RequestData(method="GET",
relativeUrl="user/{id}",
qualifiedRawTypeName="kotlin.String",
paths = listOf(PathData("id",false,"äid")))
paths = listOf(PathData("id","äid",false)))
return client.suspendRequest<String, String>(requestData)
}""".replace("\\{", "{").replace("ä","$")
Expand Down Expand Up @@ -83,7 +83,7 @@ interface TestService {
val requestData = RequestData(method="GET",
relativeUrl="user/{id}",
qualifiedRawTypeName="kotlin.String",
paths = listOf(PathData("id",true,"äid")))
paths = listOf(PathData("id","äid",true)))
return client.suspendRequest<String, String>(requestData)
}""".replace("%", "").replace("ä","$")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Ktorfit private constructor(
* @see httpClient
*/
class Builder {
private lateinit var _baseUrl: String
private var _baseUrl: String = ""
private var _httpClient = HttpClient()
private var _responseConverter: MutableSet<ResponseConverter> = mutableSetOf()
private var _suspendResponseConverter: MutableSet<SuspendResponseConverter> = mutableSetOf()
Expand All @@ -42,6 +42,13 @@ class Ktorfit private constructor(
* That will be used for every request with object
*/
fun baseUrl(url: String) = apply {
if (url.isEmpty()) {
throw IllegalStateException("Base URL required")
}

if (!url.endsWith("/")) {
throw IllegalStateException("Base URL needs to end with /")
}
this._baseUrl = url
}

Expand Down Expand Up @@ -96,14 +103,6 @@ class Ktorfit private constructor(
* Creates an instance of Ktorfit with specified baseUrl and HttpClient.
*/
fun build(): Ktorfit {
if (!this::_baseUrl.isInitialized || this._baseUrl.isEmpty()) {
throw IllegalStateException("Base URL required")
}

if (!_baseUrl.endsWith("/")) {
throw IllegalStateException("Base URL needs to end with /")
}

return Ktorfit(_baseUrl, _httpClient, _responseConverter, _suspendResponseConverter)
}
}
Expand Down

0 comments on commit f756146

Please sign in to comment.