Skip to content

Commit

Permalink
Modified HttpRequests to work with URIs & added test project
Browse files Browse the repository at this point in the history
[Mod]: Modified AbstractSimpleHttpRequest to demain a uri
[Mod]: Made Yahoo request build using URIs.

[Add]: Added test case for YahooRequests; passes.
  • Loading branch information
iamovrhere committed May 22, 2015
1 parent c5d5824 commit 81a6cc0
Show file tree
Hide file tree
Showing 11 changed files with 287 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.util.List;
import java.util.Locale;

import android.net.Uri;

import com.ovrhere.android.currencyconverter.model.requests.AbstractSimpleHttpRequest;

/**
Expand All @@ -36,47 +38,62 @@
* Note that an instance of {@link YahooApiCurrencyRequest} will only run
* one request at a time and that requests are blocking</p>
* @author Jason J.
* @version 0.4.1-20140929
* @version 0.5.0-20150521
*/
public class YahooApiCurrencyRequest extends AbstractSimpleHttpRequest {
/** The logtag for debuggin. */
/** The logtag for debugging. */
@SuppressWarnings("unused")
final static private String LOGTAG = YahooApiCurrencyRequest.class
.getSimpleName();

/** The API url to base the query on.
/** The API base url. */
final static private String API_BASE = "http://query.yahooapis.com/v1/public/yql";

/** Format query String. */
final static private String QUERY_FORMAT = "format";
/** Env query String. */
final static private String QUERY_ENV = "env";
/** Prepared Select query String. */
final static private String QUERY_PREPARED_SELECT = "q";

/** The API query.
* Use {@link String#format(java.util.Locale, String, Object...)
* to replace this with the actual request form.
* Note the form is: <code>"USDEUR","USDJPY","USDGBP",...</code>
* where USD is the starting currency. */
final static private String PREPARED_API_URL =
"http://query.yahooapis.com/v1/public/yql?"
+ "q=select * from yahoo.finance.xchange where pair in (" +
"%s"+
")&env=store://datatables.org/alltableswithkeys";
/** Append to {@link #PREPARED_API_URL}/{@link #preparedRequest} to get
* json format. */
final static private String JSON_API_APPEND = "&format=json";
final static private String VALUE_PREPARED_SELECT =
"select * from yahoo.finance.xchange where pair in (%s)";

final static private String VALUE_ENV_TABLE = "store://datatables.org/alltableswithkeys";

/** The format type. */
final static private String VALUE_FORMAT_JSON = "json";

/////////////////////////////////////////////////////////////////////////////////////////////////
/// End constants
////////////////////////////////////////////////////////////////////////////////////////////////

/** The prepared request ready for execution. */
private String preparedRequest = "";

/** Whether not to use json format. */
private boolean jsonFormat = false;

/** The select query for the query {@link #QUERY_PREPARED_SELECT}. */
final private String selectQuery;


/** Creates and prepares a new request.
* @param sourceCodes The list of source currency codes in ISO 4217 form.
* @param destCodes The list of destination codes in ISO 4217 form.
* @param onRequestEventListener The listener to process the results and errors.
*/
public YahooApiCurrencyRequest(String[] sourceCode, String[] destCodes,
public YahooApiCurrencyRequest(String[] sourceCodes, String[] destCodes,
OnRequestEventListener onRequestEventListener) {
super();
prepareRequest(Arrays.asList(sourceCode), Arrays.asList(destCodes));
List<String> dstCodes = new ArrayList<String>();
dstCodes.addAll(Arrays.asList(destCodes));
List<String> srcCodes = new ArrayList<String>();
srcCodes.addAll(Arrays.asList(sourceCodes));

this.selectQuery = prepareQuery(srcCodes, dstCodes);
this.setOnRequestEventListener(onRequestEventListener);
}

Expand All @@ -85,12 +102,11 @@ public YahooApiCurrencyRequest(String[] sourceCode, String[] destCodes,
* @param destCodes The list of destination codes in ISO 4217 form.
* @param onRequestEventListener The listener to process the results and errors.
*/
public YahooApiCurrencyRequest
(List<String> sourceCodes, List<String> destCodes,
public YahooApiCurrencyRequest (List<String> sourceCodes, List<String> destCodes,
OnRequestEventListener onRequestEventListener) {
List<String> mDestCodes = new ArrayList<String>();
mDestCodes.addAll(destCodes);
prepareRequest(sourceCodes, mDestCodes);
List<String> dstCodes = new ArrayList<String>();
dstCodes.addAll(destCodes);
this.selectQuery = prepareQuery(sourceCodes, dstCodes);
this.setOnRequestEventListener(onRequestEventListener);
}

Expand All @@ -99,19 +115,19 @@ public YahooApiCurrencyRequest(String[] sourceCode, String[] destCodes,
*/
public void setJsonFormat(boolean jsonFormat) {
this.jsonFormat = jsonFormat;
if (jsonFormat){
if (!preparedRequest.contains(JSON_API_APPEND)){
//add json string
preparedRequest += JSON_API_APPEND;
}
} else { //remove json string
preparedRequest.replaceAll(JSON_API_APPEND, "");
}
}

@Override
protected String getPreparedRequest() {
return preparedRequest;
protected Uri getUriRequest() {
Uri.Builder builder = Uri.parse(API_BASE).buildUpon();
builder .appendQueryParameter(QUERY_PREPARED_SELECT, selectQuery)
.appendQueryParameter(QUERY_ENV, VALUE_ENV_TABLE);

if (jsonFormat) {
builder.appendQueryParameter(QUERY_FORMAT, VALUE_FORMAT_JSON);
}

return builder.build();
}

/////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -124,10 +140,11 @@ protected String getPreparedRequest() {
* @param destCodes The list of destination codes
* (note: will remove elements from this; cannot be same list as sourceCodes)
*/
private void prepareRequest(List<String> sourceCodes, List<String> destCodes){
private String prepareQuery(List<String> sourceCodes, List<String> destCodes){
String codeList = "";
for (String sCode : sourceCodes){
for (int index = 0; index < destCodes.size(); index++){
final int SIZE = destCodes.size();
for (int index = 0; index < SIZE; index++){
String dCode = destCodes.get(index);
if (sCode.equalsIgnoreCase(dCode)){
continue;
Expand All @@ -141,11 +158,6 @@ private void prepareRequest(List<String> sourceCodes, List<String> destCodes){
}
//insert records & replace spaces (as the api does not handle spaces well)
//We could also use URLEncoder.encode(String, Locale);
preparedRequest = String.format(Locale.US, PREPARED_API_URL, codeList)
.replaceAll(" ", "%20");

if (jsonFormat){
preparedRequest += JSON_API_APPEND;
}
return String.format(Locale.US, VALUE_PREPARED_SELECT, codeList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.net.SocketTimeoutException;
import java.net.URL;

import android.net.Uri;
import android.util.Log;

/** Outlines the basics of a simple GET http request. The request is set via
Expand All @@ -32,9 +33,9 @@
* <p>Do not forget to
* call {@link #setOnRequestEventListener(OnRequestEventListener)}.</p>
* @author Jason J.
* @version 0.2.0-20140920
* @version 0.3.0-20150521
*/
public abstract class AbstractSimpleHttpRequest implements Runnable{
public abstract class AbstractSimpleHttpRequest implements Runnable {
/** The logtag for debugging. */
final static private String LOGTAG = AbstractSimpleHttpRequest.class
.getSimpleName();
Expand Down Expand Up @@ -67,8 +68,7 @@ public abstract class AbstractSimpleHttpRequest implements Runnable{

/** Sets a request event listener.
* @param onRequestEventListener The implementer of this interface */
public void setOnRequestEventListener(
OnRequestEventListener onRequestEventListener) {
public void setOnRequestEventListener(OnRequestEventListener onRequestEventListener) {
this.mRequestEventListener = onRequestEventListener;
}
/** Sets how long in milliseconds before the request gives up. Will not
Expand Down Expand Up @@ -98,9 +98,10 @@ public void cancel(){
}
}


/** Returns the prepared request to perform in {@link #run()}
* @return A valid request to run. */
abstract protected String getPreparedRequest();
abstract protected Uri getUriRequest();

/////////////////////////////////////////////////////////////////////////////////////////////////
/// Overridden methods
Expand All @@ -109,14 +110,14 @@ public void cancel(){
@Override
public String toString() {
return super.toString()+
String.format("[request: %s]", getPreparedRequest());
String.format("[request: %s]", getUriRequest().toString());
}

@Override
public void run() {
synchronized (reqLock) {
final int QUERY_TIMEOUT = requestTimeout;
final String preparedRequest = getPreparedRequest();
final String preparedRequest = getUriRequest().toString();

int responseCode = 0;
if (Thread.interrupted()){
Expand Down
19 changes: 19 additions & 0 deletions tests/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ovrhere.android.currencyconverter.test"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="18" />

<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.ovrhere.android.currencyconverter" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<uses-library android:name="android.test.runner" />
</application>

</manifest>
20 changes: 20 additions & 0 deletions tests/proguard-project.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
Binary file added tests/res/drawable-hdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/res/drawable-ldpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/res/drawable-mdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/res/drawable-xhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions tests/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">CurrencyConverterTestTest</string>

</resources>
32 changes: 32 additions & 0 deletions tests/src/com/ovrhere/android/currencyconverter/FullTestSuite.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2014 The Android Open Source Project
*
* 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.ovrhere.android.currencyconverter;

import android.test.suitebuilder.TestSuiteBuilder;

import junit.framework.Test;
import junit.framework.TestSuite;

public class FullTestSuite extends TestSuite {
public static Test suite() {
return new TestSuiteBuilder(FullTestSuite.class)
.includeAllPackagesUnderHere().build();
}

public FullTestSuite() {
super();
}
}
Loading

0 comments on commit 81a6cc0

Please sign in to comment.