Skip to content

Commit 5e90d6f

Browse files
committed
Add UsptoxAuthService, update LoginService
1 parent 0dfc2c3 commit 5e90d6f

15 files changed

Lines changed: 313 additions & 672 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ Checkout the [wiki](https://github.com/rishabh9/riko/wiki) for more details.
1212
<dependency>
1313
<groupId>com.github.rishabh9</groupId>
1414
<artifactId>riko</artifactId>
15-
<version>1.0.2</version>
15+
<version>2.0.0</version>
1616
</dependency>
1717
```
1818

1919
### For Gradle based project
2020
```groovy
2121
dependencies {
22-
implementation 'com.github.rishabh9:riko:1.0.2'
22+
implementation 'com.github.rishabh9:riko:2.0.0'
2323
}
2424
```
2525

2626
### For SBT based project
2727
```scala
28-
libraryDependencies += "com.github.rishabh9" % "riko" % "1.0.2"
28+
libraryDependencies += "com.github.rishabh9" % "riko" % "2.0.0"
2929
```

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ dependencyManagement {
4747
// GroupId
4848
group = 'com.github.rishabh9'
4949
// Version
50-
version = '1.0.2'
50+
version = '2.0.0'
5151
archivesBaseName = 'riko'
5252

5353
dependencies {

src/main/java/com/github/rishabh9/riko/upstox/common/Service.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,22 @@ public abstract class Service {
4040

4141
private static final Logger log = LogManager.getLogger(Service.class);
4242

43-
protected final AccessToken accessToken;
44-
protected final ApiCredentials credentials;
43+
protected final UpstoxAuthService upstoxAuthService;
4544

4645
/**
47-
* @param accessToken The user's access token
48-
* @param credentials The user's API credentials
46+
* @param upstoxAuthService The service to retrieve authentication details
4947
*/
50-
public Service(@Nonnull final AccessToken accessToken,
51-
@Nonnull final ApiCredentials credentials) {
48+
public Service(@Nonnull final UpstoxAuthService upstoxAuthService) {
5249

53-
this.accessToken = Objects.requireNonNull(accessToken);
54-
this.credentials = Objects.requireNonNull(credentials);
50+
this.upstoxAuthService = Objects.requireNonNull(upstoxAuthService);
5551
}
5652

5753
protected <T> T prepareServiceApi(@Nonnull final Class<T> type) {
5854

5955
log.debug("Preparing service API: {}", type.getName());
56+
final AccessToken accessToken = upstoxAuthService.getAccessToken();
57+
final ApiCredentials credentials = upstoxAuthService.getApiCredentials();
58+
6059
final String token = accessToken.getType() + " " + accessToken.getToken();
6160
return ServiceGenerator.getInstance()
6261
.createService(type, new AuthHeaders(token, credentials.getApiKey()));
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2018 Rishabh Joshi
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package com.github.rishabh9.riko.upstox.common;
26+
27+
import com.github.rishabh9.riko.upstox.common.models.ApiCredentials;
28+
import com.github.rishabh9.riko.upstox.login.models.AccessToken;
29+
30+
/**
31+
* The implementations of this interface are responsible to <em>always return
32+
* the latest version</em> of the {@link ApiCredentials} and {@link AccessToken}.
33+
*/
34+
public interface UpstoxAuthService {
35+
36+
/**
37+
* @return The latest API Key and API Secret of an Upstox account.
38+
*/
39+
ApiCredentials getApiCredentials();
40+
41+
/**
42+
* @return The latest access token
43+
*/
44+
AccessToken getAccessToken();
45+
}

src/main/java/com/github/rishabh9/riko/upstox/feed/FeedService.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,27 @@
2525
package com.github.rishabh9.riko.upstox.feed;
2626

2727
import com.github.rishabh9.riko.upstox.common.Service;
28-
import com.github.rishabh9.riko.upstox.common.models.ApiCredentials;
28+
import com.github.rishabh9.riko.upstox.common.UpstoxAuthService;
2929
import com.github.rishabh9.riko.upstox.common.models.UpstoxResponse;
3030
import com.github.rishabh9.riko.upstox.feed.models.Feed;
3131
import com.github.rishabh9.riko.upstox.feed.models.SubscriptionResponse;
32-
import com.github.rishabh9.riko.upstox.login.models.AccessToken;
3332
import com.google.common.base.Strings;
3433
import org.apache.logging.log4j.LogManager;
3534
import org.apache.logging.log4j.Logger;
3635

3736
import javax.annotation.Nonnull;
38-
import java.util.Objects;
3937
import java.util.concurrent.CompletableFuture;
4038

4139
public class FeedService extends Service {
4240

4341
private static final Logger log = LogManager.getLogger(FeedService.class);
4442

45-
public FeedService(@Nonnull final AccessToken accessToken,
46-
@Nonnull final ApiCredentials credentials) {
43+
/**
44+
* @param upstoxAuthService The service to retrieve authentication details
45+
*/
46+
public FeedService(@Nonnull final UpstoxAuthService upstoxAuthService) {
4747

48-
super(Objects.requireNonNull(accessToken), Objects.requireNonNull(credentials));
48+
super(upstoxAuthService);
4949
}
5050

5151
/**

src/main/java/com/github/rishabh9/riko/upstox/historical/HistoricalService.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,27 @@
2525
package com.github.rishabh9.riko.upstox.historical;
2626

2727
import com.github.rishabh9.riko.upstox.common.Service;
28-
import com.github.rishabh9.riko.upstox.common.models.ApiCredentials;
28+
import com.github.rishabh9.riko.upstox.common.UpstoxAuthService;
2929
import com.github.rishabh9.riko.upstox.common.models.UpstoxResponse;
3030
import com.github.rishabh9.riko.upstox.historical.models.Candle;
31-
import com.github.rishabh9.riko.upstox.login.models.AccessToken;
3231
import com.google.common.base.Strings;
3332
import org.apache.logging.log4j.LogManager;
3433
import org.apache.logging.log4j.Logger;
3534

3635
import javax.annotation.Nonnull;
3736
import java.util.List;
38-
import java.util.Objects;
3937
import java.util.concurrent.CompletableFuture;
4038

4139
public class HistoricalService extends Service {
4240

4341
private static final Logger log = LogManager.getLogger(HistoricalService.class);
4442

45-
public HistoricalService(@Nonnull final AccessToken accessToken,
46-
@Nonnull final ApiCredentials credentials) {
43+
/**
44+
* @param upstoxAuthService The service to retrieve authentication details
45+
*/
46+
public HistoricalService(@Nonnull final UpstoxAuthService upstoxAuthService) {
4747

48-
super(Objects.requireNonNull(accessToken), Objects.requireNonNull(credentials));
48+
super(upstoxAuthService);
4949
}
5050

5151
/**

src/main/java/com/github/rishabh9/riko/upstox/login/LoginService.java

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
package com.github.rishabh9.riko.upstox.login;
2626

2727
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;
2929
import com.github.rishabh9.riko.upstox.login.models.AccessToken;
3030
import com.github.rishabh9.riko.upstox.login.models.TokenRequest;
3131
import com.google.common.base.Strings;
@@ -35,66 +35,42 @@
3535
import javax.annotation.Nonnull;
3636
import java.util.Objects;
3737
import java.util.concurrent.CompletableFuture;
38-
import java.util.concurrent.ExecutionException;
3938

4039
public class LoginService {
4140

4241
private static final Logger log = LogManager.getLogger(LoginService.class);
4342

44-
private final TokenRequest request;
45-
46-
private final ApiCredentials credentials;
43+
private final UpstoxAuthService upstoxAuthService;
4744

4845
/**
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.
5247
*/
53-
public LoginService(@Nonnull final TokenRequest request,
54-
@Nonnull final ApiCredentials credentials) {
48+
public LoginService(@Nonnull final UpstoxAuthService upstoxAuthService) {
5549

56-
this.request = Objects.requireNonNull(request);
57-
this.credentials = Objects.requireNonNull(credentials);
50+
this.upstoxAuthService = Objects.requireNonNull(upstoxAuthService);
5851
}
5952

6053
/**
6154
* Retrieves the access code from Upstox Authorization URL through a synchronous call.<br>
6255
*
56+
* @param request The TokenRequest object containing all fields,
57+
* including the access code received after authenticating the user on Upstox site.
6358
* @return An 'optional' AccessToken. Return object is empty in case of error.
6459
*/
65-
public AccessToken getAccessToken() throws ExecutionException, InterruptedException {
60+
public CompletableFuture<AccessToken> getAccessToken(@Nonnull final TokenRequest request) {
6661

67-
if (Strings.isNullOrEmpty(request.getCode())) {
62+
if (Strings.isNullOrEmpty(Objects.requireNonNull(request).getCode())) {
6863
throw new IllegalArgumentException(
6964
"Missing value for authorization code. Code: " + request.getCode());
7065
}
7166

7267
// Create a very simple REST adapter which points the Upstox API endpoint.
73-
LoginApi loginApi =
68+
final LoginApi loginApi =
7469
ServiceGenerator.getInstance().createService(
7570
LoginApi.class,
76-
credentials.getApiKey(),
77-
credentials.getApiSecret());
78-
79-
CompletableFuture<AccessToken> future = loginApi.getAccessToken(request);
71+
upstoxAuthService.getApiCredentials().getApiKey(),
72+
upstoxAuthService.getApiCredentials().getApiSecret());
8073

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);
9975
}
10076
}

src/main/java/com/github/rishabh9/riko/upstox/orders/OrderService.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@
2525
package com.github.rishabh9.riko.upstox.orders;
2626

2727
import com.github.rishabh9.riko.upstox.common.Service;
28-
import com.github.rishabh9.riko.upstox.common.models.ApiCredentials;
28+
import com.github.rishabh9.riko.upstox.common.UpstoxAuthService;
2929
import com.github.rishabh9.riko.upstox.common.models.UpstoxResponse;
30-
import com.github.rishabh9.riko.upstox.login.models.AccessToken;
3130
import com.github.rishabh9.riko.upstox.orders.models.Order;
3231
import com.github.rishabh9.riko.upstox.orders.models.OrderRequest;
3332
import com.github.rishabh9.riko.upstox.orders.models.Trade;
@@ -37,21 +36,18 @@
3736

3837
import javax.annotation.Nonnull;
3938
import java.util.List;
40-
import java.util.Objects;
4139
import java.util.concurrent.CompletableFuture;
4240

4341
public class OrderService extends Service {
4442

4543
private static final Logger log = LogManager.getLogger(OrderService.class);
4644

4745
/**
48-
* @param accessToken The user's access token
49-
* @param credentials The user's API credentials
46+
* @param upstoxAuthService The service to retrieve authentication details
5047
*/
51-
public OrderService(@Nonnull final AccessToken accessToken,
52-
@Nonnull final ApiCredentials credentials) {
48+
public OrderService(@Nonnull final UpstoxAuthService upstoxAuthService) {
5349

54-
super(Objects.requireNonNull(accessToken), Objects.requireNonNull(credentials));
50+
super(upstoxAuthService);
5551
}
5652

5753
/**

src/main/java/com/github/rishabh9/riko/upstox/users/UserService.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@
2525
package com.github.rishabh9.riko.upstox.users;
2626

2727
import com.github.rishabh9.riko.upstox.common.Service;
28-
import com.github.rishabh9.riko.upstox.common.models.ApiCredentials;
28+
import com.github.rishabh9.riko.upstox.common.UpstoxAuthService;
2929
import com.github.rishabh9.riko.upstox.common.models.UpstoxResponse;
30-
import com.github.rishabh9.riko.upstox.login.models.AccessToken;
3130
import com.github.rishabh9.riko.upstox.users.models.*;
3231
import com.google.common.base.Strings;
3332
import okhttp3.ResponseBody;
@@ -38,21 +37,18 @@
3837
import javax.annotation.Nullable;
3938
import java.io.InputStream;
4039
import java.util.List;
41-
import java.util.Objects;
4240
import java.util.concurrent.CompletableFuture;
4341

4442
public class UserService extends Service {
4543

4644
private static final Logger log = LogManager.getLogger(UserService.class);
4745

4846
/**
49-
* @param accessToken The user's access token
50-
* @param credentials The user's API credentials
47+
* @param upstoxAuthService The service to retrieve authentication details
5148
*/
52-
public UserService(@Nonnull final AccessToken accessToken,
53-
@Nonnull final ApiCredentials credentials) {
49+
public UserService(@Nonnull final UpstoxAuthService upstoxAuthService) {
5450

55-
super(Objects.requireNonNull(accessToken), Objects.requireNonNull(credentials));
51+
super(upstoxAuthService);
5652
}
5753

5854
/**

src/main/java/com/github/rishabh9/riko/upstox/websockets/WebSocketService.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
package com.github.rishabh9.riko.upstox.websockets;
2626

2727
import com.github.rishabh9.riko.upstox.common.Service;
28+
import com.github.rishabh9.riko.upstox.common.UpstoxAuthService;
2829
import com.github.rishabh9.riko.upstox.common.models.ApiCredentials;
2930
import com.github.rishabh9.riko.upstox.common.models.UpstoxResponse;
3031
import com.github.rishabh9.riko.upstox.login.models.AccessToken;
@@ -38,7 +39,6 @@
3839

3940
import javax.annotation.Nonnull;
4041
import java.util.List;
41-
import java.util.Objects;
4242
import java.util.concurrent.CompletableFuture;
4343
import java.util.concurrent.ExecutionException;
4444
import java.util.concurrent.TimeUnit;
@@ -50,13 +50,11 @@ public class WebSocketService extends Service {
5050
private static final Logger log = LogManager.getLogger(WebSocketService.class);
5151

5252
/**
53-
* @param accessToken The user's access token
54-
* @param credentials The user's API credentials
53+
* @param upstoxAuthService The service to retrieve authentication details
5554
*/
56-
public WebSocketService(@Nonnull final AccessToken accessToken,
57-
@Nonnull final ApiCredentials credentials) {
55+
public WebSocketService(@Nonnull final UpstoxAuthService upstoxAuthService) {
5856

59-
super(Objects.requireNonNull(accessToken), Objects.requireNonNull(credentials));
57+
super(upstoxAuthService);
6058
}
6159

6260
private CompletableFuture<UpstoxResponse<WebsocketParameters>> getWebsocketParameters() {
@@ -135,6 +133,8 @@ private Request prepareRequest() {
135133
if (!Strings.isNullOrEmpty(port)) {
136134
urlBuilder.port(Integer.parseInt(port));
137135
}
136+
final AccessToken accessToken = upstoxAuthService.getAccessToken();
137+
final ApiCredentials credentials = upstoxAuthService.getApiCredentials();
138138
urlBuilder
139139
.addQueryParameter("apiKey", credentials.getApiKey())
140140
.addQueryParameter("token", accessToken.getToken());

0 commit comments

Comments
 (0)