Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
crazzyghost committed Nov 16, 2018
1 parent 637ced9 commit 85f7a08
Show file tree
Hide file tree
Showing 34 changed files with 1,989 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
.idea/**/misc.xml

.settings
# Generated files
.idea/**/contentModel.xml

Expand Down Expand Up @@ -125,4 +127,9 @@ gradle-app.setting
### Gradle Patch ###
**/build/

.project
.classpath
gradlew
gradlew.bat
gradle
# End of https://www.gitignore.io/api/java,gradle,intellij
18 changes: 18 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
plugins {
id 'java'
}

group 'crazzyghost'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
mavenCentral()
}

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation 'com.squareup.moshi:moshi:1.8.0'
}
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rootProject.name = 'alphavantage'

51 changes: 51 additions & 0 deletions src/main/java/com/crazzyghost/alphavantage/AlphaVantage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.crazzyghost.alphavantage;

import com.crazzyghost.alphavantage.exchangerate.ExchangeRate;
import com.crazzyghost.alphavantage.forex.Forex;
import com.crazzyghost.alphavantage.timeseries.TimeSeries;

public class AlphaVantage {

private static AlphaVantage INSTANCE;
private Config config;
private TimeSeries timeSeries;
private Forex forex;
private ExchangeRate exchangeRate;

private AlphaVantage(Config config){
this.config = config;
}

public static void init(Config config){
INSTANCE = new AlphaVantage(config);
}

public static AlphaVantage api(){
if(INSTANCE == null){
throw new AlphaVantageException("Call AlphaVantage.init");
}
return INSTANCE;
}

public TimeSeries timeSeries(){
if(timeSeries == null){
timeSeries = new TimeSeries(config);
}
return timeSeries;
}

public Forex forex(){
if(forex == null){
forex = new Forex(config);
}
return forex;
}


public ExchangeRate exchangeRate() {
if(exchangeRate == null){
exchangeRate = new ExchangeRate(config);
}
return exchangeRate;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.crazzyghost.alphavantage;

public class AlphaVantageException extends RuntimeException{

public AlphaVantageException(){
super();
}

public AlphaVantageException(String msg){
super(msg);
}

}
44 changes: 44 additions & 0 deletions src/main/java/com/crazzyghost/alphavantage/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.crazzyghost.alphavantage;

public class Config {

private String key;
private int timeOut;
public static String BASE_URL = "https://www.alphavantage.co/query?";

public Config(Builder builder) {
this.key = builder.key;
this.timeOut = builder.timeOut;
}

public int getTimeOut() {
return timeOut;
}

public String getKey() {
return key;
}

public static Builder builder(){
return new Builder();
}

public static class Builder{
private String key;
private int timeOut;

public Builder key(String key){
this.key = key;
return this;
}

public Builder timeOut(int timeOut){
this.timeOut = timeOut;
return this;
}

public Config build(){
return new Config(this);
}
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/crazzyghost/alphavantage/Fetcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.crazzyghost.alphavantage;

public interface Fetcher{

void fetch();

interface SuccessCallback<V>{
void onSuccess(V response);
}

interface FailureCallback{
void onFailure(AlphaVantageException ex);
}
}
34 changes: 34 additions & 0 deletions src/main/java/com/crazzyghost/alphavantage/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.crazzyghost.alphavantage;


import com.crazzyghost.alphavantage.parameters.DataType;
import com.crazzyghost.alphavantage.parameters.Interval;

public class Main {



public static void main(String[] args){


Config config = Config.builder()
.key("M77PQNYAVBG0MB5N")
.timeOut(5)
.build();

AlphaVantage.init(config);


AlphaVantage.api()
.timeSeries()
.monthly()
.onFailure(System.out::println)
.adjusted()
.onSuccess(System.out::println)
.forSymbol("AAPL")
.fetch();

}


}
39 changes: 39 additions & 0 deletions src/main/java/com/crazzyghost/alphavantage/UrlExtractor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.crazzyghost.alphavantage;

import java.lang.reflect.Field;


public class UrlExtractor{

public static String extract(Object object){

StringBuilder stringBuilder = new StringBuilder();

Class cls = object.getClass();
while(cls != null){
Field[] fields = cls.getDeclaredFields();
for(Field field : fields){
field.setAccessible(true);
try {

if (field.get(object) != null){
stringBuilder.append(field.getName())
.append("=");
if(field.getType().isEnum()){
String value = (field.get(object)).toString();
stringBuilder.append(value).append("&");
}else{
stringBuilder.append((String)field.get(object)).append("&");
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
cls = cls.getSuperclass();
}

return stringBuilder.append("apikey=").toString();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package com.crazzyghost.alphavantage.exchangerate;

import com.crazzyghost.alphavantage.AlphaVantageException;
import com.crazzyghost.alphavantage.Fetcher;
import com.crazzyghost.alphavantage.Config;
import com.crazzyghost.alphavantage.UrlExtractor;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;
import com.squareup.moshi.Types;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class ExchangeRate implements Fetcher {

private Config config;
private ExchangeRateRequest request;
private ExchangeRateRequest.Builder builder;
private Fetcher.SuccessCallback successCallback;
private Fetcher.FailureCallback failureCallback;

public ExchangeRate(Config config){
this.config = config;
this.request = null;
this.builder = ExchangeRateRequest.builder();
}

public ExchangeRate toCurrency(String toCurrency){
this.builder.toCurrency(toCurrency);
return this;
}

public ExchangeRate fromCurrency(String fromCurrency){
this.builder.fromCurrency(fromCurrency);
return this;
}

/**
* @param callback successful fetch handler
* @return current instance of {@link ExchangeRateResponse}
*/
public ExchangeRate onSuccess(SuccessCallback callback){
this.successCallback = callback;
return this;
}

/**
* @param callback failed fetch handler
* @return current instance of {@link ExchangeRateResponse}
*/
public ExchangeRate onFailure(FailureCallback callback){
this.failureCallback = callback;
return this;
}

@Override
public void fetch() {

//make sure the key is set
if(config.getKey() == null){
throw new AlphaVantageException("Config not set");
}
//build the api request parameters object finally
this.request = this.builder.build();
//okhttp
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(config.getTimeOut(), TimeUnit.SECONDS)
.build();

Request request = new Request.Builder()
.url(Config.BASE_URL + UrlExtractor.extract(this.request) + config.getKey())
.build();

System.out.println(Config.BASE_URL + UrlExtractor.extract(this.request) + config.getKey());

//make the call
System.out.println("Fetching Response ...");
client.newCall(request).enqueue(new okhttp3.Callback() {
@Override
public void onFailure(Call call, IOException e) {
//respond to callback on failure
System.out.println("Failed Fetching Response ... " + e.getClass().getCanonicalName());
if(failureCallback != null){
failureCallback.onFailure(new AlphaVantageException());
}
}

@Override
public void onResponse(Call call, Response response) throws IOException {
System.out.println("Received Response.");
if(response.isSuccessful()){

Moshi moshi = new Moshi.Builder().build();
Type type = Types.newParameterizedType(Map.class, String.class, Object.class);
JsonAdapter<Map<String,Object>> adapter = moshi.adapter(type);
ExchangeRateResponse exchangeResponse = ExchangeRateResponse.of(adapter.fromJson(response.body().string()));
if(exchangeResponse.getErrorMessage() != null){
if(failureCallback != null){
failureCallback.onFailure(new AlphaVantageException(exchangeResponse.getErrorMessage()));
}
System.err.println(exchangeResponse.getErrorMessage());
return;
}
if(successCallback != null)
successCallback.onSuccess(exchangeResponse);
System.out.println("Success Fetching Response!");
}else{

if(failureCallback != null){
failureCallback.onFailure(new AlphaVantageException());
}
System.err.println("Error Fetching Response.");
}
}
});
}
}
Loading

0 comments on commit 85f7a08

Please sign in to comment.