Esempio n. 1
0
def send_request(action: str,
                 timeout: Optional[float] = None,
                 args: Optional[dict] = None,
                 parse_json: bool = True,
                 authenticate: bool = True,
                 **kwargs):
    if not timeout:
        timeout = request_timeout
    if not args:
        args = {}

    auth = (test_user, test_pass) if authenticate else kwargs.pop('auth', ())
    set_timeout(seconds=timeout,
                on_timeout=on_timeout('Receiver response timed out'))
    response = requests.post('{}/execute'.format(base_url),
                             auth=auth,
                             json={
                                 'type': 'request',
                                 'action': action,
                                 'args': args,
                             },
                             **kwargs)

    clear_timeout()

    if parse_json:
        response = parse_response(response)
    return response
Esempio n. 2
0
def register_user(username: Optional[str] = None,
                  password: Optional[str] = None):
    if not username:
        username = test_user
        password = test_pass

    set_timeout(seconds=request_timeout,
                on_timeout=on_timeout('User registration response timed out'))
    response = requests.post(
        '{base_url}/register?redirect={base_url}/'.format(base_url=base_url),
        data={
            'username': username,
            'password': password,
            'confirm_password': password,
        })

    clear_timeout()
    return response
Esempio n. 3
0
    def send_request(self):
        set_timeout(seconds=self.timeout,
                    on_timeout=self.on_timeout('Receiver response timed out'))

        response = requests.post(
            u'http://localhost:8123/execute',
            json  = {
                'type': 'request',
                'target': Config.get('device_id'),
                'action': 'shell.exec',
                'args': { 'cmd':'echo ping' }
            }
        )

        clear_timeout()

        response = Message.build(response.json())
        self.assertTrue(isinstance(response, Response))
        self.assertEqual(response.output.strip(), 'ping')
        self.receiver.stop_app()
Esempio n. 4
0
    def _setup_response_handler(self, request, on_response, response_timeout):
        def _timeout_hndl():
            raise RuntimeError(
                'Timed out while waiting for a response from {}'.format(
                    request.target))

        req_ctx = {
            'request': request,
            'on_response': on_response,
            'response_timeout': response_timeout,
        }

        resp_backend = self.__class__(bus=self.bus,
                                      _req_ctx=req_ctx,
                                      **self._get_backend_config(),
                                      **self._kwargs)

        # Set the response timeout
        if response_timeout:
            set_timeout(seconds=response_timeout, on_timeout=_timeout_hndl)

        resp_backend.start()