Pinata is the Internet's File API, allowing you to upload, manage, and deliver your files effortlessly—no drama, no compromises. With Pinata Android library, you can now bring Pinata’s powerful file-handling capabilities directly into your Android/Kotlin apps quickly and easily.
Add the dependency below into your module's build.gradle
file:
dependencies {
implementation("net.onyxmueller.pinata:pinata:1.0.1")
}
To access the API, you'll need to pass your Pinata API Key JWT and Dedicated Gateway Domain. More details at https://docs.pinata.cloud/quickstart.
val pinataClient = PinataClient.get(PINATA_JWT_TOKEN, PINATA_GATEWAY)
// Test your API keys and your ability to connect to the Pinata API
val testAuthResponse = pinataClient.authentication.test()
testAuthResponse.onSuccess { response ->
// Handle success
}.onError { code, message ->
// Handle error
}.onException { exception ->
// Handle exception
}
// Upload a file
val filesDir = applicationContext.filesDir
val file = File(filesDir, "image.jpg")
val uploadResult = pinataClient.files.upload(file)
// List all files
val listResult = pinataClient.files.list()
// Filter on files
val filteredListResult = pinataClient.files.list(name = "image.jpg")
// Get a specific file
val fileResult = pinataClient.files.get("1234567890")
// Create signed URL
val signResult = pinataClient.files.sign("1234567890", 604800) // expires in a week
// Update a file
val updateResult = pinataClient.files.update("1234567890", "new_name.jpg", mapOf("location" to "Earth"))
// Delete a file
val deleteResult = pinataClient.files.delete("1234567890")
PinataApiResponse
serves as an interface designed to create consistent responses from the Pinata API. It offers convenient extensions to manage your payloads, encompassing both body data and exceptional scenarios. PinataApiResponse
encompasses three distinct types: Success, Error, and Exception.
You can effectively handle PinataApiResponse using the following extensions:
- onSuccess: Executes when the PinataApiResponse is of type ApiResponse.Success. Within this scope, you can directly access the body
data
. - onError: Executes when the PinataApiResponse is of type PinataApiResponse.Error. Here, you can access the error
code
andmessage
here. - onException: Executes when the ApiResponse is of type PinataApiResponse.Exception. You can access the exception here.
Each scope operates according to its corresponding ApiResponse type:
val response = pinataClient.files.list()
response.onSuccess {
// this scope will be executed if the request successful.
// handle the success case
}.onError {
// this scope will be executed when the request failed with errors.
// handle the error case
}.onException {
// this scope will be executed when the request failed with exceptions.
// handle the exception case
}
Support it by joining stargazers for this repository. ⭐ And follow me for other creations.
- A big shoutout goes out to Erik (@eriklubbers) for reviewing and providing feedback.
Copyright 2024 onyxmueller (Onyx Mueller)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.