Writing DRF reusable APIs

Viewed 107

I am trying to create a PIP package for a set of reusable APIs. I have already implemented those API in a project and are working perfectly fine.

I started looking for the way to package these API so that it can be integrated with any other project and that is how I learned about setuptools. To gain a little hands on experience with setuptools I simply created a PIP package for a helloworld() program.

Now, I have started creating the package for the API I have in my DRF app. I created an empty directory and moved all the modules of this DRF app into that directory. The setup.py file is well configured to install the dependencies which are required by these modules.

However, now I want to start this application and see if it is working or not. So when I run python manage.py runserver it didn't work because of an obvious reason - No such file or directory.

Moreover, there are certain configuration which are required for this package to work and in my previous project it is defined in settings.py file.

setup.py

import os
from setuptools import setup, find_packages


# allow setup.py to be run from any path
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))


def get_doc():

    with open("README.md", "r") as fh:

        return fh.read()


setup(
    name="django_restframework_2fa",
    version="0.0.1",
    description="Some description",
    long_description=get_doc(),
    long_description_content_type="text/markdown",
    url="https://github.com/jeetpatel9/django-restframework-2fa.git",
    py_modules=['helloworld'],
    package_dir={'': 'some_dir'},
    author='Jeet Patel',
    author_email='****@gmail.com',
    python_requires='>=3.7',
    include_package_data=True,
    zip_safe=False,
    packages=find_packages(
        exclude=['tests', 'tests.*', 'licenses', 'requirements']),
    classifiers=[
        "Environment :: Web Environment",
        "Programming Language :: Python",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.6",
        "Programming Language :: Python :: 3.7",
        "Framework :: Django",
        "Framework :: Django :: 3.1",
        "Framework :: Django :: 3.2",
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Operating System :: OS Independent',
        'Topic :: Internet :: WWW/HTTP',
    ],
    install_requires=[
        "djangorestframework",
        "django",
        "djangorestframework-simplejwt",
        "pyjwt",
        "twilio==6.55.0",
        "djangorestframework-simplejwt==4.6.0",
        "django-phonenumber-field==5.2.0",
        "phonenumbers==8.12.24",

    ],
    extra_requires={
        "dev": [
            "pytest>=3.7",
            "twine 3.4.1",
        ]
    }
)

I have looked everywhere on internet to understand how to -

  1. define settings.py file while developing a PIP package.
  2. start this package for development and testing.

Any references to the articles or tutorials will also work for me.

This community is my last hope.

1 Answers

The pip package will only contain the app files which are to be re-used in other projects; so yes, its very inefficient to build a package every time you need to run and test it.
One solution would be to create a tests project in your working directory, and write whatever test files you need (views, urls, settings etc.) and put them in tests. Let me show an example structure:

product_management  ## working directory
├─ .gitignore
├─ README.md
├─ manage.py
├─ setup.cfg
├─ setup.py
├─ tests  ## tests project
│  ├─ __init__.py
│  ├─ asgi.py
│  ├─ requirements.txt
│  ├─ settings.py
│  ├─ urls.py
│  ├─ views.py
│  └─ wsgi.py
└─ product_app  ## re-usable app
   ├─ __init__.py
   ├─ admin.py
   ├─ apps.py
   ├─ migrations
   │  ├─ 0001_initial.py
   │  └─ __init__.py
   ├─ models.py
   ├─ serializers.py
   └─ views.py

Remember, you can write an entire django project without using any apps; which we doing here using our tests project. This can also serve as a documentation on how to integrate those re-usable APIs in other projects. The manage.py in the root can be used to start the tests project.

Now, since the tests folder is technically of no use inside the pip package, it can be excluded by defining as such:

setup.py

from setuptools import setup, find_packages

setup(
    packages=find_packages(exclude=("tests",)),
)

You can learn more by looking at github repositories for various open-source packages listed on pypi.org.

Related