|
25 | 25 | package com.github.rishabh9.riko.upstox.login; |
26 | 26 |
|
27 | 27 | import com.github.rishabh9.riko.upstox.common.ServiceGenerator; |
28 | | -import com.github.rishabh9.riko.upstox.common.models.ApiCredentials; |
| 28 | +import com.github.rishabh9.riko.upstox.common.UpstoxAuthService; |
29 | 29 | import com.github.rishabh9.riko.upstox.login.models.AccessToken; |
30 | 30 | import com.github.rishabh9.riko.upstox.login.models.TokenRequest; |
31 | 31 | import com.google.common.base.Strings; |
|
35 | 35 | import javax.annotation.Nonnull; |
36 | 36 | import java.util.Objects; |
37 | 37 | import java.util.concurrent.CompletableFuture; |
38 | | -import java.util.concurrent.ExecutionException; |
39 | 38 |
|
40 | 39 | public class LoginService { |
41 | 40 |
|
42 | 41 | private static final Logger log = LogManager.getLogger(LoginService.class); |
43 | 42 |
|
44 | | - private final TokenRequest request; |
45 | | - |
46 | | - private final ApiCredentials credentials; |
| 43 | + private final UpstoxAuthService upstoxAuthService; |
47 | 44 |
|
48 | 45 | /** |
49 | | - * @param request The TokenRequest object containing all fields, |
50 | | - * including the access code received after authenticating the user on Upstox site. |
51 | | - * @param credentials The API key and secret. |
| 46 | + * @param upstoxAuthService The service to retrieve authentication details. |
52 | 47 | */ |
53 | | - public LoginService(@Nonnull final TokenRequest request, |
54 | | - @Nonnull final ApiCredentials credentials) { |
| 48 | + public LoginService(@Nonnull final UpstoxAuthService upstoxAuthService) { |
55 | 49 |
|
56 | | - this.request = Objects.requireNonNull(request); |
57 | | - this.credentials = Objects.requireNonNull(credentials); |
| 50 | + this.upstoxAuthService = Objects.requireNonNull(upstoxAuthService); |
58 | 51 | } |
59 | 52 |
|
60 | 53 | /** |
61 | 54 | * Retrieves the access code from Upstox Authorization URL through a synchronous call.<br> |
62 | 55 | * |
| 56 | + * @param request The TokenRequest object containing all fields, |
| 57 | + * including the access code received after authenticating the user on Upstox site. |
63 | 58 | * @return An 'optional' AccessToken. Return object is empty in case of error. |
64 | 59 | */ |
65 | | - public AccessToken getAccessToken() throws ExecutionException, InterruptedException { |
| 60 | + public CompletableFuture<AccessToken> getAccessToken(@Nonnull final TokenRequest request) { |
66 | 61 |
|
67 | | - if (Strings.isNullOrEmpty(request.getCode())) { |
| 62 | + if (Strings.isNullOrEmpty(Objects.requireNonNull(request).getCode())) { |
68 | 63 | throw new IllegalArgumentException( |
69 | 64 | "Missing value for authorization code. Code: " + request.getCode()); |
70 | 65 | } |
71 | 66 |
|
72 | 67 | // Create a very simple REST adapter which points the Upstox API endpoint. |
73 | | - LoginApi loginApi = |
| 68 | + final LoginApi loginApi = |
74 | 69 | ServiceGenerator.getInstance().createService( |
75 | 70 | LoginApi.class, |
76 | | - credentials.getApiKey(), |
77 | | - credentials.getApiSecret()); |
78 | | - |
79 | | - CompletableFuture<AccessToken> future = loginApi.getAccessToken(request); |
| 71 | + upstoxAuthService.getApiCredentials().getApiKey(), |
| 72 | + upstoxAuthService.getApiCredentials().getApiSecret()); |
80 | 73 |
|
81 | | - // Make a synchronous call. |
82 | | - return future |
83 | | -// .exceptionally( |
84 | | -// (throwable) -> { |
85 | | -// if (null != throwable) { |
86 | | -// if (throwable instanceof HttpException) { // Non 2XX HTTP Response Code |
87 | | -// log.error("Request to retrieve access code was unsuccessful", |
88 | | -// throwable); |
89 | | -// } else if (throwable instanceof IOError) { // Network Error |
90 | | -// log.error("A network error occurred while trying to retrieve access code", |
91 | | -// throwable); |
92 | | -// } else { |
93 | | -// log.error("Unexpected error has occurred", throwable); |
94 | | -// } |
95 | | -// } |
96 | | -// return null; |
97 | | -// }) |
98 | | - .get(); |
| 74 | + return loginApi.getAccessToken(request); |
99 | 75 | } |
100 | 76 | } |
0 commit comments