first I make a django middleware.py for django (Package)

from dapycorelogger.api import api import datetime

class Middleware:

def __init__(self, get_response):
    self.get_response= get_response
    

def __call__(self, request):
    
    response= self.get_response(request) 
    if request.encoding == None:
        request.encoding= "encoding"
    else:
        request.encoding  

    if request.content_params == {}:
        request.content_params= "empty"
    else:
        request.content_params    

    body= str(request.body)  
    content= str(response.content)  

    header= str(response.headers)
    

    api_request = {
                
                
                    "url_scheme": request.scheme,
                    "request_path": request.path,
                    "request_path_information" : request.path_info,
                    "request_method": request.method,
                    "request_cookies": request.COOKIES,
                    "request_encoding": request.encoding,
                    "request_content_type": request.content_type,
                    "request_content_params": request.content_params,
                    "host": request.get_host(),
                    "request_body": body,
                    'response_code': response.status_code,
                    "response_headers": header,
                    "response_content": content,
                    'response_length_byte': len(content)
                        
    }       
    
    api(api_request)        
    return response

----------then make an python core api.py (Package)----------------------- from dapycorelogger.server_info import Server import asyncio import httpx import aiohttp

server1= Server()

url= "http://stage.linqer.in/"

api_response= server1.get_info()

def api(data): async def call_api(url): print("test3") api_response.update(data)

    await asyncio.sleep(10)
    
    async with httpx.AsyncClient() as client:
        response= await client.get(url, params=api_response)
        print("test2")
        
        print(response.url)

asyncio.new_event_loop().run_until_complete(call_api(url))

---------------After that make a Python project for print Views.py-------- async def index(request): return HttpResponse("Hello, world. You're at the polls index.")

-------Then add a middleware in setting.py----  
        

I want to call my django views.py before my async api.py calls..Asynchonous means a unit of work run separately from the primary application.

0

There are 0 best solutions below