Offical SDK for Java.
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
dependencies {
compile 'com.github.TeamBaseHQ:base-java-sdk:master-SNAPSHOT'
}
package com.base;
import com.base.Auth.AccessToken;
import com.base.Exceptions.BaseException;
import com.base.Exceptions.BaseHttpException;
import com.base.Exceptions.Http.InputError;
import com.base.Models.Media;
import com.base.Models.User;
import java.io.File;
import java.util.Map;
public class Main {
public static void main(String[] args) {
createUser();
// getUserAccessToken();
}
public static void createUser() {
Base base = getBase();
try {
User user = base.userService().createUser("John Doe", "abcd@gmail.com", "abcd1234");
System.out.println(user.getName());
} catch (InputError e) {
// Input Error
// Fetch all the errors from the Error Bag
Map<String, String[]> errors = e.getErrorBag().getErrors();
for (String key : errors.keySet()) {
for (String error : errors.get(key)) {
System.out.println(error);
}
}
} catch (BaseHttpException e) {
System.out.println(String.valueOf(e.getResponse().getStatusCode()));
}
}
public static void getUserAccessToken() {
Base base = getBase();
try {
// Log in User
AccessToken accessToken = base.getUserAccessToken("abcd@gmail.com", "abcd1234");
// Set the Access Token for the current User
// So that this can be used for all the subsequent requests.
base.getClient().setAccessToken(accessToken);
User user = base.userService().getCurrentUser();
System.out.println(user.getName());
File file = new File("C:\\propic.jpg");
Media media = base.userService().uploadProfilePicture(file);
System.out.println(media.getUrl("small"));
} catch (BaseException e) {
e.printStackTrace();
}
}
private static Base getBase() {
BaseClient baseClient = new BaseClient();
baseClient.setApiUrl("http://backend.baseapp.io/api");
baseClient.setClientId("9").setClientSecret("tJDLmxkA2ogjEcU3qRtUwVSitkD9VJltOKnYh5AJ");
return new Base(baseClient);
}
}