5. Data Plugs
Data Plugs
In the HAT ecosystem, a Data Plug aka Data Plugin is a minimal API-to-API web service with the primary purpose of fetching data from 3rd party API and pushing it to an individual’s PDA. Due to the security considerations and limited scope of required functionality, each data plug is granted a write-only access to any particular PDA.
Fetch Data Plugs and Applications
HAT API Android provides an API call for you to fetch all the available Data Plugs:
HATExternalAppsService().getExternalApps(
userToken: userToken,
userDomain: userDomain,
completion: availablePlugsReceived,
failCallBack: errorRetrievingDataPlugs)As you would have probably noticed, the function name is getExternalApps. Applications and Data Plugs are not only represented by the same structure but they have the same API as well.
userTokenis the user's token to authenticate with thePDAuserDomainis the user'sPDA addressused to form the url to fetch theData Plugscompletionis a callback function that is used when the request is successful with a type of((List<HATApplicationObject>, String?) -> Unit).The first parameter is an array of
HATApplicationObject. This is the structure ofData PlugsandApplications. More on that in the next section.This array contains all the
Data PlugsandApplicationson thePDA. The second parameter is an optionalString, the refreshed user token that thePDAreturns.failCallBackis a callback that is used when the request has failed. The type of the function is((HATError) -> Unit).HATErroris a custom object describing the errors that have occurred during the querying of the tables in the database.
A successful response will have statusCode 200 and look like this:
applicationholds the most parts of the structure. It consists of the following 7 values:
idin theapplicationpart of the JSON is theidvalue of theData Plugin theHAT.kindin theapplicationpart of the JSON has 4 values holding the various urls and the type of theData Plug:urlis the url of theData PlugorApplicationto use in webiosUrlis the iTunes url of theApplicationin theApp Store. It's optional and only there forApplicationtypeandroidUrlis the Play Store url of theApplicationin thePlay Store. It's optional and only there forApplicationtypekindis the kind. As we mentioned earlier,Data PlugandApplicationis represented by the same structure and use the same APIs.Therefore, the
kindcan beApporData Plug. When for example you want only theApplicationsthen simply you are going to filter the array based on thekind.
infoin theapplicationpart of the JSON has 12 values:versionis the app's version number in thePDA. This may be different from the version number of theApplicationin theApp StoreorPlay StoreupdateNotesis essentially the release notes for the latest version. It is formed by theheader, the title of the release notes, and thenotes, an array ofStringto easily show the notes as an unordered list.publishedindicates if the app is published or not. It can be that the app is not yet published, so it's not yet live, and it's a testing, beta, app.nameis the official name of the app.headlineis the headline or subtitle of the app. It can be placed underneath the name, for example, to provide more context for the app if needed.descriptionallows you to describe your app better with a longer text. Also, it has support for different formats of text,text,markdownandhtml.termsUrlis aURLfor the terms and conditions. Users must be able to read them if they wish.dataUsePurposeis a text explaining why and how the app will use the data.supportContacta support email. Users will use this email to contact you.ratingis an optional object formed byscorewhich describes the permissions and the data use using a letter-based scoring system. You can read more here.dataPreviewis actually an array ofSheFeedstructures. This is used to preview items, if any, instead of making another network request to fetch those items. The array can be empty.graphicsis a structure formed by 3 values:banneris theURLof the banner image.logois theURLof the app logo image.screenshotsis theURLof the app's screenshots used to showcase what this app offers.All the above 3 values use the same structure.
PDAimages can be categorised assmall,normal,largeandxlarge. Exceptnormalall are optional values.
developerconsists of some basic info about the developer. This includes:id,name,urlandcountry.permissionsincludes 3 values:rolesGrantedwhich is an array ofroleanddetaildescribing the roles granted in theApplication.dataRetrievedwhich is a type ofDataOfferRequiredDataDefinitionObjectV2but it's out of the scope of this guide. It describes the data that theApplicationwill retrieve.dataRequiredwhich is a type ofHATExternalAppsDataRequiredObjectbut it's out of the scope of this guide. It describes the data theApplicationrequires.
setupencapsulates all the urls that theApplicationneeds in order to setup. The structure is the same askindbut the url's can be different.statusprovides some info about the status of the application:compatibilityis the last version that is compatible with the current one.versionReleaseDateis the date, inISOformat, of the latest release.kindthe kind of the release. It can be eitherInternalorExternal.
setupis Boolean value indicating if this app has been setup successfullyenabledis Boolean value indicating if this app has been enabled successfullyactiveis Boolean value indicating if this app is currently active or notneedsUpdatingis Boolean value indicating if this app needs updating. AnApplicationmay need an update in case the terms have changed or a new version has been released.
A request that has failed will look like this:
erroris the error that has occurredmessageis a more descriptive message about theerrorthat has occurred
Set up Application
Setting up an application is similar to the login process. First you have to check the kind ; is it App, or Data Plug. The 2 are different in how you set them up:
In the case of an App you first have to check if the app is already installed on the user's device. You can check with:
where appURL is the androidUrl from the Application -> setup. If the user has the app installed, then you have to form a URL like this:
hatAddressis the PDA's (fully qualified domain) address, e.g.postman.hubat.netappIDis the id of the application to setup.appURLis the URL to which the user should be sent after completing authentication. In this case it is theandroidUrl.fallbackis the URL to which the user should be sent in case the authentication has failed. Optional. For anAndroidapplication that would probably be:$applicationName://failedand has to be added in the AndroidManifest file of the project in xml as a data scheme.
As you may have noticed this is exactly the same URL as the login URL; there is a reason for this. The setup process goes through the web. Just like during the login process, the user will be asked to insert their password before continuing to the next step. The next step is accepting the terms and conditions and giving the permissions the app requires to function. If everything goes smoothly, PDA will redirect back to the success redirect. The redirect url will now include a parameter which is the token for the particular app. Android will ask the user if they want to launch the particular app. If the user selects 'yes' then the app will be launched. If there is a problem, PDA will redirect to the fallback url and you have to hide Chrome and show a message that tells the user something has gone wrong.
If you don't know how to handle WebView, you can read here.
If the user does not have the app installed, you first have to redirect them to the Google Play Store so they can download the app. The URL to use is the url in Application -> kind. Again the only thing you have to do is open it using the browser. When user has installed the app they may then tap again to set up the Application.
Set up Data Plug
Setting up a Data Plug is a lot simpler than setting up an Application. You just open the url in Application -> kind with a browser and follow the instructions. Again, the user will first have to enter the password. After that each Data Plug has a different way of asking permissions and registering with the PDA.
State of the Application and Data Plug
Application and Data Plug can be in multiple states: Setup, failing, need updating, disabled. To respond in state changes you can use the following snippet:
Runningis when theApplicationorData Plugisenabled,activeand doesn'tneedsUpdatingFetchingis when theApplicationorData Plugisenabled,active, it's anAppand themostRecentDataisnull. This means that no data have yet been saved in thePDA.Updateis when theApplicationorData Plugis marked asneedsUpdatingin the API response. This means the user has to go through the setup process again.Failingis when there is a problem in theApplicationorData Plug. Setting up again might or might not help. If this persists please contact us:[email protected].Untouchedis when theApplicationorData Plughas never been setup before
Disable Data Plug or Application
You can also disable a Data Plug or an Application. To do that you can call the next function:
appIDis the id, inApplicationstructure, of theApplicationorData PluguserDomainis the user'sHAT addressused to form the url to disable theData PlugorApplicationuserTokenis the user's token to authenticate with thePDAcompletionis a callback function that is used when the request is successful with a type of((HATApplicationObject, String?) -> Unit).The first parameter is the disabled
HATApplicationObject. The second parameter is an optionalString, the refreshed user token that thePDAreturns.failCallBackis a callback that is used when the request has failed. The type of the function is((HATError) -> Unit).HATErroris a custom object describing the errors that have occurred during the querying of the tables in the database.
A successful response will have statusCode 201 and look like this:
If you are not familiar with the structure, you can read more here.
A request that has failed will look like this:
erroris the error that has occurredmessageis a more descriptive message about theerrorthat has occurred
Last updated
Was this helpful?