Skip to content

Commit

Permalink
Merge branch 'master' into bump-kotlin-version-to-1.8
Browse files Browse the repository at this point in the history
  • Loading branch information
andregasser authored Mar 3, 2023
2 parents 6b33942 + 1e60aa0 commit 32944b4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
41 changes: 35 additions & 6 deletions bigbone/src/main/kotlin/social/bigbone/MastodonClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ private constructor(
Method.PATCH -> patch(endpoint, null)
Method.POST -> post(endpoint)
}
response.close()
if (!response.isSuccessful) {
throw BigBoneRequestException(response)
}
Expand Down Expand Up @@ -443,14 +444,42 @@ private constructor(
}
}

internal fun v2InstanceRequest(): Response {
val client = OkHttpClient.Builder().build()
return client.newCall(Request.Builder().url(fullUrl(instanceName, "api/v2/instance")).get().build()).execute()
}
/**
* Returns the server response for an instance request of version 2.
* @return server response for this request; if the response is not successful, its body will be closed
*/
internal fun v2InstanceRequest(): Response = versionedInstanceRequest(2)

internal fun v1InstanceRequest(): Response {
/**
* Returns the server response for an instance request of version 1.
* @return server response for this request; if the response is not successful, its body will be closed
*/
internal fun v1InstanceRequest(): Response = versionedInstanceRequest(1)

/**
* Returns the server response for an instance request of a specific version.
* @param version value corresponding to the version that should be returned; falls
* back to returning version 1 for illegal values.
* @return server response for this request; if the response is not successful, its body will be closed
*/
private fun versionedInstanceRequest(version: Int): Response {
val versionString = if (version == 2) {
"v2"
} else {
"v1"
}
val client = OkHttpClient.Builder().build()
return client.newCall(Request.Builder().url(fullUrl(instanceName, "api/v1/instance")).get().build()).execute()
val response = client.newCall(
Request.Builder().url(
fullUrl(
instanceName,
"api/$versionString/instance"
)
).get().build()).execute()
if (!response.isSuccessful) {
response.close()
}
return response
}

fun build(): MastodonClient {
Expand Down
1 change: 1 addition & 0 deletions bigbone/src/main/kotlin/social/bigbone/MastodonRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class MastodonRequest<T>(
throw BigBoneRequestException("Successful response could not be parsed", e)
}
} else {
response.close()
throw BigBoneRequestException(response)
}
}
Expand Down

0 comments on commit 32944b4

Please sign in to comment.