55import java .util .Optional ;
66import java .util .function .Supplier ;
77import org .springframework .beans .factory .annotation .Qualifier ;
8- import org .springframework .http .HttpStatus ;
98import org .springframework .stereotype .Component ;
109import uk .gov .companieshouse .api .InternalApiClient ;
1110import uk .gov .companieshouse .api .error .ApiErrorResponseException ;
1514import uk .gov .companieshouse .logging .Logger ;
1615import uk .gov .companieshouse .logging .LoggerFactory ;
1716import uk .gov .companieshouse .pscdataapi .exceptions .BadGatewayException ;
18- import uk .gov .companieshouse .pscdataapi .exceptions .ResourceNotFoundException ;
1917import uk .gov .companieshouse .pscdataapi .logging .DataMapHolder ;
2018
2119@ Component
@@ -31,7 +29,7 @@ public CompanyMetricsApiService(
3129 }
3230
3331 public Optional <MetricsApi > getCompanyMetrics (final String companyNumber ) {
34- ApiResponse <MetricsApi > response ;
32+ ApiResponse <MetricsApi > response = null ;
3533 try {
3634 response = metricsApiClientSupplier .get ()
3735 .privateCompanyMetricsResourceHandler ()
@@ -41,17 +39,15 @@ public Optional<MetricsApi> getCompanyMetrics(final String companyNumber) {
4139 final int statusCode = ex .getStatusCode ();
4240 LOGGER .info ("Company Metrics API call failed with status code [%s]" .formatted (statusCode ),
4341 DataMapHolder .getLogMap ());
44- if (statusCode == 404 ) {
45- throw new ResourceNotFoundException (HttpStatus .NOT_FOUND ,
46- "Company Metrics API responded with 404 Not Found" );
47- } else {
42+ if (statusCode != 404 ) {
4843 throw new BadGatewayException ("Error calling Company Metrics API endpoint" , ex );
4944 }
5045 } catch (URIValidationException ex ) {
5146 LOGGER .info ("URI validation error when calling Company Metrics API" , DataMapHolder .getLogMap ());
5247 throw new BadGatewayException ("URI validation error when calling Company Metrics API" , ex );
5348 }
5449
55- return response != null ? Optional .of (response .getData ()) : Optional .empty ();
50+ return Optional .ofNullable (response )
51+ .map (ApiResponse ::getData );
5652 }
5753}
0 commit comments