The API Dependency Conundrum [SOLVED]
Are you a mobile App developer? Do you work in a team along with Web developers? Does your app depend on APIs provided by the Web team? Have you ever faced issues with unavailability of APIs to develop or test a certain feature in your app? If your answer for all the above questions was yes, then this article is for you.
Hello everyone, It’s been a while since I wrote my last article where I talked about easing off your App distribution process via Firebase App Distribution. You can find it here.
Now let’s move to the topic which we would be discussing in this article. I would be touching in on a topic or rather we can all it as a difficulty which almost every mobile app developer regardless of the domain, be it Android, iOS or Hybrid faces when working with APIs. Let’s break this article into the 3 parts –
Mega Bundle Sale is ON! Get ALL of our React Native codebases at 90% OFF discount 🔥
Get the Mega BundleThe Problem
While working in a fast paced environment where everyone is rushing towards completing their tasks & features, we often encounter a dependency on web teams for providing us APIs. It can be a certain feature like login, or getting a list of items from web to display them in List View or post something on the web, it can be anything.
Let’s take an example of building a List-View which is powered by content from API. To implement List-Views, Recycler Views or Table Views, we need data to simulate and test our development. If the APIs are not ready by that time, we end up creating mock functions which returns an array and pass that as a data source to our views. While this may help you test your flow or views, below are some downsides of this approach –
-
Unnecessary Code – Adds unnecessary code which we would have to clean up when connecting to actual APIs.
-
Incomplete Code – We don’t write our actual Networking code : the API methods & functions that would be fetching us the data and hence a lot of code remains to be actually written when we are to connect with the actual APIs.
-
Incomplete features – Since your features depend on the API and you just mocked it in your code to test, your actual feature implementation is incomplete and you would have to come back at a later stage to pick up where you left and start developing the remaining part again.
-
2x Lost Effort – Both the Android and iOS teams would have to do this in their respective code bases and hence when you look from a Lead’s perspective, the time lost is actually 2x.
The Solution
What if..? What If..? What If I told you there is a way to avoid all the above. A way wherein you can write all your networking code – the actual production level networking code in the app. A way wherein you can write your exact functions to fetch you the data from API. Write your entire API Models, DB Models exactly the way it should have been written. A way where you don’t have to write mock functions to get you the data.
Yes! You heard it right, you can mock the APIs and this would not be in your app’s code-base. We would be utilising a 3rd party tool to create these APIs and provide us the data and we would be doing it only once and the same can be accessed by both our Android and iOS teams.
Now there are a lot of Mock API service providers, some are paid, some are free, maximum are online while some are offline apps. To name a few – Mockoon, Postman, Swagger, TestAPI, Mocky etc.
I wouldn’t be comparing these services in this article, that would have to be a separate article in itself & I would like to know if you readers would be interested in that. For now I’ll just use what I have been using for a while and really like – Mockoon. I’ll go ahead and demonstrate how easy it is to setup, create Endpoints, enter your response and start using them in your app.
The Solution – Demonstrated
The tool I would be using to demonstrate building your API and connecting it with your app is Mockoon. It is an free app available for all the platforms – Mac, Linux and Windows with unlimited Mocking. Also yes it ain't an online web app so your data and API structure remains to yourself. That's great right?
So let's begin by installing Mockoon from https://mockoon.com/
Open the app and start creating your first mock API. I'll be creating a sample Login API wherein I'll setup both success and failure response for our login flow.
Creating your first Mock API :
-
Add your Environment
Your Environment is your Project. So if you are working on 2 different apps/projects, you would be creating a separate environment for each one. In Short : 1 Environment = 1 Project
-
Add your Environment route
This is that part of URL which goes after your base URL and before your API Endpoint. Example : https://www.sampleapp.com/api/v1/login The "api/v1" would be your environment route over here. You can add custom route to your base URL to keep your mock API structure as close to your actual APIs. I usually keep it similar to our backend route structure so when I actually have to replace my Mockoon API endpoint with our actual APIs, I just have to update the host URL.
-
Add your API route
This is your actual API Endpoint. Examples of this would be /login, /movies etc. Over here I would be creating an API endpoint named – login