Proof of concept: read pricing stream from Oanda
This commit is contained in:
@ -1,12 +1,75 @@
|
||||
/*
|
||||
* This Java source file was generated by the Gradle 'init' task.
|
||||
*/
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.net.URLEncoder;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
//import com.espertech.esper.client.*;
|
||||
|
||||
public class App {
|
||||
public String getGreeting() {
|
||||
return "Hello world.";
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new App().getGreeting());
|
||||
|
||||
//EPServiceProvider engine = EPServiceProviderManager.getDefaultProvider();
|
||||
|
||||
try {
|
||||
String[] instruments = new String[] {"USD_CAD", "EUR_USD", "USD_JPY"};
|
||||
|
||||
String query = String.format("instruments=%s",
|
||||
URLEncoder.encode(String.join(",", instruments), "UTF-8"));
|
||||
|
||||
openConnection("https://stream-fxpractice.oanda.com/v3/accounts/101-001-7935538-001/pricing/stream?" + query);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static void openConnection(String urlStr)
|
||||
throws IOException
|
||||
{
|
||||
HttpsURLConnection httpConn = null;
|
||||
String line = null;
|
||||
try {
|
||||
URL url = new URL(urlStr);
|
||||
URLConnection urlConn = url.openConnection();
|
||||
if (!(urlConn instanceof HttpsURLConnection)) {
|
||||
throw new IOException ("URL is not an Https URL");
|
||||
}
|
||||
httpConn = (HttpsURLConnection)urlConn;
|
||||
httpConn.setAllowUserInteraction(false);
|
||||
httpConn.setInstanceFollowRedirects(true);
|
||||
httpConn.setRequestMethod("GET");
|
||||
httpConn.setReadTimeout(50 * 1000);
|
||||
httpConn.setRequestProperty("Authorization", "Bearer 9a480f0b83e987f4015cf0846790c7d9-695ced635526744abd61bdf0e2ae8b71");
|
||||
BufferedReader is =
|
||||
new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
|
||||
|
||||
while ((line = is.readLine( )) != null) {
|
||||
System.out.println(line);
|
||||
// Message msg = Message.obtain();
|
||||
// msg.what=1;
|
||||
// msg.obj=line;
|
||||
// mhandler.sendMessage(msg);
|
||||
|
||||
}
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
} catch(SocketTimeoutException e){
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
//Message msg = Message.obtain();
|
||||
//msg.what=2;
|
||||
//BufferedInputStream in = new BufferedInputStream(httpConn.getErrorStream());
|
||||
//line = new String(readStream(in));
|
||||
//msg.obj = line;
|
||||
//mhandler.sendMessage(msg);
|
||||
} finally {
|
||||
httpConn.disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -7,6 +7,6 @@ import static org.junit.Assert.*;
|
||||
public class AppTest {
|
||||
@Test public void testAppHasAGreeting() {
|
||||
App classUnderTest = new App();
|
||||
assertNotNull("app should have a greeting", classUnderTest.getGreeting());
|
||||
//assertNotNull("app should have a greeting", classUnderTest.getGreeting());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user