-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use yadio for pricing "exotic" fiat (#921)
* yadio api * add more currencies * fix es typo * bump version & fix cicleci build
- Loading branch information
Showing
16 changed files
with
537 additions
and
89 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
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
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
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
43 changes: 43 additions & 0 deletions
43
app/src/main/java/com/m2049r/xmrwallet/service/exchange/krakenFiat/ExchangeRateImpl.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,43 @@ | ||
/* | ||
* Copyright (c) 2019-2023 m2049r et al. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.m2049r.xmrwallet.service.exchange.krakenFiat; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import com.m2049r.xmrwallet.service.exchange.api.ExchangeApi; | ||
import com.m2049r.xmrwallet.service.exchange.api.ExchangeRate; | ||
|
||
import lombok.Getter; | ||
|
||
class ExchangeRateImpl implements ExchangeRate { | ||
@Getter | ||
private final String serviceName; | ||
@Getter | ||
private final String baseCurrency; | ||
@Getter | ||
private final String quoteCurrency; | ||
@Getter | ||
private final double rate; | ||
|
||
ExchangeRateImpl(@NonNull final String serviceName, @NonNull final String baseCurrency, @NonNull final String quoteCurrency, double rate) { | ||
super(); | ||
this.serviceName = serviceName; | ||
this.baseCurrency = baseCurrency; | ||
this.quoteCurrency = quoteCurrency; | ||
this.rate = rate; | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
app/src/main/java/com/m2049r/xmrwallet/service/exchange/yadio/ExchangeApiImpl.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,105 @@ | ||
/* | ||
* Copyright (c) 2019 m2049r@monerujo.io | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// https://developer.android.com/training/basics/network-ops/xml | ||
|
||
package com.m2049r.xmrwallet.service.exchange.yadio; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.VisibleForTesting; | ||
|
||
import com.m2049r.xmrwallet.service.exchange.api.ExchangeApi; | ||
import com.m2049r.xmrwallet.service.exchange.api.ExchangeCallback; | ||
import com.m2049r.xmrwallet.service.exchange.api.ExchangeException; | ||
import com.m2049r.xmrwallet.util.NetCipherHelper; | ||
|
||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
import java.io.IOException; | ||
|
||
import okhttp3.Call; | ||
import okhttp3.HttpUrl; | ||
import okhttp3.Response; | ||
import timber.log.Timber; | ||
|
||
public class ExchangeApiImpl implements ExchangeApi { | ||
@NonNull | ||
private final HttpUrl baseUrl; | ||
|
||
//so we can inject the mockserver url | ||
@VisibleForTesting | ||
public ExchangeApiImpl(@NonNull final HttpUrl baseUrl) { | ||
this.baseUrl = baseUrl; | ||
} | ||
|
||
public ExchangeApiImpl() { | ||
this(HttpUrl.parse("https://api.yadio.io/convert/1/eur")); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "yadio"; | ||
} | ||
|
||
@Override | ||
public void queryExchangeRate(@NonNull final String baseCurrency, @NonNull final String quoteCurrency, | ||
@NonNull final ExchangeCallback callback) { | ||
if (!baseCurrency.equals("EUR")) { | ||
callback.onError(new IllegalArgumentException("Only EUR supported as base")); | ||
return; | ||
} | ||
|
||
if (baseCurrency.equals(quoteCurrency)) { | ||
callback.onSuccess(new ExchangeRateImpl(quoteCurrency, 1.0, 0)); | ||
return; | ||
} | ||
|
||
final HttpUrl url = baseUrl.newBuilder() | ||
.addPathSegments(quoteCurrency.substring(0, 3)) | ||
.build(); | ||
final NetCipherHelper.Request httpRequest = new NetCipherHelper.Request(url); | ||
|
||
httpRequest.enqueue(new okhttp3.Callback() { | ||
@Override | ||
public void onFailure(@NonNull final Call call, @NonNull final IOException ex) { | ||
callback.onError(ex); | ||
} | ||
|
||
@Override | ||
public void onResponse(@NonNull final Call call, @NonNull final Response response) throws IOException { | ||
if (response.isSuccessful()) { | ||
try { | ||
assert response.body() != null; | ||
final JSONObject json = new JSONObject(response.body().string()); | ||
if (json.has("error")) { | ||
Timber.d("%d: %s", response.code(), json.getString("error")); | ||
callback.onError(new ExchangeException(response.code(), json.getString("error"))); | ||
return; | ||
} | ||
double rate = json.getDouble("rate"); | ||
long timestamp = json.getLong("timestamp"); | ||
callback.onSuccess(new ExchangeRateImpl(quoteCurrency, rate, timestamp)); | ||
} catch (JSONException ex) { | ||
callback.onError(ex); | ||
} | ||
} else { | ||
callback.onError(new ExchangeException(response.code(), response.message())); | ||
} | ||
} | ||
}); | ||
} | ||
} |
Oops, something went wrong.