Skip to content

Commit

Permalink
Merge pull request #5 from theselfspace/B001-login-bugfix
Browse files Browse the repository at this point in the history
Login bugfix and option to disable headless mode
  • Loading branch information
theselfspace authored Nov 17, 2022
2 parents 2cf58fd + 4cc8a8e commit fc9d7d3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>space.theself</groupId>
<artifactId>fyers-java</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>

<name>space.theself:fyers-java</name>

Expand Down Expand Up @@ -47,7 +47,7 @@
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<configuration>
<javadocExecutable>${java.home}/../bin/javadoc</javadocExecutable>
<javadocExecutable>${java.home}/bin/javadoc</javadocExecutable>
</configuration>
<executions>
<execution>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/trade/theself/fyers/FyersFly.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ public String generateAccessToken(String authCode) throws LoginException {

try {
CloseableHttpResponse response = client.execute(httpPost);
client.close();
InputStream is = response.getEntity().getContent();
StringBuffer sb = new StringBuffer();
byte[] allBytes = ByteStreams.toByteArray(is);
is.close();
client.close();
sb.append(new String(allBytes, StandardCharsets.US_ASCII));
JsonElement jsonElement = jsonParser.parse(sb.toString());
this.accessToken = jsonElement.getAsJsonObject().get("access_token").getAsString();
Expand Down
34 changes: 23 additions & 11 deletions src/main/java/trade/theself/fyers/boilerplate/LoginHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -39,7 +41,7 @@ public class LoginHandler {
private String clientCode = StringUtils.EMPTY;
private String redirectUrl = StringUtils.EMPTY;
private String state = "sample_state";

private boolean enableHeadless = true;
/**
* Login to the portal through browser path.. This could break if Fyers decide to update their UI
* @param clientId - Client ID of the user
Expand Down Expand Up @@ -67,15 +69,17 @@ public String login(String clientId, String otp, String totp) throws LoginExcept

WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.addArguments(
"--headless",
"--disable-gpu",
"--ignore-certificate-errors",
"--disable-extensions",
"--no-sandbox",
"--disable-dev-shm-usage",
"--incognito");

List<String> argList = new ArrayList<>();
argList.add("--disable-gpu");
argList.add("--ignore-certificate-errors");
argList.add("--disable-extensions");
argList.add("--no-sandbox");
argList.add("--disable-dev-shm-usage");
argList.add("--incognito");
if(enableHeadless)
argList.add("--headless");
options.addArguments(argList);

WebDriver driver = new ChromeDriver(options);
WebDriverWait wait = new WebDriverWait(driver, 30);
driver.get(loginUrl);
Expand Down Expand Up @@ -158,8 +162,10 @@ public String login(String clientId, String otp, String totp) throws LoginExcept
try {
List<NameValuePair> params = URLEncodedUtils.parse(new URI(currentUrl), Charset.forName("UTF-8"));
for (NameValuePair param : params) {
if(param.getName().equals("auth_code"))
if(param.getName().equals("auth_code")) {
authCode = param.getValue();
break;
}
}
} catch (URISyntaxException e) {
throw new LoginException(e.getMessage());
Expand Down Expand Up @@ -214,4 +220,10 @@ public void setState(String state) {
this.state = state;
}

/**
* Method primarily to disable headless mode in case any browser related debugging is required.
* @param state
*/
public void setEnableHeadless(boolean state) { this.enableHeadless = state;}

}

0 comments on commit fc9d7d3

Please sign in to comment.