-
Notifications
You must be signed in to change notification settings - Fork 49
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
code cleanup along with ws 2.0 #7
base: main
Are you sure you want to change the base?
code cleanup along with ws 2.0 #7
Conversation
81432a6
to
ccabfcc
Compare
README.md
Outdated
public void getOrder(SmartConnect smartConnect) throws SmartAPIException, IOException { | ||
List<Order> orders = smartConnect.getOrderHistory(smartConnect.getUserId()); | ||
for (Order order : orders) { | ||
logger.info(order.orderId + " " + order.status); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use logger.info("{} {}", order.orderId, order.status)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
have replaced String concatenation with place holder
log.info("{} {}", order.orderId, order.status);
README.md
Outdated
// Returns tradebook. | ||
List<Trade> trades = smartConnect.getTrades(); | ||
for (Trade trade : trades) { | ||
logger.info(trade.tradingSymbol + " " + trades.size()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
have replaced String concatenation with place holder
log.info("{} {}", trade.tradingSymbol, trades.size());
import java.util.Map; | ||
|
||
/** | ||
* Generates end-points for all smart api calls. | ||
* | ||
* <p> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
have removed the "< p >" from comments
|
||
@Data |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need this? Ideally the request body for smartConnect and the methods should be segregated into 2 files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
have segregated SmartConnect into 2 files SmartConnect and SmartConnectParams
POJO methods have been moved to SmartConnectParams
} | ||
} | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(SmartConnect.class); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use logger from lombok @slf4j
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
have used @slf4j instead of private static final Logger logger = LoggerFactory.getLogger(SmartConnect.class);
|
||
// initialize 2fa exception and call constructor of Base Exception | ||
public DataException(String message, String code){ | ||
public DataSmartAPIException(String message, String code){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the above comment is required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment is been removed
public Integer timePeriod; | ||
|
||
private Integer timePeriod; | ||
|
||
@Override | ||
public String toString() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In almost all the model classes we see this toString, Are we using this toString somewhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unnecessary toString is been removed
public void getHolding(SmartConnect smartConnect) throws SmartAPIException { | ||
// Returns Holding. | ||
JSONObject response = smartConnect.getHolding(); | ||
logger.debug("getHolding {}", response); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned last time, please remove these debug statements. Please review the dentire code for these debug statements.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
have removed some debug statements and replaced some with log.info or log.error if necessary.
|
||
public class LoginWithTOTPSample { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(LoginWithTOTPSample.class); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use logger from @slf4j
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
have used @slf4j
(quote.getLowPrice() / 100.0), | ||
(quote.getClosePrice() / 100.0), | ||
getExchangeTime(quote.getExchangeFeedTimeEpochMillis()), | ||
Instant.now().toEpochMilli() - quote.getExchangeFeedTimeEpochMillis()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are not making any asserts in these tests. What are we really testing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SmartStreamListenerImpl is not a test class it is used in SmartStreamTicker.
SmartStreamTicker is used for connecting with the webSocket
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please review the comments..
README.md
Outdated
|
||
JSONObject requestObejct = new JSONObject(); | ||
JSONObject requestObejct = new JSONObject(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use Model class instead of JSONObject
@@ -100,7 +100,7 @@ <h2 title="Class Routes" class="title">Class Routes</h2> | |||
<li>java.lang.Object</li> | |||
<li> | |||
<ul class="inheritance"> | |||
<li>com.angelbroking.smartapi.Routes</li> | |||
<li>com.angelbroking.smartapi.routes.Routes</li> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you change this?
} catch (IOException ex) { | ||
log.error("{} {}", IO_EXCEPTION_OCCURRED, ex.getMessage()); | ||
throw new IOException(String.format("%s: %s", IO_EXCEPTION_ERROR_MSG, ex.getMessage())); | ||
} catch (JSONException ex) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think we should catch JSONException, not able see any json inside method
orderParams.setSymbolToken("3045"); | ||
orderParams.setProductType("INTRADAY"); | ||
orderParams.setOrderType("LIMIT"); | ||
HttpResponse order = smartConnect.placeOrder(orderParams, "NORMAL"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Map to model instead of HttpResponse
public static final String LOGIN_URL = "https://apiconnect.angelbroking.com/rest/auth/angelbroking/user/v1/loginByPassword"; | ||
public static final String WSURI = "wss://wsfeeds.angelbroking.com/NestHtml5Mobile/socket/stream"; | ||
// public static final String SMARTSTREAM_WSURI = "ws://smartapisocket.angelone.in/smart-stream"; | ||
public static final String SMARTSTREAM_WSURI = "ws://mds-uat.angelone.in/smart-stream"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is UAT URL. Please switch to prod URL
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
have changed SMARTSTREAM_WSURI from ws://mds-uat.angelone.in/smart-stream to ws://smartapisocket.angelone.in/smart-stream
return ResponseParser.parseResponse(smartAPIRequestHandler.getRequest(smartConnectParams.getApiKey(), url, smartConnectParams.getAccessToken())); | ||
|
||
} catch (IOException ex) { | ||
log.error("{} {}", IO_EXCEPTION_OCCURRED, ex.getMessage()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
log messages should clearly denote that the error occurred while doing which operation?
In case an issue arises in the future, how can we find out that the error occurred while doing which operation?
Log should look something like "Error occurred while getting user profile" with ex.getMessage()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be addressed at all places.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
have done changes in SmartConnect class for all log messages as well as for exception
try { | ||
HttpResponse httpResponse = smartAPIRequestHandler.postRequest(smartConnectParams.getApiKey(), routes.getLoginUrl(), Utils.createLoginParams(clientCode, password, totp)); | ||
responseDTO = new ObjectMapper().readValue(httpResponse.getBody(), UserResponseDTO.class); | ||
String url = routes.get(SMART_CONNECT_API_USER_PROFILE); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we please split this into 2 try catch blocks. One for postRequest and the other one for parsing the object. This is creating unnecessary confusion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed, will be using a single-try catch block.
public HttpResponse placeOrder(OrderParams orderParams, String variety) throws SmartAPIException, IOException { | ||
try { | ||
Validators validator = new Validators(); | ||
orderParams.setVariety(variety); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please put the line which is required to be in the try-catch block. L141-L144 need not be inside the try catch block.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be done at all places.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
have removed unnecessary code from try-catch block
Routes routes = new Routes(); | ||
try { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append(routes.getSWsuri()).append("?jwttoken=").append(this.jwtToken).append("&&clientcode=").append(this.clientId).append("&&apikey=").append(this.apiKey); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move constants to separate file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
have moved to constants
switch (mode) { | ||
case LTP: { | ||
ByteBuffer packet = ByteBuffer.wrap(binary).order(ByteOrder.LITTLE_ENDIAN); | ||
LTP ltp = ByteUtils.mapByteBufferToLTP(packet); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can simply write it as mapByteBufferToLTP(packet) instead of ByteUtils.mapByteBufferToLTP(packet);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done for LTP,Quote,Snap_quote
// Added a private constructor to hide the implicit public one. | ||
|
||
|
||
private ResponseParser() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you want me to remove the constructor or comment or both?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as discussed have removed the comments
if (macAddressBytes != null) { | ||
StringBuilder macAddressStr = new StringBuilder(); | ||
for (int i = 0; i < macAddressBytes.length; i++) { | ||
macAddressStr.append(String.format("%02X%s", macAddressBytes[i], (i < macAddressBytes.length - 1) ? "-" : "")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move constants.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
log.error("Error loading configuration file: {}", e.getMessage()); | ||
throw new IOException("Failed to load configuration file"); | ||
} | ||
URL urlName = new URL(properties.getProperty("url")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Constants.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
|
The code improvements and changes are based on the JIRA ticket https://angelbrokingpl.atlassian.net/browse/SSS-13 and the feedback from the pull request kadam-mithun#1.
The following changes are implemented: