예제 #1
0
  def request(self, path, method='GET', params=None):
    if params is None: params = {}
    url = urljoin(ENDPOINT, path)

    headers = {
      'Accept'        : 'application/json',
      'Authorization' : 'AccessKey ' + self.access_key,
      'User-Agent'    : 'MessageBird/ApiClient/%s Python/%s' % (CLIENT_VERSION, PYTHON_VERSION),
      'Content-Type'  : 'application/json'
    }

    if method == 'GET':
      response = requests.get(url, verify=True, headers=headers, params=params)
    else:
      response = requests.post(url, verify=True, headers=headers, data=json.dumps(params))

    if response.status_code in self._supported_status_codes:
      json_response = response.json()
    else:
      response.raise_for_status()

    if 'errors' in json_response:
      raise(ErrorException([Error().load(e) for e in json_response['errors']]))

    return json_response
예제 #2
0
    def request(self, path, method='GET', params=None, type=REST_TYPE):
        """Builds a request, gets a response and decodes it."""
        response_text = self._get_http_client(type).request(path, method, params)
        if not response_text:
            return response_text

        response_json = json.loads(response_text)

        if 'errors' in response_json:
            raise (ErrorException([Error().load(e) for e in response_json['errors']]))

        return response_json
예제 #3
0
    def request_plain_text(self, path, method='GET', params=None, type=REST_TYPE):
        """Builds a request, gets a response and returns the body."""
        response_text = self._get_http_client(type).request(path, method, params)

        try:
            # Try to decode the response to JSON to see if the API returned any
            # errors.
            response_json = json.loads(response_text)

            if 'errors' in response_json:
                raise (ErrorException([Error().load(e) for e in response_json['errors']]))
        except ValueError:
            # Do nothing: json.loads throws if the input string is not valid JSON,
            # which is expected. We'll just return the response body below.
            pass

        return response_text