<commit_before># -*- coding: utf-8 -*-

"""
    Module which provides functionality to make requests
    to the API from the path of RAML nodes.
"""

import requests
from ramlfications.raml import AVAILABLE_METHODS

from .utils import match_type
from .exceptions import UnsupportedHTTPMethodError, UnsupportedQueryParameter


def prepare_request(node):
    """
        Prepare request to node's API route

        :param Node node: the RAML node object
    """
    if node.resource.method not in AVAILABLE_METHODS:
        raise UnsupportedHTTPMethodError(node.resource.method)


    def request(**kwargs):
        """
            Make request to node's API route with the given keyword arguments
        """
        # validate given query parameters
        for key, value in kwargs.items():
            param = next((p for p in node.resource.query_params if p.name == key), None)
            if not param:
                raise UnsupportedQueryParameter(node.resource.path, key)

            if not match_type(value, param.type):
                raise TypeError("Resource Query Parameter has type '{0}' but expected type '{1}'".format(
                    value.__class__.__name__, param.type))

        response = requests.request(node.resource.method, node.path, params=kwargs)
        return response
    return request<commit_msg>request: Make requests absolute_uri instead of path<commit_after># -*- coding: utf-8 -*-

"""
    Module which provides functionality to make requests
    to the API from the path of RAML nodes.
"""

import requests
from ramlfications.raml import AVAILABLE_METHODS

from .utils import match_type
from .exceptions import UnsupportedHTTPMethodError, UnsupportedQueryParameter


def prepare_request(node):
    """
        Prepare request to node's API route

        :param Node node: the RAML node object
    """
    if node.resource.method not in AVAILABLE_METHODS:
        raise UnsupportedHTTPMethodError(node.resource.method)


    def request(**kwargs):
        """
            Make request to node's API route with the given keyword arguments
        """
        # validate given query parameters
        for key, value in kwargs.items():
            param = next((p for p in node.resource.query_params if p.name == key), None)
            if not param:
                raise UnsupportedQueryParameter(node.resource.path, key)

            if not match_type(value, param.type):
                raise TypeError("Resource Query Parameter has type '{0}' but expected type '{1}'".format(
                    value.__class__.__name__, param.type))

        response = requests.request(node.resource.method, node.absolute_uri, params=kwargs)
        return response
    return request
{'fraction_non_alphanumeric': 0.08204562178072111, 'fraction_numerical': 0.002207505518763797, 'mean_word_length': 2.6594885598923286, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3994794', 'n_tokens_mistral': 696, 'n_tokens_neox': 673, 'n_words': 243}