Skip to content

Commit 7c9577a

Browse files
committed
Refactor and improve Mosapi client implementation
Simplifying URL validation with Guava Preconditions and refining exception handling to use `Throwables`.
1 parent 33e82ba commit 7c9577a

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

core/src/main/java/google/registry/mosapi/MosApiClient.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
package google.registry.mosapi;
1616

17+
import static com.google.common.base.Preconditions.checkArgument;
18+
19+
import com.google.common.base.Throwables;
1720
import google.registry.config.RegistryConfig.Config;
1821
import google.registry.mosapi.MosApiException.MosApiAuthorizationException;
1922
import jakarta.inject.Inject;
@@ -43,9 +46,9 @@ public MosApiClient(
4346
this.httpClient = httpClient;
4447
// Pre-calculate base URL and validate it to fail fast on bad config
4548
String fullUrl = String.format("%s/%s", mosapiUrl, entityType);
46-
if (HttpUrl.parse(fullUrl) == null) {
47-
throw new IllegalArgumentException("Invalid MoSAPI Service URL configuration: " + fullUrl);
48-
}
49+
checkArgument(
50+
HttpUrl.parse(fullUrl) != null, "Invalid MoSAPI Service URL configuration: %s", fullUrl);
51+
4952
this.baseUrl = fullUrl;
5053
}
5154

@@ -73,9 +76,7 @@ public Response sendGetRequest(
7376
return checkResponseForAuthError(response);
7477
} catch (RuntimeException | IOException e) {
7578
// Check if it's the specific authorization exception (re-thrown or caught here)
76-
if (e instanceof MosApiAuthorizationException) {
77-
throw (MosApiAuthorizationException) e;
78-
}
79+
Throwables.throwIfInstanceOf(e, MosApiAuthorizationException.class);
7980
// Otherwise, treat as a generic connection/API error
8081
throw new MosApiException("Error during GET request to " + url, e);
8182
}
@@ -114,9 +115,7 @@ public Response sendPostRequest(
114115
return checkResponseForAuthError(response);
115116
} catch (RuntimeException | IOException e) {
116117
// Check if it's the specific authorization exception (re-thrown or caught here)
117-
if (e instanceof MosApiAuthorizationException) {
118-
throw (MosApiAuthorizationException) e;
119-
}
118+
Throwables.throwIfInstanceOf(e, MosApiAuthorizationException.class);
120119
// Otherwise, treat as a generic connection/API error
121120
throw new MosApiException("Error during POST request to " + url, e);
122121
}

0 commit comments

Comments
 (0)