コード例 #1
0
def post(write_key, host=None, **kwargs):
    """Post the `kwargs` to the API"""
    log = logging.getLogger('segment')
    body = kwargs
    body["sentAt"] = datetime.utcnow().replace(tzinfo=tzutc()).isoformat()
    url = remove_trailing_slash(host or 'https://api.segment.io') + '/v1/batch'
    auth = HTTPBasicAuth(write_key, '')
    data = json.dumps(body, cls=DatetimeSerializer)
    headers = {
        'Content-Type': 'application/json',
        'User-Agent': 'analytics-python/' + VERSION
    }
    log.debug('making request: %s', data)
    res = _session.post(url, data=data, auth=auth, headers=headers, timeout=15)

    if res.status_code == 200:
        log.debug('data uploaded successfully')
        return res

    try:
        payload = res.json()
        log.debug('received response: %s', payload)
        raise APIError(res.status_code, payload['code'], payload['message'])
    except ValueError:
        raise APIError(res.status_code, 'unknown', res.text)
コード例 #2
0
ファイル: request.py プロジェクト: tcostam/analytics-python
def post(write_key, host=None, gzip=False, **kwargs):
    """Post the `kwargs` to the API"""
    log = logging.getLogger('segment')
    body = kwargs
    body["sentAt"] = datetime.utcnow().replace(tzinfo=tzutc()).isoformat()
    url = remove_trailing_slash(host or 'https://api.segment.io') + '/v1/batch'
    auth = HTTPBasicAuth(write_key, '')
    data = json.dumps(body, cls=DatetimeSerializer)
    log.debug('making request: %s', data)
    headers = {
        'Content-Type': 'application/json',
        'User-Agent': 'analytics-python/' + VERSION
    }
    if gzip:
        headers['Content-Encoding'] = 'gzip'
        buf = BytesIO()
        with GzipFile(fileobj=buf, mode='w') as gz:
            # 'data' was produced by json.dumps(), whose default encoding is utf-8.
            gz.write(data.encode('utf-8'))
        data = buf.getvalue()

    res = _session.post(url, data=data, auth=auth, headers=headers, timeout=15)

    if res.status_code == 200:
        log.debug('data uploaded successfully')
        return res

    try:
        payload = res.json()
        log.debug('received response: %s', payload)
        raise APIError(res.status_code, payload['code'], payload['message'])
    except ValueError:
        raise APIError(res.status_code, 'unknown', res.text)
コード例 #3
0
 def test_remove_slash(self):
     self.assertEqual('http://segment.io',
                      utils.remove_trailing_slash('http://segment.io/'))
     self.assertEqual('http://segment.io',
                      utils.remove_trailing_slash('http://segment.io'))
コード例 #4
0
 def test_remove_slash(self):
     self.assertEqual('http://astronomer.io',
                      utils.remove_trailing_slash('http://astronomer.io/'))
     self.assertEqual('http://astronomer.io',
                      utils.remove_trailing_slash('http://astronomer.io'))
コード例 #5
0
ファイル: utils.py プロジェクト: segmentio/analytics-python
 def test_remove_slash(self):
     self.assertEqual('http://segment.io', utils.remove_trailing_slash('http://segment.io/'))
     self.assertEqual('http://segment.io', utils.remove_trailing_slash('http://segment.io'))