Updated on 2026-08-14
This commit is contained in:
parent
3694c32695
commit
edbeb5f3bd
18 changed files with 145 additions and 393 deletions
|
|
@ -1,6 +1,6 @@
|
|||
package com.tangem.data.network;
|
||||
|
||||
import com.tangem.data.network.model.RateInfoModel;
|
||||
import com.tangem.data.network.model.RateInfoResponse;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
|
@ -9,5 +9,5 @@ import retrofit2.http.GET;
|
|||
|
||||
public interface CoinmarketApi {
|
||||
@GET(Server.ApiCoinmarket.Method.V1_TICKER_CONVERT)
|
||||
Observable<List<RateInfoModel>> getRateInfoList();
|
||||
}
|
||||
Observable<List<RateInfoResponse>> getRateInfoList();
|
||||
}
|
||||
|
|
@ -1,98 +0,0 @@
|
|||
package com.tangem.data.network;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.volley.AuthFailureError;
|
||||
import com.android.volley.NetworkResponse;
|
||||
import com.android.volley.ParseError;
|
||||
import com.android.volley.Request;
|
||||
import com.android.volley.Response;
|
||||
import com.android.volley.Response.ErrorListener;
|
||||
import com.android.volley.Response.Listener;
|
||||
import com.android.volley.toolbox.HttpHeaderParser;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Volley adapter for JSON requests with POST method that will be parsed into Java objects by Gson.
|
||||
*/
|
||||
public class GsonRequest<T> extends Request<T> {
|
||||
public static final String TAG = "api." + GsonRequest.class.getSimpleName();
|
||||
private Gson mGson = new Gson();
|
||||
private Class<T> clazz;
|
||||
private Map<String, String> headers;
|
||||
private Map<String, String> params;
|
||||
private Listener<T> listener;
|
||||
|
||||
/**
|
||||
* Make a GET request and return a parsed object from JSON.
|
||||
*
|
||||
* @param url URL of the request to make
|
||||
* @param clazz Relevant class object, for Gson's reflection
|
||||
*/
|
||||
public GsonRequest(int method,
|
||||
String url,
|
||||
Class<T> clazz,
|
||||
Listener<T> listener,
|
||||
ErrorListener errorListener) {
|
||||
super(method, url, errorListener);
|
||||
this.clazz = clazz;
|
||||
this.listener = listener;
|
||||
mGson = new Gson();
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a POST request and return a parsed object from JSON.
|
||||
*
|
||||
* @param url URL of the request to make
|
||||
* @param clazz Relevant class object, for Gson's reflection
|
||||
*/
|
||||
public GsonRequest(int method,
|
||||
String url,
|
||||
Class<T> clazz,
|
||||
Map<String, String> params,
|
||||
Listener<T> listener,
|
||||
ErrorListener errorListener) {
|
||||
|
||||
super(method, url, errorListener);
|
||||
this.clazz = clazz;
|
||||
this.params = params;
|
||||
this.listener = listener;
|
||||
this.headers = null;
|
||||
mGson = new Gson();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> getHeaders() throws AuthFailureError {
|
||||
return headers != null ? headers : super.getHeaders();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Map<String, String> getParams() throws AuthFailureError {
|
||||
return params;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void deliverResponse(T response) {
|
||||
listener.onResponse(response);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Response<T> parseNetworkResponse(NetworkResponse response) {
|
||||
try {
|
||||
String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
|
||||
Log.i(TAG, (json));
|
||||
return Response.success(mGson.fromJson(json, clazz), HttpHeaderParser.parseCacheHeaders(response));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
Log.e(TAG, "UnsupportedEncodingException");
|
||||
return Response.error(new ParseError(e));
|
||||
} catch (JsonSyntaxException e) {
|
||||
Log.e(TAG, "JsonSyntaxException");
|
||||
return Response.error(new ParseError(e));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -15,7 +15,7 @@ public class Server {
|
|||
* https://tangem-webapp.appspot.com/
|
||||
*/
|
||||
public static class ApiTangem {
|
||||
private static final String URL_TANGEM = ServerURL.API_TANGEM;
|
||||
public static final String URL_TANGEM = ServerURL.API_TANGEM;
|
||||
|
||||
public static class Method {
|
||||
public static final String INFO_VERIFY = URL_TANGEM + "info/version";
|
||||
|
|
@ -34,11 +34,7 @@ public class Server {
|
|||
|
||||
public static class Method {
|
||||
public static final String V1_TICKER_CONVERT = URL_COINMARKET + "v1/ticker/?convert=USD&lmit=10";
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -1,15 +1,26 @@
|
|||
package com.tangem.data.network;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.util.Log;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
|
||||
import com.tangem.data.network.model.RateInfoModel;
|
||||
import com.tangem.data.network.model.CardVerify;
|
||||
import com.tangem.data.network.model.CardVerifyBody;
|
||||
import com.tangem.data.network.model.CardVerifyResponse;
|
||||
import com.tangem.data.network.model.RateInfoResponse;
|
||||
import com.tangem.data.network.request.ElectrumRequest;
|
||||
import com.tangem.domain.BitcoinNode;
|
||||
import com.tangem.domain.BitcoinNodeTestNet;
|
||||
import com.tangem.domain.wallet.Blockchain;
|
||||
import com.tangem.domain.wallet.TangemCard;
|
||||
import com.tangem.util.Util;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStream;
|
||||
|
|
@ -28,12 +39,62 @@ import io.reactivex.Observable;
|
|||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.observers.DefaultObserver;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.converter.gson.GsonConverterFactory;
|
||||
|
||||
public class ServerApiHelper {
|
||||
private static String TAG = ServerApiHelper.class.getSimpleName();
|
||||
|
||||
/**
|
||||
* HTTP
|
||||
* Card verify
|
||||
*/
|
||||
private CardVerifyListener cardVerifyListener;
|
||||
|
||||
public interface CardVerifyListener {
|
||||
void onCardVerify(CardVerifyResponse cardVerifyResponse);
|
||||
}
|
||||
|
||||
public void setCardVerify(CardVerifyListener listener) {
|
||||
cardVerifyListener = listener;
|
||||
}
|
||||
|
||||
public void cardVerify(TangemCard card) {
|
||||
Retrofit retrofit = new Retrofit.Builder()
|
||||
.baseUrl(Server.ApiTangem.URL_TANGEM)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build();
|
||||
|
||||
TangemApi tangemApi = retrofit.create(TangemApi.class);
|
||||
|
||||
CardVerify[] requests = new CardVerify[1];
|
||||
requests[0] = new CardVerify(Util.bytesToHex(card.getCID()), Util.bytesToHex(card.getCardPublicKey()));
|
||||
|
||||
CardVerifyBody cardVerifyBody = new CardVerifyBody(requests);
|
||||
|
||||
Call<CardVerifyResponse> call = tangemApi.getCardVerify("application/json", cardVerifyBody);
|
||||
call.enqueue(new Callback<CardVerifyResponse>() {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<CardVerifyResponse> call, @NonNull Response<CardVerifyResponse> response) {
|
||||
if (response.code() == 200) {
|
||||
CardVerifyResponse cardVerifyResponse = response.body();
|
||||
cardVerifyListener.onCardVerify(cardVerifyResponse);
|
||||
Log.i(TAG, "cardVerify onResponse " + response.code());
|
||||
} else {
|
||||
Log.e(TAG, "cardVerify onResponse " + response.code());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<CardVerifyResponse> call, Throwable t) {
|
||||
Log.e(TAG, "cardVerify onFailure " + t.getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* HTTP
|
||||
* Used in Crypto-currency course
|
||||
|
|
@ -41,7 +102,7 @@ public class ServerApiHelper {
|
|||
private RateInfoDataListener rateInfoDataListener;
|
||||
|
||||
public interface RateInfoDataListener {
|
||||
void onRateInfoDataData(RateInfoModel rateInfoModel);
|
||||
void onRateInfoDataData(RateInfoResponse rateInfoResponse);
|
||||
}
|
||||
|
||||
public void setRateInfoData(RateInfoDataListener listener) {
|
||||
|
|
@ -63,7 +124,7 @@ public class ServerApiHelper {
|
|||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(rateInfoModelList -> {
|
||||
if (!rateInfoModelList.isEmpty()) {
|
||||
for (RateInfoModel rateInfoMode : rateInfoModelList) {
|
||||
for (RateInfoResponse rateInfoMode : rateInfoModelList) {
|
||||
if (rateInfoMode.getId().equals(cryptoId)) {
|
||||
rateInfoDataListener.onRateInfoDataData(rateInfoMode);
|
||||
Log.i("rateInfoData", rateInfoMode.getId());
|
||||
|
|
|
|||
|
|
@ -1,13 +1,6 @@
|
|||
|
||||
package com.tangem.data.network;
|
||||
|
||||
public class ServerURL {
|
||||
public static final String API_TANGEM = "https://verify.tangem.com/";
|
||||
public static final String API_COINMARKET = "https://api.coinmarketcap.com/";
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
14
app/src/main/java/com/tangem/data/network/TangemApi.java
Normal file
14
app/src/main/java/com/tangem/data/network/TangemApi.java
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
package com.tangem.data.network;
|
||||
|
||||
import com.tangem.data.network.model.CardVerifyBody;
|
||||
import com.tangem.data.network.model.CardVerifyResponse;
|
||||
|
||||
import retrofit2.Call;
|
||||
import retrofit2.http.Body;
|
||||
import retrofit2.http.Header;
|
||||
import retrofit2.http.POST;
|
||||
|
||||
public interface TangemApi {
|
||||
@POST(Server.ApiTangem.Method.VERIFY)
|
||||
Call<CardVerifyResponse> getCardVerify(@Header("Content-Type") String contentType, @Body CardVerifyBody body);
|
||||
}
|
||||
|
|
@ -1,108 +0,0 @@
|
|||
package com.tangem.data.network
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.support.v7.app.AlertDialog
|
||||
import com.android.volley.AuthFailureError
|
||||
import com.android.volley.DefaultRetryPolicy
|
||||
import com.android.volley.Request
|
||||
import com.android.volley.Response
|
||||
import com.android.volley.toolbox.StringRequest
|
||||
import com.google.gson.Gson
|
||||
import com.tangem.AppController
|
||||
import com.tangem.data.network.model.CardVerifyModel
|
||||
import com.tangem.domain.wallet.TangemCard
|
||||
import com.tangem.util.Util
|
||||
import com.tangem.wallet.R
|
||||
import java.util.*
|
||||
|
||||
class VolleyHelper (private val requestCardVerify: IRequestCardVerify){
|
||||
|
||||
interface IRequestCardVerify {
|
||||
|
||||
fun success(cardVerifyModel: CardVerifyModel)
|
||||
|
||||
fun error(error: String)
|
||||
}
|
||||
|
||||
fun requestCardVerify(card: TangemCard) {
|
||||
val params = HashMap<String, String>()
|
||||
params["CID"] = Util.bytesToHex(card.cid)
|
||||
params["publicKey"] = Util.bytesToHex(card.cardPublicKey)
|
||||
doRequestString(Server.ApiTangem.Method.VERIFY, params)
|
||||
}
|
||||
|
||||
fun requestCardVerifyShowResponse(context: Activity, card: TangemCard) {
|
||||
val params = HashMap<String, String>()
|
||||
params["CID"] = Util.bytesToHex(card.cid)
|
||||
params["publicKey"] = Util.bytesToHex(card.cardPublicKey)
|
||||
doRequestDebug(context, Server.ApiTangem.Method.VERIFY, params)
|
||||
}
|
||||
|
||||
fun doRequestDebug(context: Context, url: String, params: Map<String, String>) {
|
||||
val stringRequest = object : StringRequest(Request.Method.GET, url,
|
||||
{ response ->
|
||||
// Log.i(LoadedWallet.TAG, response.toString())
|
||||
val builder = AlertDialog.Builder(context)
|
||||
builder.setTitle(url)
|
||||
.setMessage(response.toString())
|
||||
.setPositiveButton(R.string.ok, null)
|
||||
.setNeutralButton(R.string.send) { _, _ ->
|
||||
val intent = Intent(Intent.ACTION_SEND)
|
||||
intent.type = "text/html"
|
||||
intent.putExtra(Intent.EXTRA_EMAIL, arrayOf("erogov@tangem.com"))
|
||||
intent.putExtra(Intent.EXTRA_SUBJECT, url)
|
||||
intent.putExtra(Intent.EXTRA_TEXT, response.toString())
|
||||
context.startActivity(Intent.createChooser(intent, "Send json"))
|
||||
}
|
||||
val alert = builder.create()
|
||||
alert.show()
|
||||
},
|
||||
{ error -> val networkResponse = error.networkResponse }) {
|
||||
|
||||
|
||||
@Throws(AuthFailureError::class)
|
||||
override fun getHeaders(): Map<String, String> {
|
||||
return Server.getHeader()
|
||||
}
|
||||
|
||||
@Throws(AuthFailureError::class)
|
||||
override fun getParams(): Map<String, String> {
|
||||
return params
|
||||
}
|
||||
}
|
||||
|
||||
stringRequest.retryPolicy = DefaultRetryPolicy(30000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)
|
||||
AppController.getInstance().addToRequestQueue(stringRequest)
|
||||
}
|
||||
|
||||
private fun doRequestString(url: String, params: Map<String, String>) {
|
||||
val stringRequest = object : StringRequest(Request.Method.GET, url,
|
||||
Response.Listener<String> { response ->
|
||||
val requestResponse = response.toString().substring(response.toString().lastIndexOf("Response:") + 10)
|
||||
|
||||
val responseVerify = Gson().fromJson(requestResponse, CardVerifyModel::class.java)
|
||||
|
||||
requestCardVerify.success(responseVerify)
|
||||
|
||||
// Log.i(LoadedWallet.TAG, responseVerify.results!![2].CID)
|
||||
},
|
||||
Response.ErrorListener { "That didn't work!" }) {
|
||||
|
||||
@Throws(AuthFailureError::class)
|
||||
override fun getHeaders(): Map<String, String> {
|
||||
return Server.getHeader()
|
||||
}
|
||||
|
||||
@Throws(AuthFailureError::class)
|
||||
override fun getParams(): Map<String, String> {
|
||||
return params
|
||||
}
|
||||
}
|
||||
|
||||
stringRequest.retryPolicy = DefaultRetryPolicy(30000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)
|
||||
AppController.getInstance().addToRequestQueue(stringRequest)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
package com.tangem.data.network.model;
|
||||
|
||||
public class CardVerify {
|
||||
private String CID;
|
||||
private String publicKey;
|
||||
|
||||
public CardVerify(String CID, String publicKey) {
|
||||
this.CID = CID;
|
||||
this.publicKey = publicKey;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
package com.tangem.data.network.model;
|
||||
|
||||
public class CardVerifyBody {
|
||||
private CardVerify[] requests;
|
||||
|
||||
public CardVerifyBody(CardVerify[] requests) {
|
||||
this.requests = requests;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -2,23 +2,17 @@ package com.tangem.data.network.model
|
|||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
data class CardVerifyModel(
|
||||
data class CardVerifyResponse(
|
||||
@SerializedName("results")
|
||||
var results: ArrayList<Verify>? = null,
|
||||
|
||||
@SerializedName("error")
|
||||
var error: String? = null
|
||||
var results: List<Verify>? = null
|
||||
) {
|
||||
|
||||
data class Verify(
|
||||
@SerializedName("error")
|
||||
var error: String = "",
|
||||
|
||||
@SerializedName("CID")
|
||||
var CID: String = "",
|
||||
|
||||
@SerializedName("passed")
|
||||
var passed: Boolean? = null
|
||||
var passed: Boolean = false
|
||||
)
|
||||
|
||||
}
|
||||
|
|
@ -5,7 +5,7 @@ import com.google.gson.annotations.SerializedName
|
|||
import kotlinx.android.parcel.Parcelize
|
||||
|
||||
@Parcelize
|
||||
data class RateInfoModel(
|
||||
data class RateInfoResponse(
|
||||
@SerializedName(ID)
|
||||
var id: String = "",
|
||||
|
||||
|
|
@ -54,7 +54,7 @@ data class RateInfoModel(
|
|||
) : Parcelable {
|
||||
|
||||
companion object {
|
||||
val TAG: String = RateInfoModel::class.java.simpleName
|
||||
val TAG: String = RateInfoResponse::class.java.simpleName
|
||||
|
||||
const val ID = "id"
|
||||
const val NAME = "name"
|
||||
|
|
@ -1,5 +1,7 @@
|
|||
package com.tangem.data.network.request;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.tangem.domain.wallet.TangemCard;
|
||||
|
|
@ -35,6 +37,7 @@ public class VerificationServerProtocol {
|
|||
|
||||
public void doPost(String hostURL) throws IOException {
|
||||
URL url = new URL(hostURL + "/" + command.getURL());
|
||||
Log.i("isOnlineVerified", hostURL + "/" + command.getURL());
|
||||
URLConnection con = url.openConnection();
|
||||
HttpURLConnection http = (HttpURLConnection) con;
|
||||
try {
|
||||
|
|
@ -171,7 +174,6 @@ public class VerificationServerProtocol {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
public static Date strToDate(String date) throws ParseException {
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
|
||||
return new Date(formatter.parse(date).getTime());
|
||||
|
|
@ -186,4 +188,4 @@ public class VerificationServerProtocol {
|
|||
return new GsonBuilder().create();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue