theexoticman / web3_project_scrapper

0 stars 0 forks source link

Create microservice API on top of the DB using django framework #3

Open theexoticman opened 3 weeks ago

theexoticman commented 3 weeks ago

Create microservice API on top of the DB using django framework.

for ORM we are using django. also create the method that retrieves the from the DB passing the paramter "where project_name is like %project_name%"

also provide a curl query to test from the command line.

theexoticman commented 3 weeks ago

Sure! Here's an example of how you could implement the microservice API using Django:

  1. Start by installing Django and creating a new Django project:
pip install django
django-admin startproject myproject
cd myproject
  1. Create a new Django app within the project:
python manage.py startapp myapp
  1. Define the models for the tables in models.py file within the myapp folder:
from django.db import models

class Deployer(models.Model):
    name = models.CharField(max_length=100)
    # add other fields as per your requirements

class DeployedContracts(models.Model):
    deployer = models.ForeignKey(Deployer, on_delete=models.CASCADE)
    # add other fields as per your requirements

class Contracts(models.Model):
    project_name = models.CharField(max_length=100)
    # add other fields as per your requirements
  1. Create a file called db_query.py within the myapp folder to retrieve data from the DB:
from .models import Contracts

def get_contracts(project_name):
    contracts = Contracts.objects.filter(project_name__contains=project_name)
    return contracts
  1. Create a file called api.py within the myapp folder to define the API endpoints:
from django.http import JsonResponse
from django.views.decorators.http import require_GET
from .db_query import get_contracts

@require_GET
def get_contracts_api(request):
    project_name = request.GET.get('project_name')
    contracts = get_contracts(project_name)
    contract_list = [contract.project_name for contract in contracts]
    return JsonResponse(contract_list, safe=False)
  1. Modify the urls.py file within the myproject folder to include the API endpoint:
from django.urls import path
from myapp.api import get_contracts_api

urlpatterns = [
    path('api/contracts', get_contracts_api),
]
  1. Start the Django development server:
python manage.py runserver

Now you can test the API endpoint by making a GET request to http://localhost:8000/api/contracts?project_name=your_project_name.

For unit tests, create a tests.py file within the myapp folder and write test cases for your API methods and DB queries using Django's testing framework.

Note: This is just a basic implementation example. You may need to modify the code to fit your specific requirements and add more error handling, input validation, authentication, etc.

Let me know if you need any further assistance!