Developing a RESTful API in Python with HappyX

Before that, I wrote about web application development on Nim here and here.

What? Which Nim? The title says Python!

Yes, until recently, writing web applications in HappyX web framework it was possible only with the help of Nim. At the moment HappyX also available in Python. Anyone can use the library if they don’t know Nim.

HappyX web framework

In this article, we will create fake GitHub API.

Creating a Project

At the moment, HappyX in Python does not have a CLI, so everything will have to be done paws handles. Although you can just create a project with PyCharm – that’s what we’ll do.

Creating a project in PyCharm

Now set the library itself happyx. We need a Python version 3.10 and higher.

pip install happyx

The first thing we need to do is import the function new_server from the library.

from happyx import new_server, JsonResponse

Next, let’s create a server and fake database.

app = new_server('127.0.0.1', 5000)
github_repos = [
    {'user': 'HapticX', 'repo': 'happyx', 'stars_count': 1, 'description': 'web framework'},
    {'user': 'python273', 'repo': 'vk_api', 'stars_count': 10, 'description': 'VK API for python'},
    {'user': 'pydantic', 'repo': 'pydantic', 'stars_count': 5, 'description': 'python models'},
]

Now you can declare the endpoints themselves.

Getting a list of repositories

@app.get('/repos')
def handle():
    return github_repos

Getting information on a specific repository

@app.get('/{user}/{repo}')
def get_repo(user: str, repo: str):
    for r in github_repos:
        if r['user'].lower() == user.lower() and r['repo'].lower() == repo.lower():
            return r
    return JsonResponse(
        {'response': 'failure'},
        status_code=404
    )

Add to the end of the file server start:

if __name__ == '__main__':
    app.start()

We can start the server:

py main.py

Or just run through PyCharm.

Is everything that simple?

Yes, HappyX is easy to use, both in Python and Nim.

Now to more complex
Let’s create a method for adding a new repository. To do this, change the import line:

from happyx import new_server, JsonResponse, RequestModelBase

And also add request model:

class Repository(RequestModelBase):
    name: str
    owner: str
    description: str

And the method itself:

@app.post('/repo[repo]')
def create_repo(repo: Repository):
    if repo.name == '' or repo.owner == '':
        return JsonResponse(
            {'response': 'failure', 'msg': 'repo should have name and owner!'},
            status_code=404
        )
    github_repos.append({
        'repo': repo.name, 'user': repo.owner,
        'stars_count': 0, 'description': repo.description
    })
    return JsonResponse({'response': 'success'})

That’s all 🙂
You can run the project and play with postman.

Our GitHub API on the knee created!

I will be glad if you find any bug and open issue in the repository.
Suggestions for improvements are also welcome 😉

Source: HapticX/happyx

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *