Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ See full example in [samples](samples/) - This can be imported into Android Stud

```java
// Set your credentials
Api tc = new Api("login", "password");
Api tc = new Api(new LoginPasswordAuth("login", "password"), null);

Hashtable<String, Object> param = new Hashtable<String, Object>();
param.put("flash_message", false);
Expand Down
Binary file removed jars/callr-sdk-java-1.4.0.jar
Binary file not shown.
Binary file added jars/callr-sdk-java-1.5.0.jar
Binary file not shown.
62 changes: 31 additions & 31 deletions samples/CallrSampleApp/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.callr.samples.callrsampleapp"
minSdkVersion 17
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
testCompile 'junit:junit:4.12'
compile files('libs/callr-sdk-java-1.4.0.jar')
compile 'com.google.code.gson:gson:2.8.0'
compile 'commons-codec:commons-codec:1.10'
}
apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.callr.samples.callrsampleapp"
minSdkVersion 17
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
testCompile 'junit:junit:4.12'
compile files('libs/callr-sdk-java-1.5.0.jar')
compile 'com.google.code.gson:gson:2.8.0'
compile 'commons-codec:commons-codec:1.10'
}
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.widget.EditText;

import com.callr.exceptions.CallrException;
import com.callr.auth.*;
import com.callr.Api;

public class MainActivity extends Activity {
Expand All @@ -26,7 +27,7 @@ protected void onCreate(Bundle savedInstanceState) {
public void sendSMS(View view){
String phoneNumber = ((EditText)findViewById(R.id.editPhonenumber)).getText().toString();
String message = ((EditText)findViewById(R.id.editMessage)).getText().toString();
Api api = new Api("login", "password");
Api api = new Api(new LoginPasswordAuth("login", "password"), null);
try {
api.call("sms.send","SMS", phoneNumber, message, null);
} catch (CallrException e) {
Expand Down
46 changes: 23 additions & 23 deletions samples/CallrSampleApp/build.gradle
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
jcenter()
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}
2 changes: 1 addition & 1 deletion samples/CallrSampleApp/settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include ':app'
include ':app'
65 changes: 47 additions & 18 deletions src/main/java/com/callr/Api.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.callr;

import com.callr.exceptions.*;
import com.callr.auth.*;

import com.google.gson.JsonParser;
import com.google.gson.JsonElement;
Expand All @@ -26,33 +27,44 @@
import javax.net.ssl.HttpsURLConnection;

public class Api {
private static final String SDK_VERSION = "1.4.0";
private static final String SDK_VERSION = "1.5.0";
private String _apiUrl = "https://api.callr.com/json-rpc/v1.1/";
private String _login = null;
private String _password = null;
private Hashtable<String, String> _config = null;
private LoginAs _logAs = null;
private CallrAuthentication _auth = null;

/**
* Constructor
* Default Constructor
* @param auth CallrAuthentication CALLR authentication object
* @param config extra configuration options
*/
public Api(CallrAuthentication auth, Hashtable<String, String> config){
this._auth = auth;
this._config = config;
}

/**
* Overloaded constructor
* @param login CALLR login
* @param password CALLR password
* @param config extra configuration options
* @deprecated
*/
@Deprecated
public Api(String login, String password, Hashtable<String, String> config) {
_login = login;
_password = password;
_config = config;
this(new LoginPasswordAuth(login, password), config);
}

// overload Api constructor for optional parameters
/**
* Constructor
* @param login
* @param password
* @deprecated
*/
@Deprecated
public Api(String login, String password) {
_login = login;
_password = password;
this(new LoginPasswordAuth(login, password), null);
}

/**
Expand All @@ -63,6 +75,21 @@ public void setApiUrl(String url){
this._apiUrl = url;
}

/**
* setLoginAs - set to null to clear
* @param logAs LoginAs
*/
public void setLoginAs(LoginAs logAs){
this._logAs = logAs;
}

/**
* setLoginAs overload for updating object
*/
public void setLoginAs(String type, String target) throws CallrClientException {
this._logAs = new LoginAs(type, target);
}

// Send a request to CALLR webservice
/**
* call
Expand All @@ -72,7 +99,7 @@ public void setApiUrl(String url){
@SuppressWarnings({ "rawtypes", "unchecked" })
public JsonElement call(String method, Object... params) throws CallrException, CallrClientException {
ArrayList array = new ArrayList();

for (Object el : params) {
array.add(el);
}
Expand Down Expand Up @@ -132,9 +159,6 @@ public PasswordAuthentication getPasswordAuthentication() {
proxy = Proxy.NO_PROXY;
}

// encode credentials to base64 Basic Auth format
tmp = new String(Base64.encodeBase64((this._login + ":" + this._password).getBytes()));

try {
postDataBytes = gson.toJson(createObject(method, params, id)).getBytes("UTF-8");
conn = (HttpsURLConnection) url.openConnection(proxy);
Expand All @@ -143,9 +167,14 @@ public PasswordAuthentication getPasswordAuthentication() {
conn.setRequestProperty("Content-Type", "application/json-rpc; charset=utf-8");
conn.setRequestProperty("User-Agent", "sdk=JAVA; sdk-version="+SDK_VERSION+"; lang-version="+System.getProperty("java.version")+"; platform="+System.getProperty("os.name"));
conn.setRequestProperty("charset", "utf-8");
conn.setRequestProperty("Authorization", "Basic " + tmp);
conn.setRequestProperty("Authorization", this._auth.toString());
conn.setRequestProperty("Content-Length", Integer.toString(postDataBytes.length));

// Check for LoginAs
if(this._logAs != null){
conn.setRequestProperty("CALLR-Login-As", this._logAs.toString());
}

// Send request
out = new DataOutputStream(conn.getOutputStream());
out.write(postDataBytes);
Expand Down Expand Up @@ -174,7 +203,7 @@ public PasswordAuthentication getPasswordAuthentication() {
}
}
}

// overload send method for optional parameters
@SuppressWarnings("rawtypes")
/**
Expand All @@ -199,13 +228,13 @@ private Hashtable<String, Object> createObject(String method, ArrayList params,
// Response analysis
private JsonElement parseResponse(String response) throws CallrException {
JsonElement result = null;

try {
result = new JsonParser().parse(response);
} catch (Exception e) {
throw new CallrException("INVALID_RESPONSE", -1, response);
}

if (result != null && result.isJsonObject() && result.getAsJsonObject().has("result")) {
return result.getAsJsonObject().get("result");
} else if (result.isJsonObject() && result.getAsJsonObject().has("error")) {
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/callr/auth/ApiKeyAuth.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.callr.auth;

public class ApiKeyAuth extends CallrAuthentication {
private String apiKey;

public ApiKeyAuth(String apiKey){
this.apiKey = apiKey;
}

@Override
public String toString(){
return String.format("Api-Key %s", this.apiKey);
}

}
3 changes: 3 additions & 0 deletions src/main/java/com/callr/auth/CallrAuthentication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.callr.auth;

public abstract class CallrAuthentication {}
35 changes: 35 additions & 0 deletions src/main/java/com/callr/auth/LoginAs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.callr.auth;

import java.util.*;
import com.callr.exceptions.CallrClientException;

public class LoginAs {

private String type;
private String target;

private static HashMap<String, String> loginAsTypes = new HashMap<String,String>() {
{
put("user", "user.login");
put("account", "account.hash");
}
};

public LoginAs(String type, String target) throws CallrClientException {
setLoginAs(type, target);
}

@Override
public String toString(){
return String.format("%s %s", loginAsTypes.get(this.type), this.target);
}

public void setLoginAs(String type, String target) throws CallrClientException {
if(!loginAsTypes.containsKey(type.toLowerCase())){
throw new CallrClientException("ERROR_IN_LOGINAS", -1, "type must be one of " + loginAsTypes.keySet().toString());
} else {
this.type = type.toLowerCase();
this.target = target;
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/callr/auth/LoginPasswordAuth.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.callr.auth;

import org.apache.commons.codec.binary.Base64;


public class LoginPasswordAuth extends CallrAuthentication {
private String login;
private String password;

public LoginPasswordAuth(String login, String password){
this.login = login;
this.password = password;
}

@Override
public String toString(){
String loginPassB64 = new String(Base64.encodeBase64((this.login + ":" + this.password).getBytes()));
return String.format("Basic %s", loginPassB64);
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/callr/auth/UserSessionAuth.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.callr.auth;

public class UserSessionAuth extends CallrAuthentication {
private String userSessionToken;

public UserSessionAuth(String userSessionToken){
this.userSessionToken = userSessionToken;
}

@Override
public String toString(){
return String.format("Session %s", this.userSessionToken);
}
}