Skip to content

Commit

Permalink
Add UsptoxAuthService, update LoginService
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabh9 committed Oct 24, 2018
1 parent 0dfc2c3 commit 5e90d6f
Show file tree
Hide file tree
Showing 15 changed files with 313 additions and 672 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ Checkout the [wiki](https://github.com/rishabh9/riko/wiki) for more details.
<dependency>
<groupId>com.github.rishabh9</groupId>
<artifactId>riko</artifactId>
<version>1.0.2</version>
<version>2.0.0</version>
</dependency>
```

### For Gradle based project
```groovy
dependencies {
implementation 'com.github.rishabh9:riko:1.0.2'
implementation 'com.github.rishabh9:riko:2.0.0'
}
```

### For SBT based project
```scala
libraryDependencies += "com.github.rishabh9" % "riko" % "1.0.2"
libraryDependencies += "com.github.rishabh9" % "riko" % "2.0.0"
```
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ dependencyManagement {
// GroupId
group = 'com.github.rishabh9'
// Version
version = '1.0.2'
version = '2.0.0'
archivesBaseName = 'riko'

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,22 @@ public abstract class Service {

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

protected final AccessToken accessToken;
protected final ApiCredentials credentials;
protected final UpstoxAuthService upstoxAuthService;

/**
* @param accessToken The user's access token
* @param credentials The user's API credentials
* @param upstoxAuthService The service to retrieve authentication details
*/
public Service(@Nonnull final AccessToken accessToken,
@Nonnull final ApiCredentials credentials) {
public Service(@Nonnull final UpstoxAuthService upstoxAuthService) {

this.accessToken = Objects.requireNonNull(accessToken);
this.credentials = Objects.requireNonNull(credentials);
this.upstoxAuthService = Objects.requireNonNull(upstoxAuthService);
}

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

log.debug("Preparing service API: {}", type.getName());
final AccessToken accessToken = upstoxAuthService.getAccessToken();
final ApiCredentials credentials = upstoxAuthService.getApiCredentials();

final String token = accessToken.getType() + " " + accessToken.getToken();
return ServiceGenerator.getInstance()
.createService(type, new AuthHeaders(token, credentials.getApiKey()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* MIT License
*
* Copyright (c) 2018 Rishabh Joshi
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.github.rishabh9.riko.upstox.common;

import com.github.rishabh9.riko.upstox.common.models.ApiCredentials;
import com.github.rishabh9.riko.upstox.login.models.AccessToken;

/**
* The implementations of this interface are responsible to <em>always return
* the latest version</em> of the {@link ApiCredentials} and {@link AccessToken}.
*/
public interface UpstoxAuthService {

/**
* @return The latest API Key and API Secret of an Upstox account.
*/
ApiCredentials getApiCredentials();

/**
* @return The latest access token
*/
AccessToken getAccessToken();
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,27 @@
package com.github.rishabh9.riko.upstox.feed;

import com.github.rishabh9.riko.upstox.common.Service;
import com.github.rishabh9.riko.upstox.common.models.ApiCredentials;
import com.github.rishabh9.riko.upstox.common.UpstoxAuthService;
import com.github.rishabh9.riko.upstox.common.models.UpstoxResponse;
import com.github.rishabh9.riko.upstox.feed.models.Feed;
import com.github.rishabh9.riko.upstox.feed.models.SubscriptionResponse;
import com.github.rishabh9.riko.upstox.login.models.AccessToken;
import com.google.common.base.Strings;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.annotation.Nonnull;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;

public class FeedService extends Service {

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

public FeedService(@Nonnull final AccessToken accessToken,
@Nonnull final ApiCredentials credentials) {
/**
* @param upstoxAuthService The service to retrieve authentication details
*/
public FeedService(@Nonnull final UpstoxAuthService upstoxAuthService) {

super(Objects.requireNonNull(accessToken), Objects.requireNonNull(credentials));
super(upstoxAuthService);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,27 @@
package com.github.rishabh9.riko.upstox.historical;

import com.github.rishabh9.riko.upstox.common.Service;
import com.github.rishabh9.riko.upstox.common.models.ApiCredentials;
import com.github.rishabh9.riko.upstox.common.UpstoxAuthService;
import com.github.rishabh9.riko.upstox.common.models.UpstoxResponse;
import com.github.rishabh9.riko.upstox.historical.models.Candle;
import com.github.rishabh9.riko.upstox.login.models.AccessToken;
import com.google.common.base.Strings;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.annotation.Nonnull;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;

public class HistoricalService extends Service {

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

public HistoricalService(@Nonnull final AccessToken accessToken,
@Nonnull final ApiCredentials credentials) {
/**
* @param upstoxAuthService The service to retrieve authentication details
*/
public HistoricalService(@Nonnull final UpstoxAuthService upstoxAuthService) {

super(Objects.requireNonNull(accessToken), Objects.requireNonNull(credentials));
super(upstoxAuthService);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
package com.github.rishabh9.riko.upstox.login;

import com.github.rishabh9.riko.upstox.common.ServiceGenerator;
import com.github.rishabh9.riko.upstox.common.models.ApiCredentials;
import com.github.rishabh9.riko.upstox.common.UpstoxAuthService;
import com.github.rishabh9.riko.upstox.login.models.AccessToken;
import com.github.rishabh9.riko.upstox.login.models.TokenRequest;
import com.google.common.base.Strings;
Expand All @@ -35,66 +35,42 @@
import javax.annotation.Nonnull;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class LoginService {

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

private final TokenRequest request;

private final ApiCredentials credentials;
private final UpstoxAuthService upstoxAuthService;

/**
* @param request The TokenRequest object containing all fields,
* including the access code received after authenticating the user on Upstox site.
* @param credentials The API key and secret.
* @param upstoxAuthService The service to retrieve authentication details.
*/
public LoginService(@Nonnull final TokenRequest request,
@Nonnull final ApiCredentials credentials) {
public LoginService(@Nonnull final UpstoxAuthService upstoxAuthService) {

this.request = Objects.requireNonNull(request);
this.credentials = Objects.requireNonNull(credentials);
this.upstoxAuthService = Objects.requireNonNull(upstoxAuthService);
}

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

if (Strings.isNullOrEmpty(request.getCode())) {
if (Strings.isNullOrEmpty(Objects.requireNonNull(request).getCode())) {
throw new IllegalArgumentException(
"Missing value for authorization code. Code: " + request.getCode());
}

// Create a very simple REST adapter which points the Upstox API endpoint.
LoginApi loginApi =
final LoginApi loginApi =
ServiceGenerator.getInstance().createService(
LoginApi.class,
credentials.getApiKey(),
credentials.getApiSecret());

CompletableFuture<AccessToken> future = loginApi.getAccessToken(request);
upstoxAuthService.getApiCredentials().getApiKey(),
upstoxAuthService.getApiCredentials().getApiSecret());

// Make a synchronous call.
return future
// .exceptionally(
// (throwable) -> {
// if (null != throwable) {
// if (throwable instanceof HttpException) { // Non 2XX HTTP Response Code
// log.error("Request to retrieve access code was unsuccessful",
// throwable);
// } else if (throwable instanceof IOError) { // Network Error
// log.error("A network error occurred while trying to retrieve access code",
// throwable);
// } else {
// log.error("Unexpected error has occurred", throwable);
// }
// }
// return null;
// })
.get();
return loginApi.getAccessToken(request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@
package com.github.rishabh9.riko.upstox.orders;

import com.github.rishabh9.riko.upstox.common.Service;
import com.github.rishabh9.riko.upstox.common.models.ApiCredentials;
import com.github.rishabh9.riko.upstox.common.UpstoxAuthService;
import com.github.rishabh9.riko.upstox.common.models.UpstoxResponse;
import com.github.rishabh9.riko.upstox.login.models.AccessToken;
import com.github.rishabh9.riko.upstox.orders.models.Order;
import com.github.rishabh9.riko.upstox.orders.models.OrderRequest;
import com.github.rishabh9.riko.upstox.orders.models.Trade;
Expand All @@ -37,21 +36,18 @@

import javax.annotation.Nonnull;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;

public class OrderService extends Service {

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

/**
* @param accessToken The user's access token
* @param credentials The user's API credentials
* @param upstoxAuthService The service to retrieve authentication details
*/
public OrderService(@Nonnull final AccessToken accessToken,
@Nonnull final ApiCredentials credentials) {
public OrderService(@Nonnull final UpstoxAuthService upstoxAuthService) {

super(Objects.requireNonNull(accessToken), Objects.requireNonNull(credentials));
super(upstoxAuthService);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@
package com.github.rishabh9.riko.upstox.users;

import com.github.rishabh9.riko.upstox.common.Service;
import com.github.rishabh9.riko.upstox.common.models.ApiCredentials;
import com.github.rishabh9.riko.upstox.common.UpstoxAuthService;
import com.github.rishabh9.riko.upstox.common.models.UpstoxResponse;
import com.github.rishabh9.riko.upstox.login.models.AccessToken;
import com.github.rishabh9.riko.upstox.users.models.*;
import com.google.common.base.Strings;
import okhttp3.ResponseBody;
Expand All @@ -38,21 +37,18 @@
import javax.annotation.Nullable;
import java.io.InputStream;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;

public class UserService extends Service {

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

/**
* @param accessToken The user's access token
* @param credentials The user's API credentials
* @param upstoxAuthService The service to retrieve authentication details
*/
public UserService(@Nonnull final AccessToken accessToken,
@Nonnull final ApiCredentials credentials) {
public UserService(@Nonnull final UpstoxAuthService upstoxAuthService) {

super(Objects.requireNonNull(accessToken), Objects.requireNonNull(credentials));
super(upstoxAuthService);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package com.github.rishabh9.riko.upstox.websockets;

import com.github.rishabh9.riko.upstox.common.Service;
import com.github.rishabh9.riko.upstox.common.UpstoxAuthService;
import com.github.rishabh9.riko.upstox.common.models.ApiCredentials;
import com.github.rishabh9.riko.upstox.common.models.UpstoxResponse;
import com.github.rishabh9.riko.upstox.login.models.AccessToken;
Expand All @@ -38,7 +39,6 @@

import javax.annotation.Nonnull;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
Expand All @@ -50,13 +50,11 @@ public class WebSocketService extends Service {
private static final Logger log = LogManager.getLogger(WebSocketService.class);

/**
* @param accessToken The user's access token
* @param credentials The user's API credentials
* @param upstoxAuthService The service to retrieve authentication details
*/
public WebSocketService(@Nonnull final AccessToken accessToken,
@Nonnull final ApiCredentials credentials) {
public WebSocketService(@Nonnull final UpstoxAuthService upstoxAuthService) {

super(Objects.requireNonNull(accessToken), Objects.requireNonNull(credentials));
super(upstoxAuthService);
}

private CompletableFuture<UpstoxResponse<WebsocketParameters>> getWebsocketParameters() {
Expand Down Expand Up @@ -135,6 +133,8 @@ private Request prepareRequest() {
if (!Strings.isNullOrEmpty(port)) {
urlBuilder.port(Integer.parseInt(port));
}
final AccessToken accessToken = upstoxAuthService.getAccessToken();
final ApiCredentials credentials = upstoxAuthService.getApiCredentials();
urlBuilder
.addQueryParameter("apiKey", credentials.getApiKey())
.addQueryParameter("token", accessToken.getToken());
Expand Down
Loading

0 comments on commit 5e90d6f

Please sign in to comment.