Understanding Marvels API from an Android Developer Perspective.
Tags: android, kotlin, mobile, androiddevelopment
Published: 2021-06-12 20:48:13

An interesting aspect of working as an Android Developer is working with some type of remote data by connecting to the internet. And often times we would have to rely on some type of API to use in our apps. 

I have look at some API Documentation and wow, its been nothing but fun. In fact I think that would be another topic for a separate article. In this article, we are going to look at the Marvels api and see how we can understand some of it in using them in your android apps.

###Problem
Honestly I would say the api docs of Marvel can do much better in helping those who build client side applications to effectively use it. I feel you shouldn't bother going outside the docs in learning how to use it.

## Getting your API KEY
This is pretty straightforward - you simply go to this url https://developer.marvel.com and then create a free account which allows you to make 3000 calls per day on the api.

## Parameters Needed 
We would be focusing on using the API to fetch a list of public comics with some meta-data. 

This url carefully explains what parameters would be needed in using the API https://developer.marvel.com/documentation/authorization. Below are the parameters needed: 
* Public Key =="apikey" (Note "apikey" != "apiKey" when passing them as parameters to your @GET function while using Retrofit)
* ts (Also know as a timestamp, it just wants to know the exact time the request was made, I don't know why but that's what it needs).
* hash : A md5 digest of your timestamp,public key and private key which are provided in your dashboard. If you are curious about how it really works you could do more research about that if you want, but its not really important for this aspect. Basically its just an Algorithm that converts some type of data in this instance your ts + public key and private key into a length string value. In code this would be a function that concatenates all of the strings and creates the hash value which should be in a form of string.

Now you would say, well thats pretty obvious and straight to the point. Well it is, but wasn't stated like that for those that might want to use it in a mobile client. 

Looking at the docs ![Picture of client auth](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pp8r2y7pf1lb4k5a7vvo.PNG) you might want to follow this instructions for your android app which is client. Please follow the instructions for server side Applications because the client in focus is for browser based applications.

*Testing it in PostMan*
If you are like me and always test your API Endpoints using Post Man then you should take a look at this amazing GitHub repository https://github.com/doamaral/postmanmarvelapi

## Having fun with the API
To really see how the api works and know what type of JSON data would be returned you should go to the **Interactive Documentation tab section** 

Looking at the GET public apis ![Public API](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lxzl9pz1p29fh8qf1xuj.PNG) You can decide to play around with it and know how the JSON Response is and then use that to model your Kotlin or Java Objects. 

###Android Project

Now we know how the API Works we can then use them in our application as we would when consuming other API. I would briefly drop some hints below that can guide you.

## Kotlin Model
Looking at the JSON Response after playing around with it, you would see that it involves we having 4 data class in to represent our models. ![Model Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0e472wynw4qarxlg4sob.PNG) 

Comic.kt ![Comic Model](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c8kx3escbjxafn0kb12s.PNG)

ComicResponse.kt ![ComicResponse Model](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/02l3tawjcsbxf4905mj7.PNG)

Data.kt ![Data Model](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i6va3w3kppb69czinnwb.PNG)

Thumbnail.kt![Thumbnail Model](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ud5sn1qz9vwely4vrz9v.PNG)
 
Assuming you decide using Retrofit, then you would need to create an interface and declare your method/function without any body. Remember that you are making a @GET request so it has to use the above mentioned query parameters using the @Query Annotations like this: ![Retrofit Interface](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vtkoof31awjf7fubq1jj.PNG)

So looking at my Constant.kt file![Constant](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/677vpnh1paeqc377cxzq.PNG)

Then you can go ahead and use whatever method that seems convenient for you to use in fetching the data to your activity. 



*Note*
While using Glide to load your images from the API please remember it requires the thumbnail path of type string and also the extension type appended to it. ![Adapter](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/he0uwgj0gw7q37fqduox.PNG)

I hope this short article will help you use the Marvel API with ease because it offers some really cool responses that you can use in your Android Apps. Till next time bye :)