def do_request(module, url, params, headers=None): data = urlencode(params) if headers is None: headers = dict() headers = dict(headers, **{ 'User-Agent': 'Ansible/typetalk module', }) r, info = fetch_url(module, url, data=data, headers=headers) if info['status'] != 200: exc = ConnectionError(info['msg']) exc.code = info['status'] raise exc return r
def do_request(module, url, params, headers=None): data = urllib.urlencode(params) if headers is None: headers = dict() headers = dict(headers, **{ 'User-Agent': 'Ansible/typetalk module', }) r, info = fetch_url(module, url, data=data, headers=headers) if info['status'] != 200: exc = ConnectionError(info['msg']) exc.code = info['status'] raise exc return r
def test_request_raises_on_connection_error(self, open_url_mock): open_url_mock.side_effect = ConnectionError('Unknown connection error') client = ManifoldApiClient('token-123') with self.assertRaises(ApiError) as context: client.request('test', 'endpoint') self.assertEqual('Error connecting to https://api.test.manifold.co/v1/endpoint: Unknown connection error', str(context.exception))
def test_fetch_url_connectionerror(open_url_mock, fake_ansible_module): open_url_mock.side_effect = ConnectionError('TESTS') with pytest.raises(FailJson) as excinfo: fetch_url(fake_ansible_module, 'http://ansible.com/') assert excinfo.value.kwargs['msg'] == 'TESTS' open_url_mock.side_effect = ValueError('TESTS') with pytest.raises(FailJson) as excinfo: fetch_url(fake_ansible_module, 'http://ansible.com/') assert excinfo.value.kwargs['msg'] == 'TESTS'
def __enter__(self): """Creates sessions by passing it to header""" if self.req_session: payload = {'UserName': self.username, 'Password': self.password} path = SESSION_RESOURCE_COLLECTION["SESSION"] resp = self.invoke_request('POST', path, data=payload) if resp and resp.success: self.session_id = resp.json_data.get("Id") self._headers["X-Auth-Token"] = resp.headers.get( 'X-Auth-Token') else: msg = "Could not create the session" raise ConnectionError(msg) return self