Skip to content
This repository was archived by the owner on Aug 23, 2024. It is now read-only.

Commit ca138d4

Browse files
author
Stan Wijckmans
committed
Merge branch '1-initial-code' into 'develop'
Resolve "Initial code" See merge request !1
2 parents ad709c8 + d084eb5 commit ca138d4

27 files changed

+1431
-70
lines changed

.gitignore

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,9 @@
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/workspace.xml
38-
.idea/tasks.xml
39-
.idea/gradle.xml
40-
.idea/libraries
41-
42-
# Keystore files
43-
*.jks
44-
45-
# External native build folder generated in Android Studio 2.2 and later
46-
.externalNativeBuild
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/workspace.xml
5+
/.idea/libraries
6+
.DS_Store
7+
/build
8+
/captures
9+
.externalNativeBuild

LICENSE

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
The MIT License (MIT)
2-
3-
Copyright (c) 2017 Stan Wijckmans
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.
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 Stan Wijckmans
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.

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# RestAndroid
2-
3-
A library to do REST calls from Android apps.
1+
# RestAndroid
2+
3+
A library to do REST calls from Android apps.

build.gradle

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
buildscript {
2+
repositories {
3+
jcenter()
4+
}
5+
dependencies {
6+
classpath 'com.android.tools.build:gradle:2.2.3'
7+
}
8+
}
9+
10+
allprojects {
11+
repositories {
12+
jcenter()
13+
}
14+
}
15+
16+
task clean(type: Delete) {
17+
delete rootProject.buildDir
18+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
zipStoreBase=GRADLE_USER_HOME
4+
zipStorePath=wrapper/dists
5+
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip

rest/build.gradle

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
apply plugin: 'com.android.library'
2+
apply plugin: 'maven-publish'
3+
4+
android {
5+
compileSdkVersion 25
6+
buildToolsVersion "25.0.2"
7+
8+
defaultConfig {
9+
minSdkVersion 16
10+
targetSdkVersion 25
11+
versionCode 1
12+
versionName "1.0.0.0"
13+
}
14+
15+
lintOptions {
16+
abortOnError false
17+
}
18+
}
19+
20+
dependencies {
21+
compile 'be.stannieman:commonservices:1.0.0.3'
22+
compile 'com.android.volley:volley:1.0.0'
23+
compile 'com.fasterxml.jackson.core:jackson-databind:2.8.6'
24+
}
25+
26+
task createJavaDoc(type: Javadoc) {
27+
failOnError false
28+
source = android.sourceSets.main.java.srcDirs
29+
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
30+
}
31+
32+
task createJavaDocJar(type: Jar, dependsOn: createJavaDoc) {
33+
classifier = 'javadoc'
34+
from createJavaDoc.destinationDir
35+
}
36+
37+
publishing {
38+
publications {
39+
aar(MavenPublication) {
40+
groupId 'be.stannieman'
41+
artifactId 'rest'
42+
version '1.0.0.0'
43+
artifact "$buildDir\\outputs\\aar\\signed-${project.getName()}-release.aar"
44+
artifact createJavaDocJar
45+
46+
//generate pom nodes for dependencies
47+
pom.withXml {
48+
def dependenciesNode = asNode().appendNode('dependencies')
49+
configurations.compile.allDependencies.each { dependency ->
50+
def dependencyNode = dependenciesNode.appendNode('dependency')
51+
dependencyNode.appendNode('groupId', dependency.group)
52+
dependencyNode.appendNode('artifactId', dependency.name)
53+
dependencyNode.appendNode('version', dependency.version)
54+
}
55+
}
56+
}
57+
}
58+
59+
repositories{
60+
maven {
61+
url "$buildDir\\repo"
62+
}
63+
}
64+
}

rest/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<manifest package="stannieman.rest" />
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package stannieman.rest;
2+
3+
import android.util.Base64;
4+
5+
import com.android.volley.Request;
6+
import com.android.volley.RequestQueue;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
12+
import stannieman.commonservices.models.*;
13+
import stannieman.rest.models.ErrorResponseDataBase;
14+
import stannieman.rest.models.RequestProperties;
15+
import stannieman.rest.models.RestResult;
16+
import stannieman.rest.models.SuccessResponseDataBase;
17+
18+
/**
19+
* A simple REST client that supports basic authentication.
20+
* Authentication is done by setting a basic authentication header in the request.
21+
*/
22+
public final class BasicAuthRestClient extends RestClientBase {
23+
private static final String AuthHeaderKey = "Authorization";
24+
private static final String BasicAuthHeaderValuePrefix = "Basic ";
25+
private final Map<String, String> authHeader;
26+
27+
BasicAuthRestClient(ObjectMapper objectMapper, RequestQueue requestQueue, Scheme schema, String host, int port, String apiBasePath, String endpointPath, long timeout, String userName, String password) {
28+
super(objectMapper, requestQueue, schema, host, port, apiBasePath, endpointPath, timeout);
29+
authHeader = getBasicAuthHeader(userName, password);
30+
}
31+
32+
@Override
33+
public <SuccessResponseDataType extends SuccessResponseDataBase, ErrorResponseDataType extends ErrorResponseDataBase> ServiceResult<RestResult<SuccessResponseDataType, ErrorResponseDataType>> get(RequestProperties<SuccessResponseDataType, ErrorResponseDataType> requestProperties) {
34+
return doRequestWithAuthHeader(Request.Method.GET, requestProperties);
35+
}
36+
37+
@Override
38+
public <SuccessResponseDataType extends SuccessResponseDataBase, ErrorResponseDataType extends ErrorResponseDataBase> ServiceResult<RestResult<SuccessResponseDataType, ErrorResponseDataType>> post(RequestProperties<SuccessResponseDataType, ErrorResponseDataType> requestProperties) {
39+
return doRequestWithAuthHeader(Request.Method.POST, requestProperties);
40+
}
41+
42+
@Override
43+
public <SuccessResponseDataType extends SuccessResponseDataBase, ErrorResponseDataType extends ErrorResponseDataBase> ServiceResult<RestResult<SuccessResponseDataType, ErrorResponseDataType>> put(RequestProperties<SuccessResponseDataType, ErrorResponseDataType> requestProperties) {
44+
return doRequestWithAuthHeader(Request.Method.PUT, requestProperties);
45+
}
46+
47+
@Override
48+
public <SuccessResponseDataType extends SuccessResponseDataBase, ErrorResponseDataType extends ErrorResponseDataBase> ServiceResult<RestResult<SuccessResponseDataType, ErrorResponseDataType>> patch(RequestProperties<SuccessResponseDataType, ErrorResponseDataType> requestProperties) {
49+
return doRequestWithAuthHeader(Request.Method.PATCH, requestProperties);
50+
}
51+
52+
@Override
53+
public <SuccessResponseDataType extends SuccessResponseDataBase, ErrorResponseDataType extends ErrorResponseDataBase> void getAsync(RequestProperties<SuccessResponseDataType, ErrorResponseDataType> requestProperties, IRequestResponseListener<SuccessResponseDataType, ErrorResponseDataType> requestResponseListener) {
54+
doRequestWithAuthHeaderAsync(Request.Method.GET, requestProperties, requestResponseListener);
55+
}
56+
57+
@Override
58+
public <SuccessResponseDataType extends SuccessResponseDataBase, ErrorResponseDataType extends ErrorResponseDataBase> void postAsync(RequestProperties<SuccessResponseDataType, ErrorResponseDataType> requestProperties, IRequestResponseListener<SuccessResponseDataType, ErrorResponseDataType> requestResponseListener) {
59+
doRequestWithAuthHeaderAsync(Request.Method.POST, requestProperties, requestResponseListener);
60+
}
61+
62+
@Override
63+
public <SuccessResponseDataType extends SuccessResponseDataBase, ErrorResponseDataType extends ErrorResponseDataBase> void putAsync(RequestProperties<SuccessResponseDataType, ErrorResponseDataType> requestProperties, IRequestResponseListener<SuccessResponseDataType, ErrorResponseDataType> requestResponseListener) {
64+
doRequestWithAuthHeaderAsync(Request.Method.PUT, requestProperties, requestResponseListener);
65+
}
66+
67+
@Override
68+
public <SuccessResponseDataType extends SuccessResponseDataBase, ErrorResponseDataType extends ErrorResponseDataBase> void patchAsync(RequestProperties<SuccessResponseDataType, ErrorResponseDataType> requestProperties, IRequestResponseListener<SuccessResponseDataType, ErrorResponseDataType> requestResponseListener) {
69+
doRequestWithAuthHeaderAsync(Request.Method.PATCH, requestProperties, requestResponseListener);
70+
}
71+
72+
private <SuccessResponseDataType extends SuccessResponseDataBase, ErrorResponseDataType extends ErrorResponseDataBase> ServiceResult<RestResult<SuccessResponseDataType, ErrorResponseDataType>> doRequestWithAuthHeader(int method, RequestProperties<SuccessResponseDataType, ErrorResponseDataType> requestProperties) {
73+
return doRequest(method, requestProperties, requestProperties.getQueryParameters(), getHeadersWithAuthHeader(requestProperties.getHeaders()));
74+
}
75+
76+
private <SuccessResponseDataType extends SuccessResponseDataBase, ErrorResponseDataType extends ErrorResponseDataBase> void doRequestWithAuthHeaderAsync(int method, RequestProperties<SuccessResponseDataType, ErrorResponseDataType> requestProperties, IRequestResponseListener<SuccessResponseDataType, ErrorResponseDataType> requestResponseListener) {
77+
new DoRequestWithAuthHeaderAsyncTask<>(method, requestProperties, requestResponseListener).execute();
78+
}
79+
80+
private class DoRequestWithAuthHeaderAsyncTask<SuccessResponseDataType extends SuccessResponseDataBase, ErrorResponseDataType extends ErrorResponseDataBase> extends DoRequestAsyncTaskBase<SuccessResponseDataType, ErrorResponseDataType> {
81+
DoRequestWithAuthHeaderAsyncTask(int method, RequestProperties<SuccessResponseDataType, ErrorResponseDataType> requestProperties, IRequestResponseListener<SuccessResponseDataType, ErrorResponseDataType> requestResponseListener) {
82+
super(method, requestProperties, requestResponseListener);
83+
}
84+
85+
@Override
86+
protected ServiceResult<RestResult<SuccessResponseDataType, ErrorResponseDataType>> doInBackground(Void... voids) {
87+
return doRequestWithAuthHeader(method, requestProperties);
88+
}
89+
}
90+
91+
private Map<String, String> getBasicAuthHeader(String userName, String password) {
92+
Map<String, String> basicAuthHeader = new HashMap<>();
93+
basicAuthHeader.put(AuthHeaderKey, BasicAuthHeaderValuePrefix + Base64.encodeToString(String.format("%s:%s", userName, password).getBytes(), Base64.NO_WRAP));
94+
return basicAuthHeader;
95+
}
96+
97+
private Map<String, String> getHeadersWithAuthHeader(Map<String, String> headers) {
98+
Map<String, String> headersWithAuthHeader = new HashMap<>(authHeader);
99+
100+
if (headers != null) {
101+
headersWithAuthHeader.putAll(headers);
102+
}
103+
104+
return headersWithAuthHeader;
105+
}
106+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package stannieman.rest;
2+
3+
import android.content.Context;
4+
5+
import com.android.volley.RequestQueue;
6+
import com.android.volley.toolbox.Volley;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
9+
/**
10+
* Factory that produces BasicAuthRestClients.
11+
*/
12+
public final class BasicAuthRestClientFactory implements IRestClientFactory {
13+
private final ObjectMapper objectMapper;
14+
private final RequestQueue requestQueue;
15+
private final Scheme scheme;
16+
private final String host;
17+
private final int port;
18+
private final String apiBasePath;
19+
private final long timeout;
20+
21+
private final String userName;
22+
private final String password;
23+
24+
/**
25+
* Constructor to create a factory with an existing object mapper and request queue.
26+
* It is recommended to have only one object mapper and request queue in your application,
27+
* so if you have more than one REST client factory you should to create them yourself
28+
* and use this constructor to use the existing instances.
29+
* @param objectMapper object mapper to map objects from and to JSON
30+
* @param requestQueue Volley RequestQueue for performing requests
31+
* @param scheme URI schema
32+
* @param host REST server host
33+
* @param port REST server port
34+
* @param apiBasePath base path of the REST api
35+
* @param timeout timeout for the requests
36+
* @param userName user name for basic authentication
37+
* @param password password for basic authentication
38+
*/
39+
public BasicAuthRestClientFactory(ObjectMapper objectMapper, RequestQueue requestQueue, Scheme scheme, String host, int port, String apiBasePath, long timeout, String userName, String password) {
40+
this.objectMapper = objectMapper;
41+
this.requestQueue = requestQueue;
42+
this.scheme = scheme;
43+
this.host = host;
44+
this.port = port;
45+
this.apiBasePath = apiBasePath;
46+
this.timeout = timeout;
47+
48+
this.userName = userName;
49+
this.password = password;
50+
}
51+
52+
/**
53+
* Constructor to create a factory without an existing object mapper and request queue.
54+
* A new object mapper and request queue will be created for performing the REST calls.
55+
* @param scheme URI schema
56+
* @param host REST server host
57+
* @param port REST server port
58+
* @param apiBasePath base path of the REST api
59+
* @param timeout timeout for the requests
60+
* @param userName user name for basic authentication
61+
* @param password password for basic authentication
62+
*/
63+
public BasicAuthRestClientFactory(Context context, Scheme scheme, String host, int port, String apiBasePath, long timeout, String userName, String password) {
64+
this(new ObjectMapper(), Volley.newRequestQueue(context), scheme, host, port, apiBasePath, timeout, userName, password);
65+
}
66+
67+
@Override
68+
public IRestClient getRestClient(String endpointPath) {
69+
return new BasicAuthRestClient(objectMapper, requestQueue, scheme, host, port, apiBasePath, endpointPath, timeout, userName, password);
70+
}
71+
}

0 commit comments

Comments
 (0)