<h1>How to take your software online?</h1>
<p>We as developers write code and want it to be readable by everyone and if possible, it should be available for everyone to use it. But it happens many times that we don't know how to make it public and usable across our different apps. </p>
<p>Suppose if you wrote a python code which converts a given string to a poetry, suppose you had a NodeJS program written which backs up your file to your favorite cloud, suppose you had an incredibly special and proprietary Random Number Generator code how would you put it on cloud? How would you use it across multiple apps? How would use the same code in different environment?</p>
<p>Well one solution to all of this is you create a Web API and enclose your software in it and make it run on any of your favorite cloud provider. Now the question arises what is a Web API?</p>
<p>Now Web API's can be complicated to a person who is beginning to learn about computers and distributed devices but the idea behind it is quite simple. You have some piece of code that you want to use it on multiple end applications [namely mobile apps, web apps, admin panel's], and you don't want it to be directly embedded inside those end applications. You can do it with Web API's. These are nothing but some way through which computers can contact each other and ask each other to do various tasks when user at the client end wants to do something. These paths can also be called as endpoints.</p>
<p>Let's take an example, when you login to your Facebook, Instagram or Google Account what happens is your mobile app is contacting their servers and telling them "Bro, there's this guy <em>Gat786</em> having a secret ★★★★★ and wants to use data which is related to him". It typically happens over the internet and uses <a href="https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Technical_overview">HTTP request response protocol</a>. You can read about it in detail to know more about it. </p>
<p>While this is a huge and complicated example on how things are done. What you typically want to do is similar. You have some piece of code, let's say some function which accepts some input let's say i.e. a string and returns a result. Now you want to put make it available to everyone. The easiest way to do that is REST API's. </p>
<p>REST stands for *REpresentational State Transfer. * Which says that it is used to transfer state data which you can represent in front of user. Now REST uses HTTP protocol underneath what it means for you is you request for something by providing a path and the provide data related to that request. Server accepts that request checks whether everything is all right and then replies with a response if it finds one. </p>
<p>To understand about REST Services more in a detailed way you should watch this <a href="https://www.youtube.com/watch?v=SLwpqD8n3d0&ab_channel=ProgrammingwithMosh">video</a>.</p>
<p>Now for our blog let's take an example of a Python Function which accepts a request and returns a random quote every time.</p>
<pre><code class="language-py">import json
import random
def get_random_quote():
with open("./quotes.json","r",encoding='utf-8') as quotes_file:
quotes = json.loads("".join(quotes_file.readlines()))
length_of_array = random.randint(0,len(quotes))
return quotes[length_of_array]
</code></pre>
<p>This will read a file named <code>quotes.json</code> and return a random quote from the file when it finds one. I found that file on <a href="https://www.kaggle.com/akmittal/quotes-dataset?select=quotes.json">kaggle</a>. </p>
<p>Now let's try to make an API around this function.</p>
<p>It would be better if you have watched the video which is suggested to get more idea about how it works but I will explain over here in a bit. </p>
<p>How it works in REST is you send in distinct types of HTTP requests to do different tasks.</p>
<p>For E.g. </p>
<p>If you want to fetch some data you will send a GET request, if you want to delete some data you will send a DELETE request. If you want to create some new resource, you will send in POST request and same goes for updating you will send in a UPDATE request. For more information on this go <a href="https://www.w3schools.com/tags/ref_httpmethods.asp">here</a>. </p>
<p>For this scenario you get an idea where I am trying to get at. For this scenario we can use a GET request to make this function available as an endpoint.</p>
<p>Now for this we will use a python library called <a href="https://fastapi.tiangolo.com/">FastAPI</a>. For making stuff with FastAPI you will need uvicorn installed as well. You can install both like this - </p>
<pre><code class="language-shell">pip install fastapi uvicorn[standard]
</code></pre>
<p>With FastAPI creating a Rest Endpoint is as simple as creating a file <code>main.py</code> and writing the following code in it.</p>
<pre><code class="language-py">from typing import Optional
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
</code></pre>
<p>You can run this app with the command <code>uvicorn main:app --reload</code> </p>
<p>Once you have the app running hit the endpoint <code>localhost:8000</code> and you will get a message of <code>{"Hello": "World"}</code>.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1616154143261/uvzdXzRiT.png" alt="hello_world.png"></p>
<p>We must create a new function with path quote which will return our random quote to make our first API. It can be done like this.</p>
<pre><code class="language-py">from typing import Optional
import quotes_function
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/quotes"):
def get_quote():
return quotes_function.get_random_quote()
</code></pre>
<p>After adding this function now when you hit the endpoint <code>localhost:8000/quotes</code> you will see that it returns our random quotes. Now we can deploy it to some cloud provider, and we will be able to use it in our mobile apps, desktop apps or any other app we can imagine.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1616154218740/dxvdQXMLz.png" alt="quotes_response.png"></p>
<p>Real world scenarios are much more complicated than this, but this is a beginning and all the REST Apis are built on these concepts. </p>
<p>You can use Azure App Service for deploying this with ease, but every Cloud Provider has one or the other way with which you can deploy this and make it available across the globe.</p>
<p>I will in future write about how you can deploy this but for now this is it for this write-up. See you next time around. </p>
<p>Follow me on various platforms to get connected. Bye 👋👋</p>