コード例 #1
0
ファイル: alltests.py プロジェクト: mazandaran/rubber
    def test_data_to_json(self):
        """
        Tests that data_to_json serializes dicts / objects / models to json
        """

        from rubber.instanceutils import data_to_json

        # with a dict
        data = {'foo':'bar'}
        json_data = json.dumps(data)
        self.assertEquals(json_data, data_to_json(data))

        # with a string
        json_data = json.dumps(data)
        self.assertEquals(json_data, data_to_json(json_data))

        # try a class that implements to_indexed_json
        class Foo(object):
            def to_indexed_json(self):
                return json_data
        self.assertEquals(json_data, data_to_json(Foo()))

        # try a django model
        try:
            from django.db import models
            class TestModel(models.Model):
                foo = models.CharField(max_length=3)
            bar = TestModel(foo='bar')
            self.assertEquals(json_data, data_to_json(bar))
        except ImportError:
            pass
コード例 #2
0
ファイル: clientresp.py プロジェクト: kasunsp/pinalpha_mvp
def clientresp(data, **kwargs):
    """
    Deprecated endpoint, still used because it can parse data out of a plot.

    When we get around to forcing users to create grids and then create plots,
    we can finally get rid of this.

    :param (list) data: The data array from a figure.

    """
    creds = config.get_credentials()
    cfg = config.get_config()

    dumps_kwargs = {'sort_keys': True, 'cls': utils.PlotlyJSONEncoder}

    payload = {
        'platform': 'python', 'version': version.stable_semver(),
        'args': _json.dumps(data, **dumps_kwargs),
        'un': creds['username'], 'key': creds['api_key'], 'origin': 'plot',
        'kwargs': _json.dumps(kwargs, **dumps_kwargs)
    }

    url = '{plotly_domain}/clientresp'.format(**cfg)
    response = request('post', url, data=payload)

    # Old functionality, just keeping it around.
    parsed_content = response.json()
    if parsed_content.get('warning'):
        warnings.warn(parsed_content['warning'])
    if parsed_content.get('message'):
        print(parsed_content['message'])

    return response
コード例 #3
0
        def test_default_mpl_plot_generates_expected_html(self):
            # Generate matplotlib plot for tests
            fig = plt.figure()

            x = [10, 20, 30]
            y = [100, 200, 300]
            plt.plot(x, y)

            figure = plotly.tools.mpl_to_plotly(fig).to_dict()
            data = figure['data']

            layout = figure['layout']
            data_json = _json.dumps(data,
                                    cls=plotly.utils.PlotlyJSONEncoder,
                                    sort_keys=True)
            layout_json = _json.dumps(layout,
                                      cls=plotly.utils.PlotlyJSONEncoder,
                                      sort_keys=True)
            html = self._read_html(plotly.offline.plot_mpl(fig))

            # blank out uid before comparisons
            data_json = re.sub('"uid": "[^"]+"', '"uid": ""', data_json)
            html = re.sub('"uid": "[^"]+"', '"uid": ""', html)

            # just make sure a few of the parts are in here
            # like PlotlyOfflineTestCase(TestCase) in test_core
            self.assertTrue(data_json in html)  # data is in there
            self.assertTrue(layout_json in html)  # layout is in there too
            self.assertTrue(PLOTLYJS in html)  # and the source code
            # and it's an <html> doc
            self.assertTrue(
                html.startswith('<html>') and html.endswith('</html>'))
コード例 #4
0
class DataForm(Form):
    # Define the primary test selection dropdown
    test_choices = [(test_id, TEST_DEFINITIONS[test_id]["name"]) for test_id in TEST_DEFINITIONS]
    test_choices = sorted(test_choices, key=lambda x: x[0])
    test = SelectField(label="Test Suite:", choices=test_choices)

    # Determine how many sets of IP/Port/Version to display at most
    specs_per_test = [(test_id, TEST_DEFINITIONS[test_id]["specs"]) for test_id in TEST_DEFINITIONS]
    specs_per_test = sorted(specs_per_test, key=lambda x: x[0])
    max_endpoints = 0
    for spec in specs_per_test:
        if len(spec) > max_endpoints:
            max_endpoints = len(spec)
    endpoints = FieldList(FormField(EndpointForm, label=""), min_entries=max_endpoints)

    # Define the secondary test selection dropdown
    test_selection = NonValidatingMultipleSelectField(label="Test Selection:", choices=[("all", "all"),
                                                                                        ("auto", "auto")])

    # Hide test data in the web form for dynamic modification of behaviour
    test_data = {}
    for test_id in TEST_DEFINITIONS:
        test_data[test_id] = copy.deepcopy(TEST_DEFINITIONS[test_id])
        test_data[test_id].pop("class")
        test_data[test_id]["test_methods"] = enumerate_tests(TEST_DEFINITIONS[test_id]["class"])
        test_data[test_id]["test_descriptions"] = enumerate_tests(TEST_DEFINITIONS[test_id]["class"], describe=True)

    hidden_options = HiddenField(default=max_endpoints)
    hidden_tests = HiddenField(default=json.dumps(test_data))
    hidden_specs = HiddenField(default=json.dumps(CONFIG.SPECIFICATIONS))
コード例 #5
0
ファイル: test_utils.py プロジェクト: codybushnell/plotly.py
    def test_masked_constants_example():
        # example from: https://gist.github.com/tschaume/d123d56bf586276adb98
        data = {
            'esN': [0, 1, 2, 3],
            'ewe_is0': [-398.11901997, -398.11902774,
                        -398.11897111, -398.11882215],
            'ewe_is1': [-398.11793027, -398.11792966, -398.11786308, None],
            'ewe_is2': [-398.11397008, -398.11396421, None, None]
        }
        df = pd.DataFrame.from_dict(data)

        plotopts = {'x': 'esN'}
        fig, ax = plt.subplots(1, 1)
        df.plot(ax=ax, **plotopts)

        renderer = PlotlyRenderer()
        Exporter(renderer).run(fig)

        _json.dumps(renderer.plotly_fig, cls=utils.PlotlyJSONEncoder)

        jy = _json.dumps(renderer.plotly_fig['data'][1]['y'],
                         cls=utils.PlotlyJSONEncoder)
        print(jy)
        array = _json.loads(jy)
        assert(array == [-398.11793027, -398.11792966, -398.11786308, None])
コード例 #6
0
    def test_data_to_json(self):
        """
        Tests that data_to_json serializes dicts / objects / models to json
        """

        from rubber.instanceutils import data_to_json

        # with a dict
        data = {'foo':'bar'}
        json_data = json.dumps(data)
        self.assertEquals(json_data, data_to_json(data))

        # with a string
        json_data = json.dumps(data)
        self.assertEquals(json_data, data_to_json(json_data))

        # try a class that implements to_indexed_json
        class Foo(object):
            def to_indexed_json(self):
                return json_data
        self.assertEquals(json_data, data_to_json(Foo()))

        # try a django model
        try:
            from django.db import models
            class TestModel(models.Model):
                foo = models.CharField(max_length=3)
            bar = TestModel(foo='bar')
            self.assertEquals(json_data, data_to_json(bar))
        except ImportError:
            pass
コード例 #7
0
    def test_masked_constants_example():
        # example from: https://gist.github.com/tschaume/d123d56bf586276adb98
        data = {
            'esN': [0, 1, 2, 3],
            'ewe_is0':
            [-398.11901997, -398.11902774, -398.11897111, -398.11882215],
            'ewe_is1': [-398.11793027, -398.11792966, -398.11786308, None],
            'ewe_is2': [-398.11397008, -398.11396421, None, None]
        }
        df = pd.DataFrame.from_dict(data)

        plotopts = {'x': 'esN'}
        fig, ax = plt.subplots(1, 1)
        df.plot(ax=ax, **plotopts)

        renderer = PlotlyRenderer()
        Exporter(renderer).run(fig)

        _json.dumps(renderer.plotly_fig, cls=utils.PlotlyJSONEncoder)

        jy = _json.dumps(renderer.plotly_fig['data'][1]['y'],
                         cls=utils.PlotlyJSONEncoder)
        print(jy)
        array = _json.loads(jy)
        assert (array == [-398.11793027, -398.11792966, -398.11786308, None])
コード例 #8
0
ファイル: test_utils.py プロジェクト: codybushnell/plotly.py
def test_datetime_json_encoding():
    j1 = _json.dumps(dt_list, cls=utils.PlotlyJSONEncoder)
    assert(j1 == '["2014-01-05", '
                 '"2014-01-05 01:01:01", '
                 '"2014-01-05 01:01:01.000001"]')
    j2 = _json.dumps({"x": dt_list}, cls=utils.PlotlyJSONEncoder)
    assert(j2 == '{"x": ["2014-01-05", '
                 '"2014-01-05 01:01:01", '
                 '"2014-01-05 01:01:01.000001"]}')
コード例 #9
0
def test_datetime_json_encoding():
    j1 = _json.dumps(dt_list, cls=utils.PlotlyJSONEncoder)
    assert (j1 == '["2014-01-05", '
            '"2014-01-05 01:01:01", '
            '"2014-01-05 01:01:01.000001"]')
    j2 = _json.dumps({"x": dt_list}, cls=utils.PlotlyJSONEncoder)
    assert (j2 == '{"x": ["2014-01-05", '
            '"2014-01-05 01:01:01", '
            '"2014-01-05 01:01:01.000001"]}')
コード例 #10
0
ファイル: test_grids.py プロジェクト: codybushnell/plotly.py
 def test_col_create(self):
     cols = [
         {'name': 'foo', 'data': [1, 2, 3]},
         {'name': 'bar', 'data': ['b', 'a', 'r']},
     ]
     body = {'cols': _json.dumps(cols, sort_keys=True)}
     grids.col_create('hodor:88', body)
     assert self.request_mock.call_count == 1
     args, kwargs = self.request_mock.call_args
     method, url = args
     self.assertEqual(method, 'post')
     self.assertEqual(
         url, '{}/v2/grids/hodor:88/col'.format(self.plotly_api_domain)
     )
     self.assertEqual(kwargs['data'], _json.dumps(body, sort_keys=True))
コード例 #11
0
ファイル: github.py プロジェクト: huntzhan/img2url
def _to_binary(body):
    from requests.compat import json as complexjson
    data = complexjson.dumps(body)
    if not isinstance(data, bytes):
        return data.encode('utf-8')
    else:
        return data
コード例 #12
0
    def create(cls,
               name,
               remotefs,
               server,
               num_executors=2,
               node_description=None,
               labels=None,
               exclusive=False,
               launcher=NodeLaunchMethod.COMMAND,
               launcher_params={}):
        '''
        :param name: name of node to create, ``str``
        :param remotefs: Remote root directory, ``str``
        :param num_executors: number of executors for node, ``int``
        :param node_description: Description of node, ``str``
        :param labels: Labels to associate with node, ``str``
        :param exclusive: Use this node for tied jobs only, ``bool``
        :param launcher: Slave launch method, ``NodeLaunchMethod|str``
        :param launcher_params: Additional launcher parameters, ``dict``
        '''
        node = cls(name, server)
        if node.exists:
            raise JenkinsError('node "%s" already exists' % name)

        mode = 'EXCLUSIVE' if exclusive else 'NORMAL'
        launcher_params['stapler-class'] = launcher

        # Note that CommandLauncher class removed from Jenkins core in 2.86.
        # It is now available in the command-launcher plugin.

        inner_params = {
            'name': name,
            'nodeDescription': node_description,
            'numExecutors': num_executors,
            'remoteFS': remotefs,
            'labelString': labels,
            'mode': mode,
            'type': 'hudson.slaves.DumbSlave$DescriptorImpl',
            'retentionStrategy': {
                'stapler-class': 'hudson.slaves.RetentionStrategy$Always'
            },
            'nodeProperties': {
                'stapler-class-bag': 'true'
            },
            'launcher': launcher_params
        }

        params = {
            'name': name,
            'type': 'hudson.slaves.DumbSlave$DescriptorImpl',
            'json': json.dumps(inner_params)
        }

        res = server.post('computer/doCreateItem', params=params, throw=False)

        if not res or res.status_code != 200:
            print(res.text)
            raise JenkinsError('create "%s" failed' % name)
        else:
            res.raise_for_status()
コード例 #13
0
    def _request(self, verb, path="/", data=None):
        url = "%s%s" % (API_URL, path)

        if data is not None:
            data = json.dumps(data)

        response = self._session.request(verb,
                                         url,
                                         allow_redirects=False,
                                         data=data)
        if response.status_code == 200:
            return response.json()

        if response.status_code == 401:
            raise AuthorizationError(response)

        if response.status_code != 307:
            raise APIError(response)

        redirect_url = response.headers['Location']
        response = self._session.request(verb,
                                         redirect_url,
                                         allow_redirects=False,
                                         data=data)
        # TODO check for 429 status code for too frequent access.
        # see https://developers.nest.com/documentation/cloud/data-rate-limits
        if 400 <= response.status_code < 600:
            raise APIError(response)

        return response.json()
コード例 #14
0
ファイル: graph_widget.py プロジェクト: fcolmenero/plotly.py
 def _handle_outgoing_message(self, message):
     if self._graphId == '':
         self._clientMessages.append(message)
     else:
         message['graphId'] = self._graphId
         message['uid'] = str(uuid.uuid4())
         self._message = _json.dumps(message, cls=utils.PlotlyJSONEncoder)
コード例 #15
0
ファイル: graph_widget.py プロジェクト: kasunsp/pinalpha_mvp
 def _handle_outgoing_message(self, message):
     if self._graphId == '':
         self._clientMessages.append(message)
     else:
         message['graphId'] = self._graphId
         message['uid'] = str(uuid.uuid4())
         self._message = _json.dumps(message, cls=utils.PlotlyJSONEncoder)
コード例 #16
0
 def test_validate_json_content_dict_error_empty(self):
     content = self.to_bytes(_json.dumps({'error': ''}))
     response = self.get_response(content=content, status_code=200)
     try:
         utils.validate_response(response)
     except PlotlyRequestError:
         self.fail('Expected this not to raise!')
コード例 #17
0
ファイル: stat_plugin.py プロジェクト: 1Joy1/HTTPAceProxy
    def handle(self, connection, headers_only=False):
        self.params = parse_qs(connection.query)
        path_file_ext = ''.join(connection.path.split('.')[-1:])

        if headers_only:
            self.SendResponse(200, 'json', '', connection)
            return

        if connection.path == '/stat':
            if self.params.get('action', [''])[0] == 'get_status':
                self.SendResponse(
                    200, 'json',
                    json.dumps(self.getStatusJSON(),
                               ensure_ascii=False).encode('utf-8'), connection)
            else:
                try:
                    self.SendResponse(200, 'html',
                                      self.getReqFileContent('index.html'),
                                      connection)
                except:
                    connection.dieWithError(404, 'Not Found')
                    return

        elif path_file_ext:
            try:
                self.SendResponse(
                    200, path_file_ext,
                    self.getReqFileContent(
                        connection.path.replace(r'/stat', '')), connection)
            except:
                connection.dieWithError(404, 'Not Found')
                return
        else:
            connection.dieWithError(404, 'Not Found')
コード例 #18
0
ファイル: utils.py プロジェクト: fu/plotly.py
    def encode(self, o):
        """
        Load and then dump the result using parse_constant kwarg

        Note that setting invalid separators will cause a failure at this step.

        """

        # this will raise errors in a normal-expected way
        encoded_o = super(PlotlyJSONEncoder, self).encode(o)

        # now:
        #    1. `loads` to switch Infinity, -Infinity, NaN to None
        #    2. `dumps` again so you get 'null' instead of extended JSON
        try:
            new_o = _json.loads(encoded_o,
                                parse_constant=self.coerce_to_strict)
        except ValueError:

            # invalid separators will fail here. raise a helpful exception
            raise ValueError(
                "Encoding into strict JSON failed. Did you set the separators "
                "valid JSON separators?"
            )
        else:
            return _json.dumps(new_o, sort_keys=self.sort_keys,
                               indent=self.indent,
                               separators=(self.item_separator,
                                           self.key_separator))
コード例 #19
0
ファイル: test_utils.py プロジェクト: fcolmenero/plotly.py
 def test_validate_json_content_dict_error_empty(self):
     content = self.to_bytes(_json.dumps({'error': ''}))
     response = self.get_response(content=content, status_code=200)
     try:
         utils.validate_response(response)
     except PlotlyRequestError:
         self.fail('Expected this not to raise!')
コード例 #20
0
ファイル: client.py プロジェクト: adam139/WeRobot
    def upload_permanent_video(self, title, introduction, video):
        """
        上传永久视频。

        :param title: 视频素材的标题
        :param introduction: 视频素材的描述
        :param video: 要上传的视频,一个 File-object
        :return: requests 的 Response 实例
        """
        return requests.post(
            url="https://api.weixin.qq.com/cgi-bin/material/add_material",
            params={
                "access_token": self.token,
                "type": "video"
            },
            data={
                "description": _json.dumps({
                    "title": title,
                    "introduction": introduction
                }, ensure_ascii=False).encode("utf-8")
            },
            files={
                "media": video
            }
        )
コード例 #21
0
ファイル: nest.py プロジェクト: bsmithyman/nest_thermostat
    def _set(self, what, data):
        url = '%s/v2/put/%s.%s' % (self._nest_api.urls['transport_url'],
                                   what, self._serial)
        response = self._nest_api._session.post(url, data=json.dumps(data))
        response.raise_for_status()

        self._nest_api._bust_cache()
コード例 #22
0
ファイル: aliyun_request.py プロジェクト: j380322128/lancelot
    def request(self, method, url, **kwargs):
        if isinstance(kwargs.get("data", ""), dict):
            body = _json.dumps(kwargs["data"], ensure_ascii=False)
            body = body.encode('utf8')
            kwargs["data"] = body

        params = kwargs.get("params")
        if not params:
            params = {}
        # 公共参数
        params["Format"] = "JSON"
        params["Version"] = self.version
        params["AccessKeyId"] = settings.ALIYUN_ACCESS_KEY
        params["SignatureMethod"] = "HMAC-SHA1"
        params["Timestamp"] = utc_nowtime()
        params["SignatureVersion"] = "1.0"
        params["SignatureNonce"] = nowtime()

        # 代签名字符串
        sign_string = get_signature_string(params)
        # 签名字符串
        signature = aliyun_sgin(settings.ALIYUN_SECRET, sign_string)
        params["Signature"] = signature

        kwargs['params'] = params

        r = requests.request(method=method, url=url, verify=False, **kwargs)

        json = r.json()
        return self.get_check(json)
コード例 #23
0
ファイル: client.py プロジェクト: ajparsons/tinify-python
    def request(self, method, url, body=None, header={}):
        url = url if url.lower().startswith('https://') else self.API_ENDPOINT + url
        params = {}
        if isinstance(body, dict):
            if body:
                # Dump without whitespace.
                params['headers'] = {'Content-Type': 'application/json'}
                params['data'] = json.dumps(body, separators=(',', ':'))
        elif body:
            params['data'] = body

        try:
            response = self.session.request(method, url, **params)
        except requests.exceptions.Timeout as err:
            raise ConnectionError('Timeout while connecting', cause=err)
        except Exception as err:
            raise ConnectionError('Error while connecting: {0}'.format(err), cause=err)

        count = response.headers.get('compression-count')
        if count:
            tinify.compression_count = int(count)

        if response.ok:
            return response
        else:
            details = None
            try:
                details = response.json()
            except Exception as err:
                details = {'message': 'Error while parsing response: {0}'.format(err), 'error': 'ParseError'}
            raise Error.create(details.get('message'), details.get('error'), response.status_code)
コード例 #24
0
ファイル: test_grids.py プロジェクト: fcolmenero/plotly.py
 def test_col_update(self):
     cols = [
         {'name': 'foo', 'data': [1, 2, 3]},
         {'name': 'bar', 'data': ['b', 'a', 'r']},
     ]
     body = {'cols': _json.dumps(cols, sort_keys=True)}
     grids.col_update('hodor:88', 'aaaaaa,bbbbbb', body)
     self.request_mock.assert_called_once()
     args, kwargs = self.request_mock.call_args
     method, url = args
     self.assertEqual(method, 'put')
     self.assertEqual(
         url, '{}/v2/grids/hodor:88/col'.format(self.plotly_api_domain)
     )
     self.assertEqual(kwargs['params'], {'uid': 'aaaaaa,bbbbbb'})
     self.assertEqual(kwargs['data'], _json.dumps(body, sort_keys=True))
コード例 #25
0
    def handle(self, connection):
        path_file_ext = connection.path[connection.path.rfind('.') + 1:]
        if connection.splittedpath[
                1] == 'stat' and connection.splittedpath.__len__() == 2:
            if query_get(connection.query, 'action') == 'get_status':
                Stat.SendResponse(
                    200, 'json',
                    ensure_binary(
                        json.dumps(self.getStatusJSON(), ensure_ascii=True)),
                    connection)
            else:
                try:
                    Stat.SendResponse(200, 'html',
                                      Stat.getReqFileContent('index.html'),
                                      connection)
                except:
                    connection.send_error(404, 'Not Found')

        elif path_file_ext:
            try:
                Stat.SendResponse(
                    200, path_file_ext,
                    Stat.getReqFileContent(
                        connection.path.replace(r'/stat', '')), connection)
            except:
                connection.send_error(404, 'Not Found')
        else:
            connection.send_error(404, 'Not Found')
コード例 #26
0
    def encode(self, o):
        """
        Load and then dump the result using parse_constant kwarg

        Note that setting invalid separators will cause a failure at this step.

        """

        # this will raise errors in a normal-expected way
        encoded_o = super(PlotlyJSONEncoder, self).encode(o)

        # now:
        #    1. `loads` to switch Infinity, -Infinity, NaN to None
        #    2. `dumps` again so you get 'null' instead of extended JSON
        try:
            new_o = _json.loads(encoded_o,
                                parse_constant=self.coerce_to_strict)
        except ValueError:

            # invalid separators will fail here. raise a helpful exception
            raise ValueError(
                "Encoding into strict JSON failed. Did you set the separators "
                "valid JSON separators?"
            )
        else:
            return _json.dumps(new_o, sort_keys=self.sort_keys,
                               indent=self.indent,
                               separators=(self.item_separator,
                                           self.key_separator))
コード例 #27
0
    def _set(self, what, data):
        url = '%s/v2/put/%s.%s' % (self._nest_api.urls['transport_url'], what,
                                   self._serial)
        response = self._nest_api._session.post(url, data=json.dumps(data))
        response.raise_for_status()

        self._nest_api._bust_cache()
コード例 #28
0
def _to_binary(body):
    from requests.compat import json as complexjson
    data = complexjson.dumps(body)
    if not isinstance(data, bytes):
        return data.encode('utf-8')
    else:
        return data
コード例 #29
0
ファイル: test_offline.py プロジェクト: xhesc/plotly.py
    def test_default_plot_generates_expected_html(self):
        data_json = _json.dumps(fig['data'],
                                cls=plotly.utils.PlotlyJSONEncoder)
        layout_json = _json.dumps(fig['layout'],
                                  cls=plotly.utils.PlotlyJSONEncoder)

        html = self._read_html(plotly.offline.plot(fig))

        # I don't really want to test the entire script output, so
        # instead just make sure a few of the parts are in here?
        self.assertTrue('Plotly.newPlot' in html)  # plot command is in there
        self.assertTrue(data_json in html)  # data is in there
        self.assertTrue(layout_json in html)  # so is layout
        self.assertTrue(PLOTLYJS in html)  # and the source code
        # and it's an <html> doc
        self.assertTrue(html.startswith('<html>') and html.endswith('</html>'))
コード例 #30
0
    def request(self, method, api_endpoint, **kwargs):
        """Wrapper for underlying :class:`requests.Session`

        Handles generating full API URL, session reuse and auth, request defaults, and invalid response status codes

        Used throughout library as the core underlying request/response method for all interactions with server

        Args:
            method (str): Request method (get, post, put, etc.)
            api_endpoint (str): Portion of URL matching API endpoint route as listed in platform /docs help page
            **kwargs (dict): Remaining arguments passed through to actual request call

        Notes:
            All other provided kwargs are passed to underlying :meth:`requests.Session.request()` call

        Raises:
            swimlane.exceptions.SwimlaneHTTP400Error: On 400 responses with additional context about the exception
            requests.HTTPError: Any other 4xx/5xx HTTP responses

        Returns:
            requests.Response: Successful response instances

        Examples:

            Request and parse server settings endpoint response

            >>> server_settings = swimlane.request('get', 'settings').json()
        """
        while api_endpoint.startswith('/'):
            api_endpoint = api_endpoint[1:]

        # Ensure a timeout is set
        kwargs.setdefault('timeout', self._default_timeout)

        # Manually grab and dump json data to have full control over serialization
        # Emulate default requests behavior
        json_data = kwargs.pop('json', None)
        if json_data is not None:
            headers = CaseInsensitiveDict(kwargs.get('headers', {}))
            headers.setdefault('Content-Type', 'application/json')
            kwargs['headers'] = headers

            kwargs['data'] = json.dumps(json_data,
                                        sort_keys=True,
                                        separators=(',', ':'))

        response = self._session.request(
            method, urljoin(str(self.host) + self._api_root, api_endpoint),
            **kwargs)

        # Roll 400 errors up into SwimlaneHTTP400Errors with specific Swimlane error code support
        try:
            response.raise_for_status()
        except requests.HTTPError as error:
            if error.response.status_code == 400:
                raise SwimlaneHTTP400Error(error)
            else:
                raise error

        return response
コード例 #31
0
ファイル: nest.py プロジェクト: WisdomWolf/python-nest
    def _request(self, verb, path="/", data=None):
        url = "%s%s" % (API_URL, path)

        if data is not None:
            data = json.dumps(data)

        response = self._session.request(verb, url,
                                         allow_redirects=False,
                                         data=data)
        if response.status_code == 200:
            return response.json()

        if response.status_code == 401:
            raise AuthorizationError(response)

        if response.status_code != 307:
            raise APIError(response)

        redirect_url = response.headers['Location']
        response = self._session.request(verb, redirect_url,
                                         allow_redirects=False,
                                         data=data)
        # TODO check for 429 status code for too frequent access.
        # see https://developers.nest.com/documentation/cloud/data-rate-limits
        if 400 <= response.status_code < 600:
            raise APIError(response)

        return response.json()
コード例 #32
0
    def test_create(self):

        body = {
            "figure": {
                "data": [{
                    "y": [10, 10, 2, 20]
                }],
                "layout": {
                    "width": 700
                }
            },
            "width": 1000,
            "height": 500,
            "format": "png",
            "scale": 4,
            "encoded": False
        }

        images.create(body)
        assert self.request_mock.call_count == 1
        args, kwargs = self.request_mock.call_args
        method, url = args
        self.assertEqual(method, 'post')
        self.assertEqual(url, '{}/v2/images'.format(self.plotly_api_domain))
        self.assertEqual(kwargs['data'], _json.dumps(body, sort_keys=True))
コード例 #33
0
ファイル: client.py プロジェクト: zjdpf/WeRoBot
    def upload_permanent_video(self, title, introduction, video):
        """
        上传永久视频。

        :param title: 视频素材的标题
        :param introduction: 视频素材的描述
        :param video: 要上传的视频,一个 File-object
        :return: requests 的 Response 实例
        """
        return requests.post(
            url="https://api.weixin.qq.com/cgi-bin/material/add_material",
            params={
                "access_token": self.token,
                "type": "video"
            },
            data={
                "description": _json.dumps({
                    "title": title,
                    "introduction": introduction
                }, ensure_ascii=False).encode("utf-8")
            },
            files={
                "media": video
            }
        )
コード例 #34
0
 def _prep_request(self, body):
     """
     Encodes body as json.
     """
     try:
         return json.dumps(body)
     except (TypeError, json.JSONDecodeError), e:
         raise ElasticSearchError('Invalid JSON %r' % (body, ), e)
コード例 #35
0
ファイル: test_offline.py プロジェクト: fcolmenero/plotly.py
    def test_default_plot_generates_expected_html(self):
        data_json = _json.dumps(fig['data'],
                                cls=plotly.utils.PlotlyJSONEncoder)
        layout_json = _json.dumps(
            fig['layout'],
            cls=plotly.utils.PlotlyJSONEncoder)

        html = self._read_html(plotly.offline.plot(fig, auto_open=False))

        # I don't really want to test the entire script output, so
        # instead just make sure a few of the parts are in here?
        self.assertIn('Plotly.newPlot', html)  # plot command is in there
        self.assertIn(data_json, html)         # data is in there
        self.assertIn(layout_json, html)       # so is layout
        self.assertIn(PLOTLYJS, html)          # and the source code
        # and it's an <html> doc
        self.assertTrue(html.startswith('<html>') and html.endswith('</html>'))
コード例 #36
0
 def _prep_request(self, body):
     """
     Encodes body as json.
     """
     try:
         return json.dumps(body)
     except (TypeError, json.JSONDecodeError), e:
         raise ElasticSearchError('Invalid JSON %r' % (body,), e)
コード例 #37
0
def format_test_results(results, endpoints, format, args):
    formatted = None
    total_time = 0
    max_name_len = 0
    ignored_tests = []
    if "suite" in vars(args):
        ignored_tests = args.ignore
    for test_result in results["result"]:
        _check_test_result(test_result, results)
        total_time += test_result.elapsed_time
        max_name_len = max(max_name_len, len(test_result.name))
    if format == "json":
        formatted = {
            "suite": results["suite"],
            "timestamp": time.time(),
            "duration": total_time,
            "results": [],
            "config": _export_config(),
            "endpoints": endpoints
        }
        for test_result in results["result"]:
            formatted["results"].append({
                "name": test_result.name,
                "state": str(TestStates.DISABLED if test_result.name in ignored_tests else test_result.state),
                "detail": test_result.detail,
                "duration": test_result.elapsed_time
            })
        formatted = json.dumps(formatted, sort_keys=True, indent=4)
    elif format == "junit":
        test_cases = []
        for test_result in results["result"]:
            test_case = TestCase(test_result.name, classname=results["suite"],
                                 elapsed_sec=test_result.elapsed_time, timestamp=test_result.timestamp)
            if test_result.name in ignored_tests or test_result.state in [
                TestStates.DISABLED,
                TestStates.UNCLEAR,
                TestStates.MANUAL,
                TestStates.NA,
                TestStates.OPTIONAL
            ]:
                test_case.add_skipped_info(test_result.detail)
            elif test_result.state in [TestStates.WARNING, TestStates.FAIL]:
                test_case.add_failure_info(test_result.detail, failure_type=str(test_result.state))
            elif test_result.state != TestStates.PASS:
                test_case.add_error_info(test_result.detail, error_type=str(test_result.state))
            test_cases.append(test_case)
        formatted = TestSuite(results["def"]["name"] + ": " + ", ".join(results["urls"]), test_cases)
    elif format == "console":
        formatted = "\r\nPrinting test results for suite '{}' using API(s) '{}'\r\n" \
                    .format(results["suite"], ", ".join(results["urls"]))
        formatted += "----------------------------\r\n"
        for test_result in results["result"]:
            num_extra_dots = max_name_len - len(test_result.name)
            test_state = str(TestStates.DISABLED if test_result.name in ignored_tests else test_result.state)
            formatted += "{} ...{} {}\r\n".format(test_result.name, ("." * num_extra_dots), test_state)
        formatted += "----------------------------\r\n"
        formatted += "Ran {} tests in ".format(len(results["result"])) + "{0:.3f}s".format(total_time) + "\r\n"
    return formatted
 def __str__(self):
     max_chars = 10
     jdata = _json.dumps(self.data, cls=utils.PlotlyJSONEncoder)
     if len(jdata) > max_chars:
         data_string = jdata[:max_chars] + "...]"
     else:
         data_string = jdata
     string = '<name="{name}", data={data_string}, id={id}>'
     return string.format(name=self.name, data=data_string, id=self.id)
コード例 #39
0
ファイル: jenkins.py プロジェクト: gvalkov/jenkins-webapi
    def create(cls, name, remotefs, server,
               num_executors=2,
               node_description=None,
               labels=None,
               exclusive=False,
               launcher=NodeLaunchMethod.COMMAND,
               launcher_params={}):
        '''
        :param name: name of node to create, ``str``
        :param remotefs: Remote root directory, ``str``
        :param num_executors: number of executors for node, ``int``
        :param node_description: Description of node, ``str``
        :param labels: Labels to associate with node, ``str``
        :param exclusive: Use this node for tied jobs only, ``bool``
        :param launcher: Slave launch method, ``NodeLaunchMethod|str``
        :param launcher_params: Additional launcher parameters, ``dict``
        '''
        node = cls(name, server)
        if node.exists:
            raise JenkinsError('node "%s" already exists' % name)

        mode = 'EXCLUSIVE' if exclusive else 'NORMAL'
        launcher_params['stapler-class'] = launcher

        # Note that CommandLauncher class removed from Jenkins core in 2.86.
        # It is now available in the command-launcher plugin.

        inner_params = {
            'name': name,
            'nodeDescription': node_description,
            'numExecutors': num_executors,
            'remoteFS': remotefs,
            'labelString': labels,
            'mode': mode,
            'type': 'hudson.slaves.DumbSlave$DescriptorImpl',
            'retentionStrategy': {
                'stapler-class':
                'hudson.slaves.RetentionStrategy$Always'
            },
            'nodeProperties': {'stapler-class-bag': 'true'},
            'launcher': launcher_params
        }

        params = {
            'name': name,
            'type': 'hudson.slaves.DumbSlave$DescriptorImpl',
            'json': json.dumps(inner_params)
        }

        res = server.post('computer/doCreateItem', params=params, throw=False)

        if not res or res.status_code != 200:
            print(res.text)
            raise JenkinsError('create "%s" failed' % name)
        else:
            res.raise_for_status()
コード例 #40
0
ファイル: aliyun_request.py プロジェクト: j380322128/lancelot
    def request(self, method, url, **kwargs):
        if isinstance(kwargs.get("data", ""), dict):
            body = _json.dumps(kwargs["data"], ensure_ascii=False)
            body = body.encode('utf8')
            kwargs["data"] = body

        r = requests.request(method=method, url=url, verify=False, **kwargs)
        r.raise_for_status()
        json = r.json()
        return self.get_check(json)
コード例 #41
0
 def test_row(self):
     body = {'rows': [[1, 'A'], [2, 'B']]}
     grids.row('hodor:88', body)
     self.request_mock.assert_called_once()
     args, kwargs = self.request_mock.call_args
     method, url = args
     self.assertEqual(method, 'post')
     self.assertEqual(
         url, '{}/v2/grids/hodor:88/row'.format(self.plotly_api_domain))
     self.assertEqual(kwargs['data'], _json.dumps(body, sort_keys=True))
コード例 #42
0
def request(method, url, **kwargs):
    """
    Central place to make any api v2 api request.

    :param (str) method: The request method ('get', 'put', 'delete', ...).
    :param (str) url: The full api url to make the request to.
    :param kwargs: These are passed along (but possibly mutated) to requests.
    :return: (requests.Response) The response directly from requests.

    """
    kwargs['headers'] = dict(kwargs.get('headers', {}), **get_headers())

    # Change boolean params to lowercase strings. E.g., `True` --> `'true'`.
    # Just change the value so that requests handles query string creation.
    if isinstance(kwargs.get('params'), dict):
        kwargs['params'] = kwargs['params'].copy()
        for key in kwargs['params']:
            if isinstance(kwargs['params'][key], bool):
                kwargs['params'][key] = _json.dumps(kwargs['params'][key])

    # We have a special json encoding class for non-native objects.
    if kwargs.get('json') is not None:
        if kwargs.get('data'):
            raise exceptions.PlotlyError('Cannot supply data and json kwargs.')
        kwargs['data'] = _json.dumps(kwargs.pop('json'),
                                     sort_keys=True,
                                     cls=utils.PlotlyJSONEncoder)

    # The config file determines whether reuqests should *verify*.
    kwargs['verify'] = config.get_config()['plotly_ssl_verification']

    try:
        response = requests.request(method, url, **kwargs)
    except RequestException as e:
        # The message can be an exception. E.g., MaxRetryError.
        message = str(getattr(e, 'message', 'No message'))
        response = getattr(e, 'response', None)
        status_code = response.status_code if response else None
        content = response.content if response else 'No content'
        raise exceptions.PlotlyRequestError(message, status_code, content)
    validate_response(response)
    return response
コード例 #43
0
ファイル: test_utils.py プロジェクト: fcolmenero/plotly.py
 def test_validate_json_content_array(self):
     content = self.to_bytes(_json.dumps([1, 2, 3]))
     response = self.get_response(content=content, status_code=400)
     try:
         utils.validate_response(response)
     except PlotlyRequestError as e:
         self.assertEqual(e.message, to_native_utf8_string(content))
         self.assertEqual(e.status_code, 400)
         self.assertEqual(e.content, content)
     else:
         self.fail('Expected this to raise!')
コード例 #44
0
 def request(self, method, url, **kwargs):
     if "params" not in kwargs:
         kwargs["params"] = {"access_token": self.token}
     if isinstance(kwargs.get("data", ""), dict):
         body = _json.dumps(kwargs["data"], ensure_ascii=False)
         kwargs["data"] = body
     r = requests.request(method=method, url=url, **kwargs)
     r.raise_for_status()
     json = r.json()
     if check_error(json):
         return json
コード例 #45
0
ファイル: utils.py プロジェクト: fu/plotly.py
def save_json_dict(filename, json_dict):
    """Save json to file. Error if path DNE, not a dict, or invalid json."""
    if isinstance(json_dict, dict):
        # this will raise a TypeError if something goes wrong
        json_string = _json.dumps(json_dict, indent=4)
        lock.acquire()
        with open(filename, "w") as f:
            f.write(json_string)
        lock.release()
    else:
        raise TypeError("json_dict was not a dictionary. not saving.")
コード例 #46
0
 def test_validate_json_content_dict_one_error_ok(self):
     content = self.to_bytes(_json.dumps({'error': 'not ok!'}))
     response = self.get_response(content=content, status_code=200)
     try:
         utils.validate_response(response)
     except PlotlyRequestError as e:
         self.assertEqual(e.message, 'not ok!')
         self.assertEqual(e.status_code, 200)
         self.assertEqual(e.content, content)
     else:
         self.fail('Expected this to raise!')
コード例 #47
0
 def test_validate_json_content_dict_no_error(self):
     content = self.to_bytes(_json.dumps({'foo': 'bar'}))
     response = self.get_response(content=content, status_code=400)
     try:
         utils.validate_response(response)
     except PlotlyRequestError as e:
         self.assertEqual(e.message, to_native_utf8_string(content))
         self.assertEqual(e.status_code, 400)
         self.assertEqual(e.content, content)
     else:
         self.fail('Expected this to raise!')
コード例 #48
0
ファイル: test_utils.py プロジェクト: fcolmenero/plotly.py
 def test_validate_json_content_dict_one_error_ok(self):
     content = self.to_bytes(_json.dumps({'error': 'not ok!'}))
     response = self.get_response(content=content, status_code=200)
     try:
         utils.validate_response(response)
     except PlotlyRequestError as e:
         self.assertEqual(e.message, 'not ok!')
         self.assertEqual(e.status_code, 200)
         self.assertEqual(e.content, content)
     else:
         self.fail('Expected this to raise!')
コード例 #49
0
ファイル: test_grids.py プロジェクト: codybushnell/plotly.py
 def test_row(self):
     body = {'rows': [[1, 'A'], [2, 'B']]}
     grids.row('hodor:88', body)
     assert self.request_mock.call_count == 1
     args, kwargs = self.request_mock.call_args
     method, url = args
     self.assertEqual(method, 'post')
     self.assertEqual(
         url, '{}/v2/grids/hodor:88/row'.format(self.plotly_api_domain)
     )
     self.assertEqual(kwargs['data'], _json.dumps(body, sort_keys=True))
コード例 #50
0
def save_json_dict(filename, json_dict):
    """Save json to file. Error if path DNE, not a dict, or invalid json."""
    if isinstance(json_dict, dict):
        # this will raise a TypeError if something goes wrong
        json_string = _json.dumps(json_dict, indent=4)
        lock.acquire()
        with open(filename, "w") as f:
            f.write(json_string)
        lock.release()
    else:
        raise TypeError("json_dict was not a dictionary. not saving.")
コード例 #51
0
ファイル: utils.py プロジェクト: mode/plotly.py
def request(method, url, **kwargs):
    """
    Central place to make any api v2 api request.

    :param (str) method: The request method ('get', 'put', 'delete', ...).
    :param (str) url: The full api url to make the request to.
    :param kwargs: These are passed along (but possibly mutated) to requests.
    :return: (requests.Response) The response directly from requests.

    """
    kwargs['headers'] = dict(kwargs.get('headers', {}), **get_headers())

    # Change boolean params to lowercase strings. E.g., `True` --> `'true'`.
    # Just change the value so that requests handles query string creation.
    if isinstance(kwargs.get('params'), dict):
        kwargs['params'] = kwargs['params'].copy()
        for key in kwargs['params']:
            if isinstance(kwargs['params'][key], bool):
                kwargs['params'][key] = _json.dumps(kwargs['params'][key])

    # We have a special json encoding class for non-native objects.
    if kwargs.get('json') is not None:
        if kwargs.get('data'):
            raise exceptions.PlotlyError('Cannot supply data and json kwargs.')
        kwargs['data'] = _json.dumps(kwargs.pop('json'), sort_keys=True,
                                     cls=utils.PlotlyJSONEncoder)

    # The config file determines whether reuqests should *verify*.
    kwargs['verify'] = config.get_config()['plotly_ssl_verification']

    try:
        response = requests.request(method, url, **kwargs)
    except RequestException as e:
        # The message can be an exception. E.g., MaxRetryError.
        message = str(getattr(e, 'message', 'No message'))
        response = getattr(e, 'response', None)
        status_code = response.status_code if response else None
        content = response.content if response else 'No content'
        raise exceptions.PlotlyRequestError(message, status_code, content)
    validate_response(response)
    return response
コード例 #52
0
def data_to_json(data):

    # convert dicts
    if isinstance(data, dict):
        return json.dumps(data)

    # convert objects that have a to_indexed_json method
    if 'to_indexed_json' in dir(data):
        return data.to_indexed_json()

    # convert django models
    try:
        from django.db import models
        if isinstance(data, models.Model):
            from django.core import serializers
            from django.core.serializers.json import DjangoJSONEncoder
            return json.dumps(serializers.serialize("python", [data])[0]['fields'], cls=DjangoJSONEncoder)
    except ImportError:
        pass

    return data
コード例 #53
0
ファイル: test_utils.py プロジェクト: codybushnell/plotly.py
def test_figure_json_encoding():
    df = pd.DataFrame(columns=['col 1'], data=[1, 2, 3])
    s1 = Scatter3d(x=numeric_list, y=np_list, z=mixed_list)
    s2 = Scatter(x=df['col 1'])
    data = Data([s1, s2])
    figure = Figure(data=data)

    js1 = _json.dumps(s1, cls=utils.PlotlyJSONEncoder, sort_keys=True)
    js2 = _json.dumps(s2, cls=utils.PlotlyJSONEncoder, sort_keys=True)

    assert(js1 == '{"type": "scatter3d", "x": [1, 2, 3], '
                  '"y": [1, 2, 3, null, null, null, "2014-01-05"], '
                  '"z": [1, "A", "2014-01-05", '
                  '"2014-01-05 01:01:01", "2014-01-05 01:01:01.000001"]}')
    assert(js2 == '{"type": "scatter", "x": [1, 2, 3]}')

    # Test JSON encoding works
    _json.dumps(data, cls=utils.PlotlyJSONEncoder, sort_keys=True)
    _json.dumps(figure, cls=utils.PlotlyJSONEncoder, sort_keys=True)

    # Test data wasn't mutated
    np_array = np.array(
        [1, 2, 3, np.NaN, np.NAN, np.Inf, dt(2014, 1, 5)]
    )
    for k in range(len(np_array)):
        if k in [3, 4]:
            # check NaN
            assert np.isnan(np_list[k]) and np.isnan(np_array[k])
        else:
            # non-NaN
            assert np_list[k] == np_array[k]

    assert(set(data[0]['z']) ==
           set([1, 'A', dt(2014, 1, 5), dt(2014, 1, 5, 1, 1, 1),
                dt(2014, 1, 5, 1, 1, 1, 1)]))
コード例 #54
0
ファイル: test_utils.py プロジェクト: codybushnell/plotly.py
def test_pandas_json_encoding():
    j1 = _json.dumps(df['col 1'], cls=utils.PlotlyJSONEncoder)
    print(j1)
    print('\n')
    assert(j1 == '[1, 2, 3, "2014-01-05", null, null, null]')

    # Test that data wasn't mutated
    assert_series_equal(df['col 1'],
                        pd.Series([1, 2, 3, dt(2014, 1, 5),
                                   pd.NaT, np.NaN, np.Inf], name='col 1'))

    j2 = _json.dumps(df.index, cls=utils.PlotlyJSONEncoder)
    assert(j2 == '[0, 1, 2, 3, 4, 5, 6]')

    nat = [pd.NaT]
    j3 = _json.dumps(nat, cls=utils.PlotlyJSONEncoder)
    assert(j3 == '[null]')
    assert(nat[0] is pd.NaT)

    j4 = _json.dumps(rng, cls=utils.PlotlyJSONEncoder)
    assert(j4 == '["2011-01-01", "2011-01-01 01:00:00"]')

    j5 = _json.dumps(ts, cls=utils.PlotlyJSONEncoder)
    assert(j5 == '[1.5, 2.5]')
    assert_series_equal(ts, pd.Series([1.5, 2.5], index=rng))

    j6 = _json.dumps(ts.index, cls=utils.PlotlyJSONEncoder)
    assert(j6 == '["2011-01-01", "2011-01-01 01:00:00"]')
コード例 #55
0
        def test_default_mpl_plot_generates_expected_html(self):
            # Generate matplotlib plot for tests
            fig = plt.figure()

            x = [10, 20, 30]
            y = [100, 200, 300]
            plt.plot(x, y)

            figure = plotly.tools.mpl_to_plotly(fig)
            data = figure['data']
            layout = figure['layout']
            data_json = _json.dumps(data, cls=plotly.utils.PlotlyJSONEncoder)
            layout_json = _json.dumps(layout, cls=plotly.utils.PlotlyJSONEncoder)
            html = self._read_html(plotly.offline.plot_mpl(fig))

            # just make sure a few of the parts are in here
            # like PlotlyOfflineTestCase(TestCase) in test_core
            self.assertTrue(data_json.split('"uid":')[0] in html)  # data is in there
            self.assertTrue(layout_json in html)        # layout is in there too
            self.assertTrue(PLOTLYJS in html)         # and the source code
            # and it's an <html> doc
            self.assertTrue(html.startswith('<html>') and html.endswith('</html>'))
コード例 #56
0
ファイル: client.py プロジェクト: whtsky/WeRoBot
    def request(self, method, url, **kwargs):
        if "params" not in kwargs:
            kwargs["params"] = {"access_token": self.token}
        if isinstance(kwargs.get("data", ""), dict):
            body = _json.dumps(kwargs["data"], ensure_ascii=False)
            body = body.encode("utf8")
            kwargs["data"] = body

        r = requests.request(method=method, url=url, **kwargs)
        r.raise_for_status()
        json = r.json()
        if check_error(json):
            return json
コード例 #57
0
ファイル: test_utils.py プロジェクト: fcolmenero/plotly.py
    def test_validate_json_content_dict_one_error_bad(self):
        content = self.to_bytes(_json.dumps({'errors': [{}]}))
        response = self.get_response(content=content, status_code=400)
        try:
            utils.validate_response(response)
        except PlotlyRequestError as e:
            self.assertEqual(e.message, to_native_utf8_string(content))
            self.assertEqual(e.status_code, 400)
            self.assertEqual(e.content, content)
        else:
            self.fail('Expected this to raise!')

        content = self.to_bytes(_json.dumps({'errors': [{'message': ''}]}))
        response = self.get_response(content=content, status_code=400)
        try:
            utils.validate_response(response)
        except PlotlyRequestError as e:
            self.assertEqual(e.message, to_native_utf8_string(content))
            self.assertEqual(e.status_code, 400)
            self.assertEqual(e.content, content)
        else:
            self.fail('Expected this to raise!')
コード例 #58
0
ファイル: test_utils.py プロジェクト: fcolmenero/plotly.py
 def test_validate_json_content_dict_multiple_errors(self):
     content = self.to_bytes(_json.dumps({'errors': [
         {'message': 'not ok!'}, {'message': 'bad job...'}
     ]}))
     response = self.get_response(content=content, status_code=400)
     try:
         utils.validate_response(response)
     except PlotlyRequestError as e:
         self.assertEqual(e.message, 'not ok!\nbad job...')
         self.assertEqual(e.status_code, 400)
         self.assertEqual(e.content, content)
     else:
         self.fail('Expected this to raise!')