Hug is a rest API tool. Python projects are easy to use. Also defend the rapid development process. Services are written with Hug. Services are used in large projects. (Usually :))
What Will I Learn?
- Basic Rest Api use
- Hug.get()
- Hug.cli()
- Services production
- gunicorn
Requirements
- Python3
- Hug
- Gunicorn
Dependens study
- Decorator
- HTTP life crycle
Difficulty
- Basic
A simple example API application will be explained. We will release the application we made.
How to install Hug
- First you must set up a virtual environment. (optional)
mkdir project_directory
cd project_directory
virtualenv -p python3 src
source src/bin/activate
- Now, Hug install
pip install hug
pip install gunicorn
What is Hug?
Hug is a tool for lazy. (They say that!) Hug provides an API with very simple ways. It is possible to make large projects with a few simple functions. I have a recommendation. If you want to make a public API, use hug. Let's see what promises.
What is '@hug.cli()'?
It is tasked with forwarding requests from the Serviste. It is appended to the function. So it's a decorator. Think of an adrese gone. For example, requesting
url.com/tolgahanuzun
request. The service will see yourtolgahanuzun
and know which function to run. The@hug.cli()
task is simple.If you want a function to be checked by Hug, you must add
@hug.cli()
. Do not forget!
What is @hug.get()
?
- In the HTTP life cycle, a
get
request is issued to request data from the server. However, some functions can accept both GET and POST requests. We need to limit this to some point.
For example, you do not need a POST request for index
. It is therefore necessary to limit the allowed.
import hug
@hug.get('/index')
@hug.cli()
def root():
return 'Welcome Steemit!!'
Router construction for large projects
The above uses are generally preferred in small projects. If you have a large project, you need a router page.
I assume you have more than one pure functioning in your hand. Identify the road map from a single page. (router.py)
# router.py
import hug
import steemit
api = hug.API(__name__)
hug.get('/user', api=api)(steemit.get_user)
hug.get('/post', api=api)(steemit.post)
hug.get('/comment', api=api)(steemit.post_in_comment)
hug.get('/vote', api=api)(steemit.vote)
Run with hug gunicorn
- This is my favorite feature! It will save you from configuring. There is a command you can run, is very simple.
gunicorn app: __ hug_wsgi__
- Your application gives the name
hug_wsgi
. The gunicorn is taking care of the rest.
Example Application
Imagine that. Let's do a project on Steemit. Be a team project. For example, 3 people. 1 person, run steemite data. 1 person, make the web page. 1 person, write mobile application. Steemit data processing task is yours. With your API application you will serve two friends. For now, let's think simple. Just return data based on the incoming usernames.
import hug
from steem import Steem
def userDetails(name):
return Steem().get_account(name)
@hug.cli()
@hug.get(examples='name=tolgahanuzun')
def user_details(name: hug.types.text, hug_timer=3):
return {'user_details': userDetails(name),
'took': float(hug_timer)}
if __name__ == '__main__':
user_details.interface.cli()
- We will use the gunicorn to run the above code. Run the following code..
gunicorn app:__hug_wsgi__
http://127.0.0.1:8000
It's starting to serve, what is the here?We see a documentation that. We see a documentation that. It was very nice for friends who wrote web and mobile app (not?)
- Nice. Let's make a request on behalf of the user.
- Very Good. Let's now write a non-user name.
- Welldone. Let's make a request on behalf of the non-endpoint
What do the functions do?
userDetails
Method gives us member details.user_details
Connecting with Hug Cli.hug_timer
: response time
Results
- We can produce very fast projects.
- It's helping us debug.
- We can write the application with low response time.
Posted on Utopian.io - Rewarding Open Source Contributors