-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
637ced9
commit 85f7a08
Showing
34 changed files
with
1,989 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
rootProject.name = 'alphavantage' | ||
|
51 changes: 51 additions & 0 deletions
51
src/main/java/com/crazzyghost/alphavantage/AlphaVantage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/crazzyghost/alphavantage/AlphaVantageException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
39
src/main/java/com/crazzyghost/alphavantage/UrlExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
src/main/java/com/crazzyghost/alphavantage/exchangerate/ExchangeRate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} | ||
}); | ||
} | ||
} |
Oops, something went wrong.