예제 #1
0
    def request(self,
                method,
                path,
                data=None,
                files=None,
                headers=None,
                **extras):
        headers = headers or {}

        if not path.startswith('/'):
            path = u'/{}'.format(path)

        self.last_response = self._request(
            method,
            u'{}://{}{}'.format(self.url.scheme, self.url.netloc, path),
            headers=headers,
            data=data,
            files=files,
            verify=self.verify,
            timeout=(self.timeout, self.read_timeout),
            **extras)

        if 400 <= self.last_response.status_code < 500:
            raise ClientException(None, self.last_response)

        if not 200 <= self.last_response.status_code < 400:
            raise ApiException(None, self.last_response)

        return self.last_response
예제 #2
0
    def delete(self, pk):
        """Deletes your form"""

        resp = self.conn.delete('{}/{}'.format(self.forms_ep, pk))
        if resp.status_code != 204:
            raise ClientException('Invalid api delete response: {}, {}'.format(
                resp.status_code, resp.reason))
예제 #3
0
    def test_http_exception_messeges(self):
        resp = MockResponse(404, 'Not Found', '{"detail": "Not found"}')

        msglist = [str(ApiException(None, resp)),
                   str(ClientException(None, resp)),
                   '404, Not Found']

        self.assertTrue(msglist[1:] == msglist[:-1],
                        u'HTTP exception messages fail to print as expected.')
예제 #4
0
    def export(self, pk, data_format=None):
        path = '{}/{}'.format(self.forms_ep, pk)
        if data_format is not None:
            if data_format not in ['json', 'xml', 'xls', 'csv']:
                raise ClientException(
                    'Invalid representation:- {}. Options are '
                    'json, csv, xml or xls'.format(data_format))
            path = '{}.{}'.format(path, data_format)

        return self.conn.get(path).text
예제 #5
0
파일: utils.py 프로젝트: jnordling/onapie
    def _request_with_body(self, method, url_path, file_path=None,
                           payload=None, headers=None, **extras):
        if method not in ['POST', 'PUT']:
            raise ClientException(u'method arg must either be a POST or PUT!')

        if file_path is not None:
            file_data = open(file_path, 'rb')
            fdata = file_data.read()
            file_data.close()

        return self.request(method, url_path, payload, headers, fdata,
                            **extras)
예제 #6
0
    def get(self, pk, representation=None, *tag_args):
        """Get Form Information or representation"""
        path = '{}/{}'.format(self.forms_ep, pk)

        if representation is not None:
            if representation not in ['json', 'xml', 'xls', 'csv']:
                raise ClientException(
                    'Invalid representation:- {}. Options are '
                    'json, csv, xml or xls'.format(representation))
            path = '{}/form.{}'.format(path, representation)

        if any(tag_args):
            tags = tag_args[0]
            for tag in tag_args[1:]:
                tags = '{},{}'.format(tags, tag)

            path = '{}?tags={}'.format(path, tags)

        if representation and representation != 'json':
            return self.conn.get(path).text
        else:
            return json.loads(self.conn.get(path).text)
예제 #7
0
파일: utils.py 프로젝트: jnordling/onapie
    def __init__(self, url, **kwargs):
        self.url = urlparse(url)

        if self.url.scheme not in ['http', 'https']:
            raise ClientException(
                u'{} protocol is not supported'.format(self.url.scheme))

        self.headers = {}
        self.timeout = kwargs.get('timeout', 20)
        self.read_timeout = kwargs.get('read_timeout', 180)
        self.verify = bool(kwargs.get('ssl_verify', True))
        retries = kwargs.get('max_retries', 5)

        self.session = requests.Session()

        self.session.mount("https://",
                           requests.adapters.HTTPAdapter(max_retries=retries))
        self.session.mount("http://", requests.adapters.HTTPAdapter(
            requests.adapters.HTTPAdapter(max_retries=retries)))

        self.user_agent = kwargs.get('user_agent', 'python-json2xlsclient')
        self.set_header('user_agent', self.user_agent)
예제 #8
0
    def create(self, xls_path=None, xls_url=None, owner=None):
        """Uploads an XLSForm

        .. attribute::xls_path

            Full path to the xlsform file. Mutually exclusive with xls_url

        .. attribute::xls_url

            A url to an xlsform. Mutually exclusive with xls_path

        .. attribute::owner

            Optional. username to the target account (Optional)
            TODO([email protected]): pass owner to the REST api?
        """
        if (xls_path is None) == (xls_url is None):
            raise ClientException(u'You must provide a path or a url '
                                  'for creation. The two args are '
                                  'mutually exclusive!')

        return json.loads(
            self.conn.post(self.forms_ep, xls_path, xls_url).text)
예제 #9
0
 def get_formdata(self, pk, export_format):
     # TODO: Implement when API behaves
     raise ClientException('This has not Implemented.')
예제 #10
0
 def test_plain_exception_messeges(self):
     msglist = [str(ApiException('Test Message')),
                str(ClientException('Test Message')),
                'Test Message']
     self.assertTrue(msglist[1:] == msglist[:-1],
                     u'Basic exception messages fail to print as expected.')