Пример #1
0
 def post(self, request, format=None):
     serializer = UserSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Пример #2
0
def cart_remove(request):
    cart = Cart(request)
    product = get_object_or_404(Product, id=request.data['product_id'])
    cart.remove(product)
    return Response({**request.session, 'total': cart.get_total_price()})
Пример #3
0
 def add_contact_to_list(self, email: str, list_id: str) -> Response:
     response = Response()
     response.status_code = 201
     return response
Пример #4
0
def _fake_resp(status_code):
    resp = Response()
    resp.status_code = status_code
    return resp
Пример #5
0
def cart_detail(request):
    cart = Cart(request)
    return Response({**request.session, 'total': cart.get_total_price()})
Пример #6
0
"""
Run these tests with tox -e test -- -k test_translation
"""
from pathlib import Path
from unittest import mock

from requests import Response

from dashboard.internet_nl_dashboard.logic.internet_nl_translations import (
    convert_vue_i18n_format, get_locale_content, load_as_po_file)

path = Path(__file__).parent

# Create the desired normal response, by simply forcing the correct properties to be present.
# This is probably not the way to do it, yet i found the other methods be mostly obscure.
perfect_response = Response()
perfect_response._content = "yolo"


def file_get_contents(filepath):
    with open(filepath, 'r') as content_file:
        return content_file.read()


def mocked_requests_get(*args, **kwargs):
    class MockResponse:
        def __init__(self, content, status_code):
            self.content = content
            self.status_code = status_code

    if args[0] == 'https://raw.githubusercontent.com/NLnetLabs/Internet.nl/master/translations/nl/main.po':
Пример #7
0
 def error(code: int, message: str) -> HTTPError:
     response = Response()
     response.status_code = code
     response.reason = message
     return HTTPError(f'Exception {code} {message}', response=response)
Пример #8
0
    def send(self, request, **kwargs):
        """ Wraps a file, described in request, in a Response object.

            :param request: The PreparedRequest` being "sent".
            :returns: a Response object containing the file
        """

        # Check that the method makes sense. Only support GET
        if request.method not in ("GET", "HEAD"):
            raise ValueError("Invalid request method %s" % request.method)

        # Parse the URL
        url_parts = urlparse(request.url)

        # Make the Windows URLs slightly nicer
        if is_win32 and url_parts.netloc.endswith(":"):
            url_parts = url_parts._replace(path="/" + url_parts.netloc +
                                           url_parts.path,
                                           netloc='')

        # Reject URLs with a hostname component
        if url_parts.netloc and url_parts.netloc not in ("localhost", ".",
                                                         "..", "-"):
            raise ValueError(
                "file: URLs with hostname components are not permitted")

        # If the path is relative update it to be absolute
        if url_parts.netloc in (".", ".."):
            pwd = os.path.abspath(url_parts.netloc).replace(os.sep, "/") + "/"
            if is_win32:
                # prefix the path with a / in Windows
                pwd = "/" + pwd
            url_parts = url_parts._replace(
                path=urljoin(pwd, url_parts.path.lstrip("/")))

        resp = Response()
        resp.url = request.url

        # Open the file, translate certain errors into HTTP responses
        # Use urllib's unquote to translate percent escapes into whatever
        # they actually need to be
        try:
            # If the netloc is - then read from stdin
            if url_parts.netloc == "-":
                if is_py3:
                    resp.raw = sys.stdin.buffer
                else:
                    resp.raw = sys.stdin
                # make a fake response URL, the current directory
                resp.url = "file://" + os.path.abspath(".").replace(
                    os.sep, "/") + "/"
            else:
                # Split the path on / (the URL directory separator) and decode any
                # % escapes in the parts
                path_parts = [unquote(p) for p in url_parts.path.split('/')]

                # Strip out the leading empty parts created from the leading /'s
                while path_parts and not path_parts[0]:
                    path_parts.pop(0)

                # If os.sep is in any of the parts, someone fed us some shenanigans.
                # Treat is like a missing file.
                if any(os.sep in p for p in path_parts):
                    raise IOError(errno.ENOENT, os.strerror(errno.ENOENT))

                # Look for a drive component. If one is present, store it separately
                # so that a directory separator can correctly be added to the real
                # path, and remove any empty path parts between the drive and the path.
                # Assume that a part ending with : or | (legacy) is a drive.
                if path_parts and (path_parts[0].endswith('|')
                                   or path_parts[0].endswith(':')):
                    path_drive = path_parts.pop(0)
                    if path_drive.endswith('|'):
                        path_drive = path_drive[:-1] + ':'

                    while path_parts and not path_parts[0]:
                        path_parts.pop(0)
                else:
                    path_drive = ''

                # Try to put the path back together
                # Join the drive back in, and stick os.sep in front of the path to
                # make it absolute.
                path = path_drive + os.sep + os.path.join(*path_parts)

                # Check if the drive assumptions above were correct. If path_drive
                # is set, and os.path.splitdrive does not return a drive, it wasn't
                # reall a drive. Put the path together again treating path_drive
                # as a normal path component.
                if path_drive and not os.path.splitdrive(path):
                    path = os.sep + os.path.join(path_drive, *path_parts)

                # Use io.open since we need to add a release_conn method, and
                # methods can't be added to file objects in python 2.
                resp.raw = io.open(path, "rb")
                resp.raw.release_conn = resp.raw.close
        except IOError as e:
            if e.errno == errno.EACCES:
                resp.status_code = codes.forbidden
            elif e.errno == errno.ENOENT:
                resp.status_code = codes.not_found
            else:
                resp.status_code = codes.bad_request

            # Wrap the error message in a file-like object
            # The error message will be localized, try to convert the string
            # representation of the exception into a byte stream
            resp_str = str(e).encode(locale.getpreferredencoding(False))
            resp.raw = BytesIO(resp_str)
            resp.headers['Content-Length'] = len(resp_str)

            # Add release_conn to the BytesIO object
            resp.raw.release_conn = resp.raw.close
        else:
            resp.status_code = codes.ok

            # If it's a regular file, set the Content-Length
            resp_stat = os.fstat(resp.raw.fileno())
            if stat.S_ISREG(resp_stat.st_mode):
                resp.headers['Content-Length'] = resp_stat.st_size

        return resp
Пример #9
0
def test_submit_batch_params(dag, mocker):
    http_conn_id_yarn = "http_conn_id_yarn"
    http_conn_id_spark = "http_conn_id_spark"
    http_conn_id_livy = "http_conn_id_livy"
    timeout_minutes = 4
    poll_period_sec = 5
    verify_in = "yarn"
    op = LivyBatchOperator(
        file="file",
        proxy_user="******",
        class_name="class_name",
        arguments=["arg1", "arg2"],
        jars=["jar1", "jar2"],
        py_files=["py_file1", "py_file2"],
        files=["file1", "file2"],
        driver_memory="driver_memory",
        driver_cores=1,
        executor_memory="executor_memory",
        executor_cores=2,
        num_executors=3,
        archives=["archive1", "archive2"],
        queue="queue",
        name="name",
        conf={"key1": "val1", "key2": 2},
        timeout_minutes=timeout_minutes,
        poll_period_sec=poll_period_sec,
        verify_in=verify_in,
        http_conn_id_livy=http_conn_id_livy,
        http_conn_id_spark=http_conn_id_spark,
        http_conn_id_yarn=http_conn_id_yarn,
        task_id="test_submit_batch_params",
        dag=dag,
    )
    mock_response = Response()
    mock_response._content = b'{"id": 1}'
    patched_hook = mocker.patch.object(HttpHook, "run", return_value=mock_response)

    op.submit_batch()

    assert op.timeout_minutes == timeout_minutes
    assert op.poll_period_sec == poll_period_sec
    assert op.verify_in == verify_in
    assert op.http_conn_id_livy == http_conn_id_livy
    assert op.http_conn_id_spark == http_conn_id_spark
    assert op.http_conn_id_yarn == http_conn_id_yarn
    expected_json = json.loads(
        """{
      "proxyUser": "******",
      "file": "file",
      "className": "class_name",
      "args": [
        "arg2",
        "arg1"
      ],
      "pyFiles": [
        "py_file1",
        "py_file2"
      ],
      "jars": [
        "jar1",
        "jar2"
      ],
      "files": [
        "file1",
        "file2"
      ],
      "driverMemory": "driver_memory",
      "driverCores": 1,
      "executorMemory": "executor_memory",
      "executorCores": 2,
      "numExecutors": 3,
      "archives": [
        "archive1",
        "archive2"
      ],
      "name": "name",
      "queue": "queue",
      "conf": {
        "key1": "val1",
        "key2": 2
      }
    }"""
    )
    actual_args, actual_kwargs = patched_hook._call_matcher(patched_hook.call_args)
    actual_json = find_json_in_args(actual_args, actual_kwargs)
    if actual_json is None:
        raise AssertionError(
            f"Can not find JSON in HttpHook args.\n"
            f"Args:\n{actual_args}\n"
            f"KWArgs (JSON should be under 'data' key):\n{actual_kwargs}"
        )
    else:
        diff = DeepDiff(actual_json, expected_json, ignore_order=True)
        if diff:
            print(f"\nDifference:\n{json.dumps(diff, indent=2)}")
        assert not diff
Пример #10
0
 def get(self, request, pk, format=None):
     snippet = self.get_object(pk)
     serializer = DisciplinesSerializer(snippet)
     return Response(serializer.data)
Пример #11
0
 def delete(self, request, pk, format=None):
     snippet = self.get_object(pk)
     snippet.delete()
     return Response(status=status.HTTP_204_NO_CONTENT)
Пример #12
0
 def get(self, request, format=None):
     snippets = Disciplines.objects.all()
     serializer = DisciplinesSerializer(snippets, many=True)
     return Response(serializer.data)
Пример #13
0
 def delete(self, request, pk, format=None):
     person = self.get_object(pk)
     person.delete()
     return Response(status=status.HTTP_204_NO_CONTENT)
Пример #14
0
 def get(self, request, pk, format=None):
     person = self.get_object(pk)
     serializer = UserSerializer(person)
     return Response(serializer.data)
 def get_response(self, content=b"", status_code=200):
     response = Response()
     response.status_code = status_code
     response._content = content
     response.encoding = "utf-8"
     return response
Пример #16
0
def _response(content):
    r = Response()
    r._content = json.dumps(content).encode()
    return r
Пример #17
0
 def get_response(code: str, status_code: int, content: bytes) -> Response:
     r = Response()
     r.code = code
     r.status_code = status_code
     r._content = content
     return r
Пример #18
0
 def _get_response():
     response = Response()
     response.status_code = 200
     response._content = b'Some content'
     return response
Пример #19
0
 def get(self, request):
     users = User.objects.all()
     serializer = UserSerializer(users, many=True)
     return Response(serializer.data, status=status.HTTP_200_OK)
Пример #20
0
 def raise_http_error():
     x = Response()
     x.status_code = 404
     x.reason = err
     raise HTTPError(err, response=x)
Пример #21
0
def build_response(reason=None, status_code=200, headers={}):
    response = Response()
    response.status_code = status_code
    response.headers = headers
    response.reason = reason
    return response
Пример #22
0
 def connection_error():
     x = Response()
     x.status_code = 500
     x.reason = err
     raise ConnectionError(err, response=x)
Пример #23
0
def get_session_id(request):
    cart = Cart(request)
    cart.save()
    return Response({'session_id': cart.session.session_key})
Пример #24
0
def create_fake_response(dict_response, status_code=200):
    response = Response()
    response.status_code = status_code
    response._content = bytes(json.dumps(dict_response).encode('utf-8'))
    return response
Пример #25
0
def cart_add(request):
    cart = Cart(request)
    product = get_object_or_404(Product, id=request.data['product_id'])
    cart.add(product=product, quantity=request.data['quantity'])
    return Response({**request.session, 'total': cart.get_total_price()})
Пример #26
0
def create_response(body, status, headers=None):
    res = Response()
    res.headers = headers
    res._content = body
    res.status_code = status
    return res
Пример #27
0
 def update_contact(self, email: str, *, birth_date: date,
                    department: str) -> Response:
     response = Response()
     response.status_code = 200
     return response
Пример #28
0
 def setUp(self):
     self.test_resp = Response()
     self.test_resp = mock.MagicMock(status_code=601,
                                     url='https://fake.url.com',
                                     headers='blah')
Пример #29
0
 def create_contact(self, email: str) -> Response:
     response = Response()
     response.status_code = 400
     return response
Пример #30
0
 def get(self, request, format=None):
     persons = Users.objects.all()
     serializer = UserSerializer(persons, many=True)
     return Response(serializer.data)