Skip to content

Instantly share code, notes, and snippets.

@jaisontj
Last active June 29, 2017 06:53
Show Gist options
  • Save jaisontj/40f1a7cf55fc5b889c43c4a71996a34a to your computer and use it in GitHub Desktop.
Save jaisontj/40f1a7cf55fc5b889c43c4a71996a34a to your computer and use it in GitHub Desktop.
Syntax for HasuraSDK
/*
What does the SDK help solve ?
1. Easy way to access the Auth, Data and Filestore Service
Components
1. HasuraClient
a.HasuraUser
b.DataService
c.QueryTemplate
d.CustomService
2. HasuraQuery
*/
//*******************************INITIALISATION********************************************************//
//Initialisation
Hasura.setConfig(new ProjectConfig.Builder()
.setProjectName("projectName") // or it can be .setCustomBaseDomain("somthing.com")
.enableOverHttp() // if not included, then https by default
.setDefaultRole("customDefaultRole") // if not included then "user" role is used by default
.setApiVersion(2) //if not included v1 is used by default
.build())
.enableLogs() // not included by default
.initialise(this);
//******************************CLIENT********************************************************************//
HasuraClient client = Hasura.getClient(); //Client is built using the config specified above
//The client has the following functionalities
//1. HasuraUser
//2. Data Service
//3. QueryTemplate Service
//4. FileStore
//5. CustomService
//******************************AUTH********************************************************************//
//Checking if the client has a logged in user
HasuraUser user = client.getUser(); // Hasura.getClient.getUser();
if (user.isLoggedIn()) {
//This user is logged in
} else {
//This user is not logged in
}
//SignUp
HasuraUser user = HasuraClient.getUser();
user.setUsername("username");
user.setPassword("password");
user.signUp(new SignUpResponseListener() {
@Override
public void onSuccessAwaitingVerification(HasuraUser user) {
//The user is registered on Hasura, but either his mobile or email needs to be verified.
}
@Override
public void onSuccess(HasuraUser user) {
//Now HasuraClient.getCurrentUser() will have this user
}
@Override
public void onFailure(HasuraException e) {
//Handle Error
}
});
//Login
HasuraUser user = HasuraClient.getUser();
user.setUsername("username");
user.setPassword("password");
user.login(new AuthResponseListener() {
@Override
public void onSuccess(HasuraUser user) {
//Now HasuraClient.getCurrentUser() will have this user
}
@Override
public void onFailure(HasuraException e) {
//Handle Error
}
});
//*******************************************DATA SERVICE*****************************************************//
//Using the data service
//If a signedUp/LoggedIn user is set on the HasuraClient
//Then every call made using HasuraClient will be authenticated by default with "user" as the default role.
//A different default role can be specified at the time of Hasura.init();
HasuraClient client = Hasura.getClient();
client.useDataService() // Hasura.getClient.useDataService()
.setRequestBody(JsonObject)
.expectResponseType(MyResponse.class)
.executeAsync(new Callback<MyResponse>, HasuraException>() {
@Override
public void onSuccess(MyResponse response) {
//Handle response
}
@Override
public void onFailure(HasuraException e) {
//Handle error
}
});
//In case you want to make the above call for an anonymous user
client.asAnonymousRole()
.useDataService()
.setRequestBody(JsonObject)
.expectResponseType(MyResponse.class)
.executeAsync(new Callback<MyResponse>, HasuraException>() {
@Override
public void onSuccess(MyResponse response) {
//Handle response
}
@Override
public void onFailure(HasuraException e) {
//Handle error
}
});
//In case you want to make the above call for a custom user
client.asRole("customRole") //throws an error if the current user does not have this role
.useDataService()
.setRequestBody(JsonObject)
.expectResponseType(MyResponse.class)
.executeAsync(new Callback<MyResponse>, HasuraException>() {
@Override
public void onSuccess(MyResponse response) {
//Handle response
}
@Override
public void onFailure(HasuraException e) {
//Handle error
}
});
//*******************************************QUERY TEMPLATE SERVICE*****************************************************//
//Syntax is the same as data service, replace useDataService with useQueryTemplateService(TemplateName)
//*******************************************FILESTORE SERVICE*****************************************************//
client.useFileStoreService()
.uploadFile("fileName", /*File*/, /*mimeType*/"image/*", new FileUploadResponseListener() {
@Override
public void onUploadComplete(FileUploadResponse response) {
//FileUploadResponse contains the following:
//FileID
//UserID
//Created_at
}
@Override
public void onUploadFailed(HasuraException e) {
}
});
client.useFileStoreService()
.downloadFile(1 /*FileId*/, new FileDownloadResponseListener() {
@Override
public void onDownloadComplete(byte[] data) {
}
@Override
public void onDownloadFailed(HasuraException e) {
}
@Override
public void onDownloading(float completedPercentage) {
}
});
//*******************************************CUSTOM SERVICE*****************************************************//
//Using a custom service - Retrofit Support
//Let's say you have a custom service set up on Hasura called "api"
//Your external endpoint for this custom service would be -> "api.<project-name>.hasura-app.io"
//This is a wrapper over Retrofit for custom services,
//assuming that your interface with the api definitions is called "MyCustomInterface.java"
//Step1: Build your custom service (before Hasura Init)
RetrofitCustomService<MyCustomInterface> cs = new RetrofitCustomService.Builder()
.serviceName("api")
.build(MyCustomInterface.class);
//Step2: Add this custom service during init
Hasura.setProjectConfig(new HasuraConfig.Builder()
.setProjectName("projectName") // or it can be .setCustomBaseDomain("somthing.com")
.enableOverHttp() // if not included, then https by default
.setDefaultRole("customDefaultRole") // if not included then "user" role is used by default
.setApiVersion(2) //if not included v1 is used by default
.build())
.enableLogs() // not included by default
.addCustomService(cs)
.initialise(this);
//Step3: Accessing Custom Service
//Here, user is of type HasuraUser
MyCustomService cs = client.useCustomService(MyCustomInterface.class);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment