3. File Storage
Upload Files
Hat API Android
allows you to also upload files in your PDA. You can read more about HAT File Storage
here.
The uploading of a file is a multi-step process:
Request from
PDA
a temporaryURL
to upload the fileUpload the file to the
URL
Mark the file as complete
Step 1
To start a file upload you have to call the function below:
fileName
is the desired name of the file. You can either randomise it or provide meaningful names, it's up to you.token
is the user's token. This is needed to authenticate the user with thePDA
.userDomain
is the user'sPDA Address
. This is needed to form theURL
to upload the file.tags
is an array ofString
with useful tags to make searching for that file easier.completion
is an optional callback function of type((FileUploadObject, String?) -> Unit)?
,FileUploadObject
is a custom object ofHAT API Android
describing the file upload object. The OptionalString
is the refreshed token returned from thePDA
. This is executed on success and it includes theURL
that you have to upload your file.errorCallback
is an optional callback that is executed when the request has failed. The type of the callback function is((HATError) -> Unit)?
.HATError
is a custom object describing the errors that have occurred during the querying of the tables in the database.
For the purpose of this example let's say that fileName
is testfile
, tags
is just test-tag
, userDomain
is postman.hubat.net
and token
is token. That will translate to the request below:
A successful request will result in a status code
200 and the response will look like this:
The above response is described by FileUploadObject
. The values are explained below:
fileId
is the ID of the file on thePDA
, it is formed by combining thesource
andname
of the file.name
is the name of the file that we assigned. In case of a duplicatefileId
,PDA
will append an incremented number at the end.source
is the source that requested to upload this file. It's good to have your app name as thesource
.dateCreated
is the date that the file was created inISO
formatlastUpdated
is the date that the file was last modified. You can change thename
,status
and make the file private or not.tags
is an array ofString
that allow for easier search of the file in the future. You can add multiple tags.status
is aArray<Pair<String, String>>
Key-Value pair and it can beNew
, when you just requested the file upload,orCompleted
after the uploading has finished and you marked the file asCompleted
.contentUrl
is theURL
you will use to upload the file. ThisURL
is short-lived as its use is just to provide aURL
for your app to upload the file.contentPublic
is the flag that indicates if the file isprivate
orpublic
. You can mark a file asprivate
orpublic
after the file has been uploaded.permissions
is an array of Key-Value pairs of typeArray<Pair<String, Any>>
. In the above example, the permissions provide the user with thatuserId
the ability to have access to the file even if the file is marked asprivate
.
A request that has failed will look like this:
error
is the error that has occurredmessage
is a more descriptive message about theerror
that has occurred
Step 2
The second step is to upload the actual file. There is no limitation on what type of file you can upload. You can upload images, videos, documents etc. It's up to the application to ensure that the file is uploaded correctly and that it can then download the file and read it without problems.
To upload a file you can use the function below included in HAT API Android
:
image
is type ofFile
. This function requires you to convert your file intoFile
type first. You can convert aBitmap
image to `File``.url
is theURL
to upload the file. As we said earlier thisURL
is thecontentUrl
provided by theDA
in the first step.completion
is a callback function of type(_ r: HATNetworkHelper.ResultType) -> Unit)
. It can be eitherisSuccess(var statusCode: Int?, var error: Error?, var json: Json?, var resultString: String?, var token: String?)
if the request was successful orHasFailed(var statusCode: Int?, var error: Error?, var json: Json?, var resultString: String?, var token: String?)
if the request has failed for some reason. You can read more about it here.
A successful request will look almost exactly like the original one:
The only thing that has now changed is the lastUpdated
field.
A request that has failed will look like this:
Step 3
When the file has finished uploading it's required to mark the file as completed
, or else the file will be deleted. To do so you have to make another request. To achieve that you have to call the next function:
fileID
is the file id to be marked ascompleted
token
is the user's token. This is needed to authenticate the user with thePDA
.tags
is an array ofString
with useful tags to make searching for that file easier. This is added beforeHAT API Android
returns you the full file.userDomain
is the user'sPDA Address
. This is needed to form theURL
to mark the file ascompleted
.completion
is an optional callback function of type((FileUploadObject, String?) -> Unit)?
,FileUploadObject
is a custom object ofHAT API Android
describing the file upload object. The OptionalString
is the refreshed token returned from thePDA
.errorCallback
is an optional callback that is executed when the request has failed. The type of callback function is((HATError) -> Unit)?
.HATError
is a custom object describing the errors that have occurred during the querying of the tables in the database.
A successful request will look almost exactly like the original one:
The only thing that has now changed is the lastUpdated
field and the status
field. The status
is marked as Completed
and includes the file size
in bytes.
A request that has failed will look like this:
error
is the error that has occurredmessage
is a more descriptive message about theerror
that has occurred
Mark file as public
By default the uploaded files are marked as private
. You can change that by calling this function:
fileID
is the file id to be marked aspublic
token
is the user's token. This is needed to authenticate user with thePDA
.userDomain
is the user'sPDA Address
. This is needed to form theURL
to mark the file aspublic
.completion
is an optional callback function of type(Boolean -> Unit)
returnstrue
errorCallback
is an optional callback that is executed when the request has failed. The type of callback function is((HATError) -> Unit)?
.HATError
is a custom object describing the general error that occurred on thePDA
.
A successful request will have a statusCode
of 200 and will look almost exactly like the original one:
The only thing that has now changed is the lastUpdated
field and the contentPublic
field.
A request that has failed will look like this:
error
is the error that has occurredmessage
is a more descriptive message about theerror
that has occurred
Mark file as Private
By default the uploaded files are marked as private
. You can change that by calling this function:
fileID
is the file id to be marked asprivate
token
is the user's token. This is needed to authenticate user with thePDA
userDomain
is the user'sPDA Address
. This is need in order to form theURL
to mark the file asprivate
completion
is an optional callback function of type(Boolean -> Unit)
returnstrue
errorCallback
is an optional callback that is executed when the request has failed. The type of the callback function is((HATError) -> Unit)?
.HATError
is a custom object describing the general error that occurred onPDA
A successful request will have a statusCode
of 200 and look like almost exactly the same as the original one:
The only thing that has now changed is the lastUpdated
field and the contentPublic
field.
A request that has failed will look like this:
error
is the error that has occurredmessage
is a more descriptive message about theerror
that has occurred
Search files
You can also search for files with Hat API Android
. To do so you can use the next function:
userDomain
is the user'sHAT PDA Address
. This is needed to form theURL
to fetch the files.token
is the user's token. This is needed to authenticate the user with thePDA
.status
is the status of the file. You can search forCompleted
files only or forNew
files.name
is the name of the file. You can search for a specific file or leave this as an emptyString
in case you want to fetch all the files that match thestatus
andtags
.tags
is an array ofString
with useful tags to make searching for that file easier. You can search file with specific tags.completion
is a callback function of type(List<FileUploadObject>, String?) -> Unit
. The first parameter is an array ofFileUploadObject
, it contains all the files that thePDA
found during the search. The second parameter is an optionalString
, it is a refreshed token returned from thePDA
. It can benil
.errorCallback
is an optional callback that is executed when the request has failed. The type of the callback function is((HATError) -> Unit)?
.HATError
is a custom object describing the general error that occurred onPDA
.
A successful request will have a statusCode
of 200 and look like this:
A request that has failed will look like this:
error
is the error that has occurredmessage
is a more descriptive message about theerror
that has occurred
Delete files
You can also delete files from the PDA
using the next function:
fileID
is the file id on thePDA
token
is the user's token. This is needed to authenticate the user with thePDA
.userDomain
is the user'sHAT PDA Address
. This is needed to form theURL
to delete the file.completion
is a callback function of type(Boolean, String?) -> Unit
. The first parameter is true. The second parameter is an optionalString
, it is a refreshed token returned from thePDA
. It can benil
.errorCallback
is an optional callback that is executed when the request has failed. The type of the callback function is((HATError) -> Unit)?
.HATError
is a custom object describing the general error that occurred onPDA
.
A successful request will have a statusCode
of 200 and will look almost exactly like the original one:
The only thing that has now changed is the status
to Deleted
.
A request that has failed will look like this:
error
is the error that has occurredmessage
is a more descriptive message about theerror
that has occurred
Last updated