File size: 1,315 Bytes
ce4e319
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from curl_cffi import requests as curl_requests
import json

def custom_requests(url, method='GET', res_type='json', kwargs=None):
    """
    Make a custom HTTP request
    
    Args:
        url (str): The URL to make the request to
        method (str): HTTP method (GET, POST, PUT, DELETE)
        res_type (str): Response type (json or text)
        kwargs (dict): Additional arguments for the request (headers, body, etc.)
    
    Returns:
        dict/str: Response data based on res_type
    """
    try:
        # Parse kwargs if it's a string
        if isinstance(kwargs, str):
            kwargs = json.loads(kwargs)
        elif kwargs is None:
            kwargs = {}

        # Make the request
        response = curl_requests.request(
            method=method.upper(),
            url=url,
            **kwargs,
            impersonate='chrome101'
        )
        
        # Raise for bad status
        response.raise_for_status()
        
        # Return based on response type
        if res_type.lower() == 'json':
            return response.json()
        else:
            return response.text
            
    except curl_requests.exceptions.RequestException as e:
        return {"error": str(e)}
    except json.JSONDecodeError:
        return {"error": "Invalid JSON in response"}