Skip to content
Prev Previous commit
Asserting API contract
  • Loading branch information
SessionHero01 committed Jan 14, 2026
commit a21140c6c61326a68579d2dce6d8aa0740009b83
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class PushRegistryV2 @Inject constructor(
val server = Server.LATEST
val url = "${server.url}/$path"

val (intermediateResults, rawApiResponse) = serverClient.sendWithData(
val (r, rawApiResponse) = serverClient.sendWithData(
operationName = "PushRegistryV2.$path",
requestFactory = {
val requests = builder()
Expand All @@ -128,19 +128,25 @@ class PushRegistryV2 @Inject constructor(

val bodyString = Json.encodeToString(successfullyBuiltRequests)
val body = bodyString.toRequestBody("application/json".toMediaType())
results to Request.Builder().url(url).post(body).build()
results to successfullyBuiltRequests.size to Request.Builder().url(url).post(body).build()
},
serverBaseUrl = server.url,
x25519PublicKey = server.publicKey,
version = Version.V4
)

val (intermediateResults, numSuccessfullyBuiltRequests) = r

@Suppress("OPT_IN_USAGE") val apiResponses = withContext(Dispatchers.Default) {
requireNotNull(rawApiResponse.body) { "Response doesn't have a body" }
.inputStream()
.use { Json.decodeFromStream<List<Response>>(it) }
}

check(numSuccessfullyBuiltRequests == apiResponses.size) {
"Number of API responses (${apiResponses.size}) does not match number of successfully built requests ($numSuccessfullyBuiltRequests)"
}

val apiResponseIterator = apiResponses.iterator()

intermediateResults.forEachIndexed { idx, result ->

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we get fewer results than expected from the server, isn't it possible to misalign the results?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the server MUST give us exactly the same number of result back, otherwise we have no way to know if a request is success or not. Think of it this way: we send an array of request to the server, you'd expect to get the same sized array back, it is an assumption that we have made prior to the network refactoring.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add an explicit check for this server behavior and we will reject the whole call if the server is naughty

Expand All @@ -150,10 +156,6 @@ class PushRegistryV2 @Inject constructor(
}
}

check(!apiResponseIterator.hasNext()) {
"API returned more results than expected"
}

@Suppress("UNCHECKED_CAST")
return intermediateResults as List<Result<Response>>
}
Expand Down