Skip to content

Commit 6927add

Browse files
committed
Create #isSuccess for HTTP Codes
1 parent 815bb20 commit 6927add

File tree

1 file changed

+14
-7
lines changed
  • src/main/kotlin/io/codemc/api/nexus

1 file changed

+14
-7
lines changed

src/main/kotlin/io/codemc/api/nexus/nexus.kt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ data class NexusConfig(
4646
*/
4747
lateinit var nexusConfig: NexusConfig
4848

49+
/**
50+
* Whether this HTTP Response is a success.
51+
* @receiver The HTTP Response Code.
52+
*/
53+
val Int.isSuccess
54+
get() = this in 200..299
55+
4956
// Implementation
5057

5158
/**
@@ -66,7 +73,7 @@ suspend fun nexus(url: String, request: HttpRequest.Builder.() -> Unit = { GET()
6673
*/
6774
fun ping(): Boolean = runBlocking {
6875
val text = nexus("$API_URL/status")
69-
return@runBlocking text.statusCode() == 200
76+
return@runBlocking text.statusCode().isSuccess
7077
}
7178

7279
/**
@@ -81,15 +88,14 @@ fun ping(): Boolean = runBlocking {
8188
fun createNexus(name: String, password: String) = runBlocking(Dispatchers.IO) {
8289
// Create User Repository
8390
val id = name.lowercase()
84-
8591
if (getNexusRepository(id) == null) {
8692
val repo = createMavenRepository(name)
8793
val repoResponse = nexus("$API_URL/repositories/maven/hosted") {
8894
POST(HttpRequest.BodyPublishers.ofString(repo))
8995
header("Content-Type", "application/json")
9096
}
9197

92-
if (repoResponse.statusCode() != 201) return@runBlocking false
98+
if (!repoResponse.statusCode().isSuccess) return@runBlocking false
9399
}
94100

95101
// Add Role
@@ -108,7 +114,7 @@ fun createNexus(name: String, password: String) = runBlocking(Dispatchers.IO) {
108114
header("Content-Type", "application/json")
109115
}
110116

111-
if (roleReq.statusCode() != 200) return@runBlocking false
117+
if (!roleReq.statusCode().isSuccess) return@runBlocking false
112118
}
113119

114120
// Add User with Role
@@ -130,8 +136,9 @@ fun createNexus(name: String, password: String) = runBlocking(Dispatchers.IO) {
130136
header("Content-Type", "application/json")
131137
}
132138

133-
if (userRes.statusCode() != 201) return@runBlocking false
139+
if (!userRes.statusCode().isSuccess) return@runBlocking false
134140
}
141+
135142
return@runBlocking true
136143
}
137144

@@ -154,7 +161,7 @@ fun changeNexusPassword(name: String, newPassword: String) = runBlocking(Dispatc
154161
header("Content-Type", "text/plain")
155162
}
156163

157-
return@runBlocking res.statusCode() == 204
164+
return@runBlocking res.statusCode().isSuccess
158165
}
159166

160167
/**
@@ -172,7 +179,7 @@ fun deleteNexus(name: String) = runBlocking(Dispatchers.IO) {
172179
if (roleRes.statusCode() != 204) return@runBlocking false
173180

174181
val userRes = nexus("$API_URL/security/users/$id") { DELETE() }
175-
return@runBlocking userRes.statusCode() == 204
182+
return@runBlocking userRes.statusCode().isSuccess
176183
}
177184

178185
@VisibleForTesting

0 commit comments

Comments
 (0)