Skip to content

Commit 5eaaab0

Browse files
committed
Add SDK project
1 parent 0ebfbb3 commit 5eaaab0

33 files changed

+755
-0
lines changed

StudyplusAndroidSDK/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

StudyplusAndroidSDK/build.gradle

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
apply plugin: 'android-library'
2+
3+
android {
4+
compileSdkVersion 19
5+
buildToolsVersion "19.0.1"
6+
7+
defaultConfig {
8+
minSdkVersion 8
9+
targetSdkVersion 16
10+
versionCode 1
11+
versionName "1.0"
12+
}
13+
}
14+
15+
dependencies {
16+
compile 'com.android.support:appcompat-v7:19.0.1'
17+
compile 'com.google.guava:guava:16.0'
18+
}
19+
20+
apply plugin: 'maven'
21+
22+
uploadArchives {
23+
repositories {
24+
mavenDeployer {
25+
repository(url: "file://" + new File(rootDir, "repository"))
26+
pom.version = "0.1.0-SNAPSHOT"
27+
pom.groupId = 'jp.studyplus.android.sdk'
28+
pom.artifactId = "studyplus-android-sdk"
29+
}
30+
}
31+
}
32+
33+
android.libraryVariants.all { variant ->
34+
def name = variant.name
35+
task "javadoc-$name"(type: Javadoc) {
36+
description = "Generates javadoc for build $name"
37+
destinationDir = new File(destinationDir, variant.baseName)
38+
source = files(variant.javaCompile.source)
39+
classpath = files(android.plugin.runtimeJarList, variant.javaCompile.classpath)
40+
exclude '**/R.java'
41+
}
42+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="jp.studyplus.android.sdk">
3+
4+
<application android:allowBackup="true"
5+
android:label="@string/app_name"
6+
android:icon="@drawable/ic_launcher">
7+
8+
</application>
9+
10+
</manifest>
46 KB
Loading
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package jp.studyplus.android.sdk.internal.api;
2+
3+
import com.google.common.base.Optional;
4+
5+
import org.apache.http.HttpEntity;
6+
import org.apache.http.HttpResponse;
7+
import org.apache.http.client.ResponseHandler;
8+
import org.apache.http.client.methods.HttpGet;
9+
import org.apache.http.client.methods.HttpPost;
10+
import org.apache.http.client.methods.HttpUriRequest;
11+
import org.apache.http.entity.StringEntity;
12+
import org.apache.http.impl.client.DefaultHttpClient;
13+
import org.apache.http.params.BasicHttpParams;
14+
import org.apache.http.params.HttpConnectionParams;
15+
import org.apache.http.params.HttpParams;
16+
import org.apache.http.util.EntityUtils;
17+
import org.json.JSONObject;
18+
19+
import java.io.IOException;
20+
21+
public class ApiAccess {
22+
23+
private final String url;
24+
private final String accessToken;
25+
26+
public ApiAccess(String url, String accessToken) {
27+
this.url = url;
28+
this.accessToken = accessToken;
29+
}
30+
31+
public ApiResponse get() throws IOException {
32+
HttpGet get = new HttpGet(url);
33+
return request(get);
34+
}
35+
public ApiResponse post() throws IOException {
36+
HttpPost post = new HttpPost(url);
37+
return request(post);
38+
}
39+
public ApiResponse post(JSONObject parameter) throws IOException {
40+
HttpPost post = new HttpPost(url);
41+
post.setEntity(new StringEntity(parameter.toString(), "UTF-8"));
42+
return request(post);
43+
}
44+
45+
private ApiResponse request(HttpUriRequest request) throws IOException {
46+
HttpParams params = new BasicHttpParams();
47+
HttpConnectionParams.setConnectionTimeout(params, 10 * 1000);
48+
HttpConnectionParams.setSoTimeout(params, 10 * 1000);
49+
50+
DefaultHttpClient client = new DefaultHttpClient(params);
51+
final ApiResponse response;
52+
try {
53+
request.setHeader("Content-type", "application/json");
54+
request.setHeader("HTTP_AUTHORIZATION", "OAuth " + accessToken);
55+
response = client.execute(request, new OnLoadResponse());
56+
} finally {
57+
client.getConnectionManager().shutdown();
58+
}
59+
return response;
60+
}
61+
62+
private static class OnLoadResponse implements ResponseHandler<ApiResponse> {
63+
@Override
64+
public ApiResponse handleResponse(HttpResponse httpResponse) throws IOException {
65+
return ApiHttpResponse.create(httpResponse);
66+
}
67+
}
68+
69+
private static class ApiHttpResponse implements ApiResponse {
70+
71+
private final Optional<String> content;
72+
private final int statusCode;
73+
74+
public static ApiHttpResponse create(HttpResponse response) throws IOException {
75+
HttpEntity entity = response.getEntity();
76+
Optional<String> content = (entity == null) ?
77+
Optional.<String>absent() :
78+
Optional.of(EntityUtils.toString(entity, "UTF-8"));
79+
80+
return new ApiHttpResponse(
81+
content,
82+
response.getStatusLine().getStatusCode());
83+
}
84+
85+
private ApiHttpResponse(Optional<String> content, int statusCode) {
86+
this.content = content;
87+
this.statusCode = statusCode;
88+
}
89+
90+
@Override
91+
public Optional<String> getContent() {
92+
return content;
93+
}
94+
95+
@Override
96+
public int getStatusCode() {
97+
return statusCode;
98+
}
99+
}
100+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package jp.studyplus.android.sdk.internal.api;
2+
3+
import com.google.common.base.Optional;
4+
5+
public interface ApiResponse {
6+
7+
public Optional<String> getContent();
8+
9+
public int getStatusCode();
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package jp.studyplus.android.sdk.service.api;
2+
3+
/**
4+
* Represents parameters to use API by authenticated credentials;
5+
*/
6+
public interface ApiCertification {
7+
8+
public String getAccessToken();
9+
10+
public String getBaseUrl();
11+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package jp.studyplus.android.sdk.service.api;
2+
3+
/**
4+
* Represents a user of this API.
5+
*/
6+
public class ApiClient {
7+
private final ApiCertification certification;
8+
9+
public ApiClient(ApiCertification certification) {
10+
this.certification = certification;
11+
}
12+
13+
public void send(ApiRequest request){
14+
new ApiRequestTask(request, certification).execute();
15+
}
16+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package jp.studyplus.android.sdk.service.api;
2+
3+
import com.google.common.base.Optional;
4+
5+
import jp.studyplus.android.sdk.internal.api.ApiResponse;
6+
7+
8+
class ApiEither {
9+
private final Optional<Exception> exception;
10+
private final Optional<ApiResponse> response;
11+
12+
static ApiEither create(Exception exception){
13+
return new ApiEither(
14+
Optional.of(exception),
15+
Optional.<ApiResponse>absent());
16+
}
17+
18+
static ApiEither create(ApiResponse response){
19+
return new ApiEither(
20+
Optional.<Exception>absent(),
21+
Optional.of(response));
22+
}
23+
24+
private ApiEither(Optional<Exception> exception, Optional<ApiResponse> response) {
25+
this.exception = exception;
26+
this.response = response;
27+
}
28+
29+
Optional<Exception> getLeft(){
30+
return exception;
31+
}
32+
Optional<ApiResponse> getRight(){
33+
return response;
34+
}
35+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package jp.studyplus.android.sdk.service.api;
2+
3+
public enum ApiMethod {
4+
GET,
5+
POST,
6+
}

0 commit comments

Comments
 (0)