├── demo ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── colors.xml │ │ │ │ ├── dimens.xml │ │ │ │ └── styles.xml │ │ │ ├── mipmap-hdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-mdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── values-w820dp │ │ │ │ └── dimens.xml │ │ │ └── layout │ │ │ │ └── activity_main.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── bmutinda │ │ │ │ └── httpbuster │ │ │ │ └── demo │ │ │ │ ├── HttpBusterApplication.java │ │ │ │ └── MainActivity.java │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── bmutinda │ │ │ └── httpbuster │ │ │ └── demo │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── bmutinda │ │ └── httpbuster │ │ └── demo │ │ └── ApplicationTest.java ├── proguard-rules.pro └── build.gradle ├── library ├── .gitignore ├── src │ ├── main │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── com │ │ │ └── bmutinda │ │ │ └── httpbuster │ │ │ ├── ApiMethod.java │ │ │ ├── ConfigKey.java │ │ │ ├── ApiCallback.java │ │ │ ├── exceptions │ │ │ └── ConfigurationException.java │ │ │ ├── ApiRequestParam.java │ │ │ ├── ApiHeader.java │ │ │ ├── GetRequest.java │ │ │ ├── PostRequest.java │ │ │ ├── DeleteRequest.java │ │ │ ├── Configuration.java │ │ │ ├── BusterResponse.java │ │ │ ├── BusterRequest.java │ │ │ ├── files │ │ │ └── RequestFile.java │ │ │ ├── MultipartRequest.java │ │ │ ├── Api.java │ │ │ ├── HttpBuster.java │ │ │ └── BusterRequestExecutor.java │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── bmutinda │ │ │ └── httpbuster │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── bmutinda │ │ └── httpbuster │ │ └── ApplicationTest.java ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── gradle.properties ├── LICENSE ├── gradlew.bat ├── README.md └── gradlew /demo/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /library/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':library', ':demo' -------------------------------------------------------------------------------- /demo/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Demo 3 | 4 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bmutinda/HttpBuster/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /demo/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bmutinda/HttpBuster/HEAD/demo/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /demo/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bmutinda/HttpBuster/HEAD/demo/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /demo/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bmutinda/HttpBuster/HEAD/demo/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /demo/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bmutinda/HttpBuster/HEAD/demo/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /demo/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bmutinda/HttpBuster/HEAD/demo/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /library/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | -------------------------------------------------------------------------------- /library/src/main/java/com/bmutinda/httpbuster/ApiMethod.java: -------------------------------------------------------------------------------- 1 | package com.bmutinda.httpbuster; 2 | 3 | public enum ApiMethod { 4 | GET, 5 | POST, 6 | DELETE, 7 | MULTIPART 8 | } 9 | -------------------------------------------------------------------------------- /library/src/main/java/com/bmutinda/httpbuster/ConfigKey.java: -------------------------------------------------------------------------------- 1 | package com.bmutinda.httpbuster; 2 | 3 | public enum ConfigKey { 4 | CONNECTION_TIMEOUT, 5 | READ_TIMEOUT, 6 | WRITE_TIMEOUT 7 | } -------------------------------------------------------------------------------- /demo/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /demo/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /library/src/main/java/com/bmutinda/httpbuster/ApiCallback.java: -------------------------------------------------------------------------------- 1 | package com.bmutinda.httpbuster; 2 | 3 | import org.json.JSONObject; 4 | 5 | public interface ApiCallback { 6 | void done(BusterResponse busterResponse, JSONObject jsonObject, Exception exception); 7 | } -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Thu Jun 02 12:57:44 EAT 2016 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip 7 | -------------------------------------------------------------------------------- /library/src/main/java/com/bmutinda/httpbuster/exceptions/ConfigurationException.java: -------------------------------------------------------------------------------- 1 | 2 | package com.bmutinda.httpbuster.exceptions; 3 | 4 | public class ConfigurationException extends Exception { 5 | public ConfigurationException(){ 6 | super(); 7 | } 8 | public ConfigurationException(String message){ 9 | super(message); 10 | } 11 | } -------------------------------------------------------------------------------- /demo/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /library/src/test/java/com/bmutinda/httpbuster/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.bmutinda.httpbuster; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * To work on unit tests, switch the Test Artifact in the Build Variants view. 9 | */ 10 | public class ExampleUnitTest { 11 | @Test 12 | public void addition_isCorrect() throws Exception { 13 | assertEquals(4, 2 + 2); 14 | } 15 | } -------------------------------------------------------------------------------- /demo/src/test/java/com/bmutinda/httpbuster/demo/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.bmutinda.httpbuster.demo; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * To work on unit tests, switch the Test Artifact in the Build Variants view. 9 | */ 10 | public class ExampleUnitTest { 11 | @Test 12 | public void addition_isCorrect() throws Exception { 13 | assertEquals(4, 2 + 2); 14 | } 15 | } -------------------------------------------------------------------------------- /demo/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /library/src/androidTest/java/com/bmutinda/httpbuster/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.bmutinda.httpbuster; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /demo/src/androidTest/java/com/bmutinda/httpbuster/demo/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.bmutinda.httpbuster.demo; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /library/src/main/java/com/bmutinda/httpbuster/ApiRequestParam.java: -------------------------------------------------------------------------------- 1 | package com.bmutinda.httpbuster; 2 | 3 | public class ApiRequestParam { 4 | String key; 5 | String value; 6 | 7 | public ApiRequestParam( String key, String value){ 8 | this.key = key; 9 | this.value = value; 10 | } 11 | 12 | public String getKey(){ 13 | return this.key; 14 | } 15 | 16 | public String getValue( ){ 17 | return this.value; 18 | } 19 | } -------------------------------------------------------------------------------- /library/src/main/java/com/bmutinda/httpbuster/ApiHeader.java: -------------------------------------------------------------------------------- 1 | package com.bmutinda.httpbuster; 2 | 3 | public class ApiHeader { 4 | String key; 5 | String value; 6 | 7 | public ApiHeader(){ 8 | } 9 | 10 | public ApiHeader(String key, String value){ 11 | this.key = key; 12 | this.value = value; 13 | } 14 | 15 | public String getKey(){ 16 | return key; 17 | } 18 | public String getValue(){ 19 | return value; 20 | } 21 | } -------------------------------------------------------------------------------- /library/src/main/java/com/bmutinda/httpbuster/GetRequest.java: -------------------------------------------------------------------------------- 1 | package com.bmutinda.httpbuster; 2 | 3 | import java.util.HashMap; 4 | 5 | public class GetRequest extends BusterRequest { 6 | 7 | public GetRequest(){ 8 | super(); 9 | this.method = ApiMethod.GET; 10 | } 11 | 12 | public GetRequest(String url){ 13 | this(); 14 | this.url = url; 15 | } 16 | 17 | public GetRequest(String url, HashMap params){ 18 | this(); 19 | this.url = url; 20 | addParams(params); 21 | } 22 | } -------------------------------------------------------------------------------- /library/src/main/java/com/bmutinda/httpbuster/PostRequest.java: -------------------------------------------------------------------------------- 1 | package com.bmutinda.httpbuster; 2 | 3 | import java.util.HashMap; 4 | 5 | public class PostRequest extends BusterRequest { 6 | 7 | public PostRequest(){ 8 | super(); 9 | this.method = ApiMethod.POST; 10 | } 11 | 12 | public PostRequest(String url){ 13 | this(); 14 | this.url = url; 15 | } 16 | 17 | public PostRequest(String url, HashMap params){ 18 | this(); 19 | this.url = url; 20 | addParams( params ); 21 | } 22 | } -------------------------------------------------------------------------------- /library/src/main/java/com/bmutinda/httpbuster/DeleteRequest.java: -------------------------------------------------------------------------------- 1 | 2 | package com.bmutinda.httpbuster; 3 | 4 | import java.util.HashMap; 5 | 6 | public class DeleteRequest extends BusterRequest { 7 | 8 | public DeleteRequest(){ 9 | super(); 10 | this.method = ApiMethod.DELETE; 11 | } 12 | 13 | public DeleteRequest(String url){ 14 | this(); 15 | this.url = url; 16 | } 17 | 18 | public DeleteRequest(String url, HashMap params){ 19 | this(); 20 | this.url = url; 21 | addParams( params ); 22 | } 23 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the ART/Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | out/ 15 | 16 | # Gradle files 17 | .gradle/ 18 | build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio Navigation editor temp files 30 | .navigation/ 31 | 32 | # Android Studio captures folder 33 | captures/ 34 | 35 | # Intellij 36 | *.iml 37 | .idea/* 38 | .idea/workspace.xml 39 | 40 | # Keystore files 41 | *.jks 42 | .DS_Store 43 | -------------------------------------------------------------------------------- /demo/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/mutinda/Library/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /library/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/mutinda/Library/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /demo/src/main/java/com/bmutinda/httpbuster/demo/HttpBusterApplication.java: -------------------------------------------------------------------------------- 1 | package com.bmutinda.httpbuster.demo; 2 | 3 | import android.app.Application; 4 | 5 | import com.bmutinda.httpbuster.Api; 6 | import com.bmutinda.httpbuster.HttpBuster; 7 | 8 | public class HttpBusterApplication extends Application { 9 | 10 | static HttpBuster httpBuster; 11 | 12 | @Override 13 | public void onCreate(){ 14 | super.onCreate(); 15 | 16 | initializeApi(); 17 | } 18 | 19 | private void initializeApi(){ 20 | Api api = new Api(); 21 | api.setEndpoint("http://f87d9e68.ngrok.io/apps/httpbuster/api/v1/"); 22 | httpBuster = HttpBuster.withApi(api) 23 | .enableLogs(true) 24 | .build(); 25 | } 26 | 27 | public static HttpBuster getHttpBuster(){ 28 | return httpBuster; 29 | } 30 | 31 | } -------------------------------------------------------------------------------- /library/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'com.github.dcendents.android-maven' 3 | 4 | group='com.github.bmutinda' 5 | 6 | configurations { 7 | compile.exclude module: 'demo' 8 | } 9 | 10 | android { 11 | compileSdkVersion 23 12 | buildToolsVersion "23.0.2" 13 | 14 | defaultConfig { 15 | minSdkVersion 11 16 | targetSdkVersion 23 17 | versionCode 1 18 | versionName "1.0" 19 | } 20 | buildTypes { 21 | release { 22 | minifyEnabled false 23 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 24 | } 25 | } 26 | } 27 | 28 | dependencies { 29 | compile fileTree(dir: 'libs', include: ['*.jar']) 30 | testCompile 'junit:junit:4.12' 31 | compile 'com.android.support:appcompat-v7:23.1.1' 32 | 33 | compile 'com.squareup.okhttp:okhttp:2.6.0' 34 | } 35 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /demo/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | allprojects { 4 | repositories { 5 | maven { url 'https://jitpack.io' } 6 | } 7 | } 8 | 9 | android { 10 | compileSdkVersion 23 11 | buildToolsVersion "23.0.2" 12 | 13 | defaultConfig { 14 | applicationId "com.bmutinda.httpbuster.demo" 15 | minSdkVersion 11 16 | targetSdkVersion 23 17 | versionCode 1 18 | versionName "1.0" 19 | } 20 | buildTypes { 21 | release { 22 | minifyEnabled false 23 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 24 | } 25 | } 26 | } 27 | 28 | dependencies { 29 | compile fileTree(dir: 'libs', include: ['*.jar']) 30 | testCompile 'junit:junit:4.12' 31 | compile 'com.android.support:appcompat-v7:23.4.0' 32 | 33 | compile 'com.github.bmutinda:ask:1.0' 34 | 35 | compile project(":library") 36 | } 37 | -------------------------------------------------------------------------------- /demo/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 17 | 18 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Mutinda Boniface 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /demo/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /library/src/main/java/com/bmutinda/httpbuster/Configuration.java: -------------------------------------------------------------------------------- 1 | package com.bmutinda.httpbuster; 2 | 3 | public class Configuration { 4 | 5 | private int connectionTimeout = 30; 6 | private int writeTimeout = 30; 7 | private int readTimeout = 30; 8 | 9 | public Configuration(){ 10 | } 11 | 12 | public static Configuration create(){ 13 | return new Configuration(); 14 | } 15 | 16 | public Configuration add( ConfigKey key, int val ){ 17 | if (key.equals(ConfigKey.CONNECTION_TIMEOUT)){ 18 | this.connectionTimeout = val; 19 | } 20 | else if (key.equals(ConfigKey.WRITE_TIMEOUT)){ 21 | this.writeTimeout = val; 22 | } 23 | else if (key.equals(ConfigKey.READ_TIMEOUT)){ 24 | this.readTimeout = val; 25 | } 26 | return this; 27 | } 28 | 29 | public int getConnectionTimeout(){ return connectionTimeout; } 30 | public int getReadTimeout(){ return readTimeout; } 31 | public int getWriteTimeout(){ return writeTimeout; } 32 | 33 | 34 | @Override 35 | public String toString(){ 36 | return String.format( 37 | "ConnectionTimeout=%s\n" + 38 | "WriteTimeout=%s\n" + 39 | "ReadTimeout=%s", connectionTimeout, writeTimeout, readTimeout); 40 | } 41 | } -------------------------------------------------------------------------------- /library/src/main/java/com/bmutinda/httpbuster/BusterResponse.java: -------------------------------------------------------------------------------- 1 | package com.bmutinda.httpbuster; 2 | 3 | import com.squareup.okhttp.Response; 4 | import com.squareup.okhttp.ResponseBody; 5 | 6 | public class BusterResponse { 7 | Response response; 8 | String string; 9 | ResponseBody body; 10 | 11 | public BusterResponse( Response response ){ 12 | this.response = response; 13 | readBody(); 14 | readString(); 15 | } 16 | 17 | private void readBody(){ 18 | if ( response == null ){ 19 | return; 20 | } 21 | 22 | this.body = response.body(); 23 | } 24 | 25 | private void readString(){ 26 | if ( body ==null ){ 27 | return; 28 | } 29 | 30 | try{ 31 | this.string = response.body().string(); 32 | }catch (Exception e){ 33 | HttpBuster.log("Okhttp string() is alread called... empty body initialized"); 34 | } 35 | } 36 | 37 | public Response getResponse(){ 38 | return response; 39 | } 40 | 41 | public ResponseBody getBody(){ 42 | return body; 43 | } 44 | 45 | public String getString(){ 46 | return this.string; 47 | } 48 | 49 | public static BusterResponse build( Response response ){ 50 | return new BusterResponse(response); 51 | } 52 | } -------------------------------------------------------------------------------- /library/src/main/java/com/bmutinda/httpbuster/BusterRequest.java: -------------------------------------------------------------------------------- 1 | package com.bmutinda.httpbuster; 2 | 3 | import java.util.HashMap; 4 | import java.util.List; 5 | 6 | public class BusterRequest { 7 | protected String url; 8 | protected ApiMethod method; 9 | protected HashMap params; 10 | 11 | public BusterRequest(){ 12 | params = new HashMap<>(); 13 | } 14 | 15 | protected BusterRequest setUrl( String url ){ 16 | this.url = url; 17 | return this; 18 | } 19 | 20 | protected BusterRequest addParams( HashMap params ){ 21 | if ( params !=null ){ 22 | this.params.putAll(params); 23 | } 24 | return this; 25 | } 26 | 27 | protected BusterRequest addParams( List apiRequestParams ){ 28 | for ( ApiRequestParam apiRequestParam: apiRequestParams){ 29 | addParam(apiRequestParam); 30 | } 31 | return this; 32 | } 33 | 34 | protected BusterRequest addParam( ApiRequestParam apiRequestParam ){ 35 | if ( apiRequestParam !=null ){ 36 | this.params.put(apiRequestParam.getKey(), apiRequestParam.getValue()); 37 | } 38 | return this; 39 | } 40 | 41 | public String getUrl(){ 42 | return this.url; 43 | } 44 | 45 | public ApiMethod getMethod(){ 46 | return this.method; 47 | } 48 | 49 | public HashMap getParams(){ 50 | return this.params; 51 | } 52 | } -------------------------------------------------------------------------------- /library/src/main/java/com/bmutinda/httpbuster/files/RequestFile.java: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * Copyright (c) 2016 Mutinda Boniface 4 | * 5 | * 6 | */ 7 | 8 | package com.bmutinda.httpbuster.files; 9 | 10 | import com.squareup.okhttp.MediaType; 11 | 12 | import java.io.File; 13 | 14 | public class RequestFile { 15 | String fileKey="file"; 16 | String filePath; 17 | MediaType mediaType; 18 | 19 | public RequestFile(){ 20 | } 21 | 22 | public RequestFile(String fileKey, String filePath, MediaType mediaType){ 23 | this.fileKey = fileKey; 24 | this.filePath = filePath; 25 | this.mediaType = mediaType; 26 | } 27 | 28 | public RequestFile setFileKey( String fileKey ){ 29 | this.fileKey = fileKey; 30 | return this; 31 | } 32 | 33 | public RequestFile setFilePath( String filePath ){ 34 | this.filePath = filePath; 35 | return this; 36 | } 37 | 38 | public RequestFile setMediaType( MediaType mediaType ){ 39 | this.mediaType = mediaType; 40 | return this; 41 | } 42 | 43 | public String getFileKey(){ 44 | return this.fileKey; 45 | } 46 | public String getFileName(){ 47 | if (getFilePath() ==null){ 48 | return ""; 49 | } 50 | 51 | File f = new File(getFilePath()); 52 | return f.getName(); 53 | } 54 | 55 | public String getFilePath(){ 56 | return this.filePath; 57 | } 58 | 59 | public MediaType getMediaType(){ 60 | return this.mediaType; 61 | } 62 | } -------------------------------------------------------------------------------- /library/src/main/java/com/bmutinda/httpbuster/MultipartRequest.java: -------------------------------------------------------------------------------- 1 | 2 | package com.bmutinda.httpbuster; 3 | 4 | import com.bmutinda.httpbuster.files.RequestFile; 5 | 6 | import java.util.ArrayList; 7 | import java.util.HashMap; 8 | import java.util.List; 9 | 10 | public class MultipartRequest extends BusterRequest { 11 | 12 | List requestFiles; 13 | 14 | public MultipartRequest(){ 15 | super(); 16 | this.method = ApiMethod.MULTIPART; 17 | requestFiles = new ArrayList<>(); 18 | } 19 | 20 | public MultipartRequest(String url){ 21 | this(); 22 | this.url = url; 23 | } 24 | 25 | public MultipartRequest(String url, HashMap params){ 26 | this(); 27 | this.url = url; 28 | addParams( params ); 29 | } 30 | 31 | public MultipartRequest(String url, HashMap params, List requestFiles){ 32 | this(); 33 | this.url = url; 34 | this.requestFiles = requestFiles; 35 | addParams( params ); 36 | } 37 | 38 | protected MultipartRequest addRequestFiles( List requestFiles ){ 39 | if ( requestFiles !=null ){ 40 | this.requestFiles.addAll(requestFiles); 41 | } 42 | return this; 43 | } 44 | 45 | protected MultipartRequest addFile( RequestFile requestFile ){ 46 | if (requestFile !=null ){ 47 | this.requestFiles.add(requestFile); 48 | } 49 | return this; 50 | } 51 | 52 | public List getRequestFiles(){ 53 | return this.requestFiles; 54 | } 55 | } -------------------------------------------------------------------------------- /library/src/main/java/com/bmutinda/httpbuster/Api.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Mutinda Boniface 3 | * 4 | * 5 | */ 6 | 7 | package com.bmutinda.httpbuster; 8 | 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | 12 | public class Api { 13 | List headers; 14 | List defaultParams; 15 | String endpoint = null; 16 | 17 | public Api(){ 18 | headers = new ArrayList<>(); 19 | defaultParams = new ArrayList<>(); 20 | } 21 | 22 | public Api setEndpoint( String url ){ 23 | this.endpoint = url; 24 | return this; 25 | } 26 | 27 | public Api addHeaders( List headers){ 28 | for (ApiHeader apiHeader: headers){ 29 | addHeader(apiHeader); 30 | } 31 | return this; 32 | } 33 | 34 | public Api addHeader( ApiHeader header ){ 35 | boolean alreadyAdded = false; 36 | if ( header !=null ){ 37 | for( ApiHeader apiHeader: this.headers){ 38 | if ( apiHeader.getKey().equals(header.getKey())){ 39 | // update its value 40 | apiHeader.value = header.getValue(); 41 | alreadyAdded = true; 42 | break; 43 | } 44 | } 45 | } 46 | if ( ! alreadyAdded){ 47 | this.headers.add(header); 48 | } 49 | return this; 50 | } 51 | 52 | public Api addDefaultParams( List apiRequestParams){ 53 | this.defaultParams.addAll(apiRequestParams); 54 | return this; 55 | } 56 | 57 | public Api addDefaultParam( ApiRequestParam param ){ 58 | this.defaultParams.add(param); 59 | return this; 60 | } 61 | 62 | public String getEndpoint(){ 63 | return this.endpoint; 64 | } 65 | 66 | public List getHeaders(){ 67 | return this.headers; 68 | } 69 | 70 | public List getDefaultParams(){ 71 | return this.defaultParams; 72 | } 73 | } -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HttpBuster 2 | 3 | A very simple way to do http requests in **Android** using **okHttp** 4 | *** 5 | 6 | ### How to use 7 | --- 8 | - Create an instance of **HttpBuster** with an api endpoint 9 | ```java 10 | Api api = new Api(); 11 | api.setEndpoint("https://example.com/api/"); 12 | HttpBuster httpBuster = HttpBuster.withApi(api).build(); 13 | ``` 14 | Thats it. Now you are ready to make http requests to your api endpoint. 15 | #### Make GET request 16 | a.) Without any request parameters 17 | ```java 18 | httpBuster.makeGetRequest("jokes/random", null, new ApiCallback() { 19 | @Override 20 | public void done(BusterResponse response, JSONObject jsonObject, Exception exception) { 21 | Log.e(TAG, "GET without params done"); 22 | } 23 | }); 24 | ``` 25 | 26 | b.) With request parameters 27 | ```java 28 | HashMap map = new HashMap<>(); 29 | map.put("firstName", "Mutinda"); 30 | map.put("lastName", "Boniface"); 31 | httpBuster.makeGetRequest("jokes/random", map, new ApiCallback() { 32 | @Override 33 | public void done(BusterResponse response, JSONObject jsonObject, Exception exception) { 34 | Log.e(TAG, "GET with params done"); 35 | } 36 | }); 37 | ``` 38 | 39 | #### NB: 40 | 1. **The same applies for `POST`, `PUT`, `DELETE` requests** 41 | 2. We recommend using a single **HttpBuster** instance for the entire application- You can do this by intializing your **HttpBuster** instance via the Application class 42 | 43 | #### Make FILE UPLOAD request 44 | ```java 45 | 46 | // Add files to be upload 47 | List files = new ArrayList<>(); 48 | String file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath()+"/no_picture.png"; 49 | files.add( new RequestFile("photo", file, MediaType.parse("image/PNG")) ); 50 | 51 | // add optional payload (this is optional) 52 | HashMap map = new HashMap<>(); 53 | map.put("name", "Mutinda Boniface"); 54 | 55 | httpBuster.makeMultipartRequest("photo-upload/", map, files, new ApiCallback() { 56 | @Override 57 | public void done(BusterResponse response, JSONObject jsonObject, Exception exception) { 58 | Log.e(TAG, "POST MULTIPART - Response =" +(response!=null? response.getString() :"Not reachable" )); 59 | } 60 | }); 61 | ``` 62 | 63 | Have a look at the demo app for a complete app using the Library [Demo app](https://github.com/bmutinda/HttpBuster/tree/master/demo/src/main/java/com/bmutinda/httpbuster/demo) 64 | 65 | --- 66 | #### Latest Version: [![Release](https://jitpack.io/v/bmutinda/httpbuster.svg)](https://jitpack.io/#bmutinda/HttpBuster) 67 | 68 | --- 69 | ### Installation 70 | --- 71 | 72 | In your app `build.gradle` under repositories, include `jitpack.io` like below 73 | ```java 74 | allprojects{ 75 | repositories { 76 | maven { url "https://jitpack.io" } 77 | } 78 | } 79 | ``` 80 | Then in your dependecies add this line replacing **{latest_version}** with the latest version under releases 81 | ```java 82 | compile 'com.github.bmutinda:httpbuster:{latest_version}' 83 | ``` 84 | Example: 85 | ```java 86 | compile 'com.github.bmutinda:httpbuster:1.0' 87 | ``` 88 | 89 | ## Wanna Support? 90 | If `HttpBuster` helped you save time for your project delivery, you can buy coffee for me. Cheers 91 | Buy Me A Coffee 92 | -------------------------------------------------------------------------------- /demo/src/main/java/com/bmutinda/httpbuster/demo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.bmutinda.httpbuster.demo; 2 | 3 | import android.Manifest; 4 | import android.os.Bundle; 5 | import android.os.Environment; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.util.Log; 8 | 9 | import com.bmutinda.httpbuster.ApiCallback; 10 | import com.bmutinda.httpbuster.BusterResponse; 11 | import com.bmutinda.httpbuster.files.RequestFile; 12 | import com.squareup.okhttp.MediaType; 13 | import com.vistrav.ask.Ask; 14 | import com.vistrav.ask.annotations.AskGrantedAll; 15 | 16 | import org.json.JSONObject; 17 | 18 | import java.util.ArrayList; 19 | import java.util.HashMap; 20 | import java.util.List; 21 | 22 | public class MainActivity extends AppCompatActivity { 23 | 24 | private static final String TAG = MainActivity.class.getSimpleName(); 25 | 26 | @Override 27 | protected void onCreate(Bundle savedInstanceState) { 28 | super.onCreate(savedInstanceState); 29 | setContentView(R.layout.activity_main); 30 | 31 | Ask.on(this) 32 | .forPermissions(Manifest.permission.READ_EXTERNAL_STORAGE 33 | , Manifest.permission.WRITE_EXTERNAL_STORAGE) 34 | .withRationales("READ", 35 | "READ") 36 | .go(); 37 | } 38 | 39 | @AskGrantedAll 40 | public void grantedAll(){ 41 | runGet(); 42 | runPost(); 43 | runPostUpload(); 44 | runPut(); 45 | runDelete(); 46 | } 47 | 48 | private void runGet(){ 49 | // Get Request without any params added 50 | HttpBusterApplication.getHttpBuster().makeGetRequest("post/1", null, new ApiCallback() { 51 | @Override 52 | public void done(BusterResponse response, JSONObject jsonObject, Exception exception) { 53 | log( "GET - Response NO-PARAMS =" +(response!=null? response.getString() :"Not reachable" )); 54 | } 55 | }); 56 | 57 | // Get Request with other parameters added 58 | HashMap map = new HashMap<>(); 59 | map.put("orderBy", "date"); 60 | HttpBusterApplication.getHttpBuster().makeGetRequest("posts/", map, new ApiCallback() { 61 | @Override 62 | public void done(BusterResponse response, JSONObject jsonObject, Exception exception) { 63 | log( "GET - Response NO-PARAMS =" +(response!=null? response.getString() :"Not reachable" )); 64 | 65 | } 66 | }); 67 | } 68 | 69 | private void runPost(){ 70 | 71 | HashMap map = new HashMap<>(); 72 | map.put("title", "new title is here"); 73 | HttpBusterApplication.getHttpBuster().makePostRequest("post/1/", map, new ApiCallback() { 74 | @Override 75 | public void done(BusterResponse response, JSONObject jsonObject, Exception exception) { 76 | log( "POST - Response =" +(response!=null? response.getString() :"Not reachable" )); 77 | } 78 | }); 79 | 80 | } 81 | 82 | private void runPostUpload(){ 83 | String file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath()+"/images.jpeg"; 84 | 85 | RequestFile requestFile = new RequestFile("photo", file, MediaType.parse("image/jpeg")); 86 | List files = new ArrayList<>(); 87 | files.add( requestFile ); 88 | 89 | HashMap map = new HashMap<>(); 90 | map.put("name", "Mutinda Boniface"); 91 | 92 | HttpBusterApplication.getHttpBuster().makeMultipartRequest("photo-upload/", map, files, new ApiCallback() { 93 | @Override 94 | public void done(BusterResponse response, JSONObject jsonObject, Exception exception) { 95 | 96 | if ( exception !=null ){ 97 | exception.printStackTrace(); 98 | log( "Excepti===" +exception.getLocalizedMessage()); 99 | } 100 | 101 | log( "POST MULTIPART - Response =" +(response!=null? response.getString() :"Not reachable" )); 102 | } 103 | }); 104 | 105 | } 106 | 107 | private void runPut(){ 108 | 109 | } 110 | 111 | private void runDelete(){ 112 | 113 | } 114 | 115 | 116 | private static void log( String message ){ 117 | Log.e(TAG, message); 118 | System.out.print(message); 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /library/src/main/java/com/bmutinda/httpbuster/HttpBuster.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Mutinda Boniface 3 | * 4 | * 5 | */ 6 | 7 | package com.bmutinda.httpbuster; 8 | 9 | import android.util.Log; 10 | 11 | import com.bmutinda.httpbuster.files.RequestFile; 12 | import com.squareup.okhttp.Cache; 13 | import com.squareup.okhttp.OkHttpClient; 14 | 15 | import java.io.File; 16 | import java.util.HashMap; 17 | import java.util.List; 18 | import java.util.concurrent.TimeUnit; 19 | 20 | public class HttpBuster { 21 | private OkHttpClient okHttpClient; 22 | private Api api; 23 | private Configuration configuration; 24 | public static boolean debug = false; 25 | 26 | private HttpBuster(Api api){ 27 | this.api = api; 28 | } 29 | 30 | public HttpBuster enableLogs( boolean enable ){ 31 | debug = enable; 32 | return this; 33 | } 34 | 35 | public HttpBuster withConfiguration(Configuration configuration ){ 36 | this.configuration = configuration; 37 | return this; 38 | } 39 | 40 | public HttpBuster build(){ 41 | if ( configuration == null ){ 42 | configuration = new Configuration(); 43 | } 44 | createHttpClient(); 45 | return this; 46 | } 47 | 48 | private OkHttpClient createHttpClient(){ 49 | if ( okHttpClient !=null ){ 50 | log("Oops! okHttp client seems to be already created."); 51 | return okHttpClient; 52 | } 53 | 54 | if ( configuration == null ){ 55 | log("Configuration seems to be empty. Pass an non-empty configuration object"); 56 | return null; 57 | } 58 | 59 | okHttpClient = new OkHttpClient(); 60 | okHttpClient.setConnectTimeout(configuration.getConnectionTimeout(), TimeUnit.SECONDS); 61 | okHttpClient.setWriteTimeout(configuration.getWriteTimeout(), TimeUnit.SECONDS); 62 | okHttpClient.setReadTimeout(configuration.getReadTimeout(), TimeUnit.SECONDS); 63 | 64 | return okHttpClient; 65 | } 66 | 67 | public boolean enableCache( File cacheDirectory, int cacheSize ){ 68 | if ( okHttpClient ==null || cacheDirectory == null ){ 69 | return false; 70 | } 71 | Cache cache = new Cache(cacheDirectory, cacheSize); 72 | okHttpClient.setCache(cache); 73 | return true; 74 | } 75 | 76 | public OkHttpClient getHttpClient(){ 77 | return okHttpClient; 78 | } 79 | public Api getApi(){ 80 | return api; 81 | } 82 | 83 | /** 84 | * Generate url with the endpoint prepended to it 85 | * 86 | * @param url - the url to append to the endpoint 87 | * @return 88 | */ 89 | public String generateUrl( String url ){ 90 | if ( api.getEndpoint() == null || url ==null ){ 91 | return url; 92 | } 93 | if ( url.contains( api.getEndpoint())){ 94 | return url; 95 | } 96 | return String.format("%s%s%s", 97 | api.getEndpoint(), api.getEndpoint().endsWith("/") ? "" : "/", url.startsWith("/") ? url.substring(1, url.length()) : url ); 98 | } 99 | 100 | // ===================== 101 | // REQUESTS stuff 102 | // ===================== 103 | 104 | public void makeGetRequest( String url, HashMap params, ApiCallback apiCallback ){ 105 | GetRequest getRequest = new GetRequest(url, params); 106 | BusterRequestExecutor.execute(this, getRequest, apiCallback); 107 | } 108 | 109 | public void makePostRequest( String url, HashMap params, ApiCallback apiCallback ){ 110 | PostRequest postRequest = new PostRequest(url, params); 111 | BusterRequestExecutor.execute(this, postRequest, apiCallback); 112 | } 113 | 114 | public void makeDeleteRequest( String url, HashMap params, ApiCallback apiCallback ){ 115 | DeleteRequest deleteRequest = new DeleteRequest(url, params); 116 | BusterRequestExecutor.execute(this, deleteRequest, apiCallback); 117 | } 118 | 119 | public void makeMultipartRequest( String url, HashMap params, List requestFiles, ApiCallback apiCallback ){ 120 | MultipartRequest multipartRequest = new MultipartRequest(url, params, requestFiles); 121 | BusterRequestExecutor.execute(this, multipartRequest, apiCallback); 122 | } 123 | 124 | public static HttpBuster withApi( Api api ){ 125 | return new HttpBuster(api); 126 | } 127 | 128 | public static void log( String message ){ 129 | if ( ! debug ){ 130 | return; 131 | } 132 | Log.e("HttpBuster", message); 133 | } 134 | } -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /library/src/main/java/com/bmutinda/httpbuster/BusterRequestExecutor.java: -------------------------------------------------------------------------------- 1 | package com.bmutinda.httpbuster; 2 | 3 | import com.bmutinda.httpbuster.files.RequestFile; 4 | import com.squareup.okhttp.Callback; 5 | import com.squareup.okhttp.FormEncodingBuilder; 6 | import com.squareup.okhttp.MultipartBuilder; 7 | import com.squareup.okhttp.Request; 8 | import com.squareup.okhttp.RequestBody; 9 | import com.squareup.okhttp.Response; 10 | 11 | import org.json.JSONException; 12 | import org.json.JSONObject; 13 | 14 | import java.io.File; 15 | import java.io.IOException; 16 | import java.util.List; 17 | import java.util.Map; 18 | 19 | public class BusterRequestExecutor { 20 | HttpBuster httpBuster; 21 | BusterRequest request; 22 | 23 | public BusterRequestExecutor(HttpBuster httpBuster, BusterRequest request){ 24 | this.httpBuster = httpBuster; 25 | this.request = request; 26 | } 27 | 28 | public void run( final ApiCallback apiCallback ){ 29 | if ( httpBuster == null || request == null ){ 30 | apiCallback.done(null, null, new Exception("HttpBuster or request is empty.")); 31 | return; 32 | } 33 | 34 | Request httpRequest = null; 35 | String finalUrl = httpBuster.generateUrl(request.getUrl()); 36 | if (request.getMethod().equals(ApiMethod.GET)){ 37 | StringBuilder sb = new StringBuilder(); 38 | if ( request.getParams()!=null ){ 39 | for (Map.Entry entry: request.getParams().entrySet() ){ 40 | sb.append( (String.valueOf(entry.getKey())+"="+String.valueOf(entry.getValue() ))+"&" ); 41 | } 42 | } 43 | finalUrl+="?"+sb.toString(); 44 | httpRequest = prepareRequest(finalUrl).build(); 45 | 46 | }else if ( request.getMethod().equals(ApiMethod.POST)){ 47 | FormEncodingBuilder formBuilder = new FormEncodingBuilder(); 48 | if ( request.getParams()!=null ){ 49 | for (Map.Entry entry : request.getParams().entrySet()) { 50 | formBuilder.add(entry.getKey(), String.valueOf(entry.getValue())); 51 | } 52 | } 53 | Request.Builder builder = prepareRequest(finalUrl); 54 | httpRequest = builder.post( formBuilder.build() ).build(); 55 | 56 | }else if ( request.getMethod().equals(ApiMethod.DELETE)){ 57 | FormEncodingBuilder formBuilder = new FormEncodingBuilder(); 58 | if ( request.getParams()!=null ){ 59 | for (Map.Entry entry : request.getParams().entrySet()) { 60 | formBuilder.add(entry.getKey(), String.valueOf(entry.getValue())); 61 | } 62 | } 63 | Request.Builder builder = prepareRequest(finalUrl); 64 | httpRequest = builder.delete( formBuilder.build() ).build(); 65 | 66 | }else if ( request.getMethod().equals(ApiMethod.MULTIPART)){ 67 | MultipartBuilder multipartBuilder = new MultipartBuilder(); 68 | multipartBuilder.type(MultipartBuilder.FORM); 69 | 70 | if ( request.getParams()!=null ){ 71 | for (Map.Entry entry : request.getParams().entrySet()) { 72 | multipartBuilder.addFormDataPart(entry.getKey(), String.valueOf(entry.getValue())); 73 | } 74 | } 75 | 76 | MultipartRequest req = (MultipartRequest) request; 77 | for (RequestFile requestFile: req.getRequestFiles()){ 78 | multipartBuilder.addFormDataPart(requestFile.getFileKey(), requestFile.getFileName(), RequestBody.create(requestFile.getMediaType(), new File(requestFile.getFilePath()))); 79 | } 80 | Request.Builder builder = prepareRequest(finalUrl); 81 | httpRequest = builder.post( multipartBuilder.build() ).build(); 82 | } 83 | 84 | if ( httpRequest == null ){ 85 | apiCallback.done(null, null, new Exception("Un-known request method supplied.") ); 86 | return; 87 | } 88 | 89 | HttpBuster.log("Making a request to "+finalUrl+", type = "+request.getMethod().toString()); 90 | 91 | httpBuster.getHttpClient().newCall( httpRequest ).enqueue(new Callback() { 92 | @Override 93 | public void onFailure(Request request, IOException e) { 94 | apiCallback.done( null, null, e ); 95 | } 96 | 97 | @Override 98 | public void onResponse(Response response) throws IOException { 99 | JSONObject json = new JSONObject(); 100 | Exception exception = null; 101 | BusterResponse busterResponse = BusterResponse.build(response); 102 | 103 | if( busterResponse.getString() !=null ){ 104 | try { 105 | json = new JSONObject( busterResponse.getString() ); 106 | } catch (JSONException e) { 107 | exception = e ; 108 | } 109 | } 110 | apiCallback.done(busterResponse, json, exception); 111 | } 112 | }); 113 | } 114 | 115 | private Request.Builder prepareRequest( String url ){ 116 | Request.Builder requestBuilder = new Request.Builder().url(url); 117 | List headers = httpBuster.getApi().getHeaders(); 118 | for( ApiHeader entity: headers ){ 119 | requestBuilder.addHeader( entity.getKey(), String.valueOf( entity.getValue()) ); 120 | } 121 | return requestBuilder; 122 | } 123 | 124 | public static void execute( HttpBuster httpBuster, BusterRequest request, ApiCallback apiCallback){ 125 | // Add default api params 126 | List defaultParams = httpBuster.getApi().getDefaultParams(); 127 | for ( ApiRequestParam apiRequestParam: defaultParams){ 128 | request.addParam(apiRequestParam); 129 | } 130 | new BusterRequestExecutor(httpBuster, request).run(apiCallback); 131 | } 132 | } --------------------------------------------------------------------------------