Skip to content

Commit

Permalink
Make http client and its filter non-blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashish Sharma committed Jul 10, 2020
1 parent 68f4915 commit ab31185
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/main/java/micronaut/http/AuthClient.java
Expand Up @@ -5,6 +5,7 @@
import io.micronaut.http.annotation.QueryValue;
import io.micronaut.http.client.annotation.Client;
import io.reactivex.Flowable;
import io.reactivex.Single;

@Client("${auth.url}")
public interface AuthClient {
Expand All @@ -16,5 +17,5 @@ Flowable<ValidateTokenResponse> validateToken(
);

@Post(value = "/issue/token", consumes = "application/json")
Token retrieveToken(@Body AuthRequest issueTokenRequest);
Single<Token> retrieveToken(@Body AuthRequest issueTokenRequest);
}
4 changes: 3 additions & 1 deletion src/main/java/micronaut/http/AuthenticateFilter.java
Expand Up @@ -21,6 +21,8 @@ public AuthenticateFilter(TokenIssuer tokenIssuer) {

@Override
public Publisher<? extends HttpResponse<?>> doFilter(MutableHttpRequest<?> request, ClientFilterChain chain) {
return chain.proceed(request.bearerAuth(tokenIssuer.retrieveToken().getAccessToken()));
return tokenIssuer.retrieveToken()
.map(token -> request.bearerAuth(token.getAccessToken()))
.flatMapPublisher(chain::proceed);
}
}
3 changes: 2 additions & 1 deletion src/main/java/micronaut/http/TokenIssuer.java
@@ -1,6 +1,7 @@
package micronaut.http;

import io.micronaut.context.annotation.Property;
import io.reactivex.Single;

import javax.inject.Singleton;

Expand All @@ -18,7 +19,7 @@ public TokenIssuer(
this.clientId = clientId;
}

public Token retrieveToken() {
public Single<Token> retrieveToken() {
return authClient.retrieveToken(new AuthRequest(clientId));
}
}

0 comments on commit ab31185

Please sign in to comment.