Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How to capture 403 response from generated APIClient (java/webclient) #12809

Open
rocb opened this issue Jul 8, 2022 · 4 comments
Labels
Announcement Project/release related announcements

Comments

@rocb
Copy link

rocb commented Jul 8, 2022

I generated a client using generatorName java and library webclient. The generate api client works the service side REST api returns success. But when I get a HTTP 403 response, the block() call just hangs.

Code is like this:

ReportsApi reportApi = new ReportsApi();
Mono rsp = reportApi.reportsGetDatasourcesInGroup(UUID.fromString(workspaceId), UUID.fromString(reportId));
Datasources ds= rsp.block();

I attempt to add .onErrorMap() and other on the Mono object, with no luck.

I know why I am getting 403 from the server, but I just need to be able to capture it in code so I can do the appropriate.

@rocb rocb added the Announcement Project/release related announcements label Jul 8, 2022
@rocb rocb changed the title [Question] How to capture 403 response from generated APIClient (java/we client) [Question] How to capture 403 response from generated APIClient (java/webclient) Jul 8, 2022
@borsch
Copy link
Member

borsch commented Jul 9, 2022

@rocb usually there are additional methods with WithHttpInfo suffix.
In your very case there should be method with following signature

public Mono<ResponseEntity<Datasources>> reportsGetDatasourcesInGroupWithHttpInfo(UUID workspaceId, UUID reportId) {
..
}

I believe you can use this method to extract status code from that ResponseEntity<Datasources>

@rocb
Copy link
Author

rocb commented Jul 9, 2022

@borsch Wow.... you are correct, I did not notice that method. After giving it a try, it behaves the same. It will simply hang on 403, any other response (like 404) can get an exception.

try {
  ReportsApi reportApi = new ReportsApi();
  reportApi.getApiClient().setAccessToken( badAccessToken );  // to simulate 403
  Mono<ResponseEntity<Report>>  rsp = reportApi.reportsGetReportInGroupWithHttpInfo(UUID.fromString(workspaceId),	UUID.fromString(reportId));		
  Report dt = rsp.onErrorMap(WebClientException.class, e -> new Exception("Error on call " + e.getMessage())).block().getBody();
} catch (Exception e) {
  e.printStackTrace();
}

I am able to catch a 404. But a 403 just hangs on .block()

13:33:23.567 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [5051d905-1, L:/192.168.1.4:65103 - R:api.powerbi.com/13.73.248.69:443] onStateChange(GET{uri=/v1.0/myorg/groups/7fc18365-6951-406e-aaf2-dc1e93fe8a07/reports/b36d5746-3166-4e1d-8758-c3d57ed5004a, connection=PooledConnection{channel=[id: 0x5051d905, L:/192.168.1.4:65103 - R:api.powerbi.com/13.73.248.69:443]}}, [request_prepared])
13:33:23.578 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [5051d905-1, L:/192.168.1.4:65103 - R:api.powerbi.com/13.73.248.69:443] onStateChange(GET{uri=/v1.0/myorg/groups/7fc18365-6951-406e-aaf2-dc1e93fe8a07/reports/b36d5746-3166-4e1d-8758-c3d57ed5004a, connection=PooledConnection{channel=[id: 0x5051d905, L:/192.168.1.4:65103 - R:api.powerbi.com/13.73.248.69:443]}}, [request_sent])
13:33:23.612 [reactor-http-nio-2] DEBUG io.netty.handler.codec.compression.Brotli - brotli4j not in the classpath; Brotli support will be unavailable.
13:33:23.613 [reactor-http-nio-2] DEBUG reactor.netty.http.client.HttpClientOperations - [5051d905-1, L:/192.168.1.4:65103 - R:api.powerbi.com/13.73.248.69:443] Received response (auto-read:false) : [Strict-Transport-Security=max-age=31536000; includeSubDomains, X-Frame-Options=deny, X-Content-Type-Options=nosniff, Access-Control-Expose-Headers=RequestId, RequestId=03b85579-df34-4048-8c90-671663f1b53e, Date=Sun, 10 Jul 2022 18:33:23 GMT, content-length=0]
13:33:23.613 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [5051d905-1, L:/192.168.1.4:65103 - R:api.powerbi.com/13.73.248.69:443] onStateChange(GET{uri=/v1.0/myorg/groups/7fc18365-6951-406e-aaf2-dc1e93fe8a07/reports/b36d5746-3166-4e1d-8758-c3d57ed5004a, connection=PooledConnection{channel=[id: 0x5051d905, L:/192.168.1.4:65103 - R:api.powerbi.com/13.73.248.69:443]}}, [response_received])
13:33:23.615 [reactor-http-nio-2] DEBUG org.springframework.web.reactive.function.client.ExchangeFunctions - [75d2da2d] [5051d905-1, L:/192.168.1.4:65103 - R:api.powerbi.com/13.73.248.69:443] Response 403 FORBIDDEN
13:33:23.622 [reactor-http-nio-2] DEBUG reactor.netty.http.client.HttpClientOperations - [5051d905-1, L:/192.168.1.4:65103 - R:api.powerbi.com/13.73.248.69:443] Received last HTTP packet
13:33:23.622 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [5051d905, L:/192.168.1.4:65103 - R:api.powerbi.com/13.73.248.69:443] onStateChange(GET{uri=/v1.0/myorg/groups/7fc18365-6951-406e-aaf2-dc1e93fe8a07/reports/b36d5746-3166-4e1d-8758-c3d57ed5004a, connection=PooledConnection{channel=[id: 0x5051d905, L:/192.168.1.4:65103 - R:api.powerbi.com/13.73.248.69:443]}}, [response_completed])
13:33:23.622 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [5051d905, L:/192.168.1.4:65103 - R:api.powerbi.com/13.73.248.69:443] onStateChange(GET{uri=/v1.0/myorg/groups/7fc18365-6951-406e-aaf2-dc1e93fe8a07/reports/b36d5746-3166-4e1d-8758-c3d57ed5004a, connection=PooledConnection{channel=[id: 0x5051d905, L:/192.168.1.4:65103 - R:api.powerbi.com/13.73.248.69:443]}}, [disconnecting])
13:33:23.622 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [5051d905, L:/192.168.1.4:65103 - R:api.powerbi.com/13.73.248.69:443] Releasing channel
13:33:23.624 [reactor-http-nio-2] DEBUG reactor.netty.resources.PooledConnectionProvider - [5051d905, L:/192.168.1.4:65103 - R:api.powerbi.com/13.73.248.69:443] Channel cleaned, now: 0 active connections, 1 inactive connections and 0 pending acquire requests.

So, for now, I am gonna put in a Duration timeout in.. .block(5) and assume it is a 403 (not really a solution)

@borsch
Copy link
Member

borsch commented Jul 12, 2022

Hi @rocb
I've extended webclient api to allow manually control/configure ResponseSpec. Check this PR - #12843 and this Spring example - spring-projects/spring-framework#20280

Hope this will help you

@borsch
Copy link
Member

borsch commented Jul 25, 2022

@rocb try latest master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Announcement Project/release related announcements
Projects
None yet
Development

No branches or pull requests

2 participants