Beispiel #1
0
    def _CheckGitkitError(self, raw_response):
        """Raises error if API invocation failed.

    Args:
      raw_response: string, the http response.

    Raises:
      GitkitClientError: if the error code is 4xx.
      GitkitServerError: if the response if malformed.

    Returns:
      Successful response as dict.
    """
        try:
            response = simplejson.loads(raw_response)
            if 'error' not in response:
                return response
            else:
                error = response['error']
                if 'code' in error:
                    code = error['code']
                    if str(code).startswith('4'):
                        raise errors.GitkitClientError(error['message'])
                    else:
                        raise errors.GitkitServerError(error['message'])
        except simplejson.JSONDecodeError:
            pass
        raise errors.GitkitServerError('null error code from Gitkit server')
  def FromDictionary(cls, dictionary):
    """Initializes from user specified dictionary.

    Args:
      dictionary: dict of user specified attributes
    Returns:
      GitkitUser object
    """
    if 'user_id' in dictionary:
      raise errors.GitkitClientError('use localId instead')
    if 'localId' not in dictionary:
      raise errors.GitkitClientError('must specify localId')
    if 'email' not in dictionary:
      raise errors.GitkitClientError('must specify email')

    return cls(decode=False, **dictionary)
  def _BuildOobLink(self, param, mode):
    """Builds out-of-band URL.

    Gitkit API GetOobCode() is called and the returning code is combined
    with Gitkit widget URL to building the out-of-band url.

    Args:
      param: dict of request.
      mode: string, Gitkit widget mode to handle the oob action after user
          clicks the oob url in the email.

    Raises:
      GitkitClientError: if oob code is not returned.

    Returns:
      A string of oob url.
    """
    code = self.rpc_helper.GetOobCode(param)
    if code:
      parsed = list(parse.urlparse(self.widget_url))

      query = dict(parse.parse_qsl(parsed[4]))
      query.update({'mode': mode, 'oobCode': code})

      try:
        parsed[4] = parse.urlencode(query)
      except AttributeError:
        parsed[4] = urllib.urlencode(query)

      return code, parse.urlunparse(parsed)
    raise errors.GitkitClientError('invalid request')
    def FromConfigFile(cls, config):
        json_data = simplejson.load(open(config))

        key_file = open(json_data['serviceAccountPrivateKeyFile'], 'rb')
        key = key_file.read()
        key_file.close()

        clientId = json_data.get('clientId')
        projectId = json_data.get('projectId')
        if not clientId and not projectId:
            raise errors.GitkitClientError(
                'Missing projectId or clientId in server configuration.')

        return cls(clientId, json_data['serviceAccountEmail'], key,
                   json_data['widgetUrl'], json_data['cookieName'], None,
                   projectId)
Beispiel #5
0
    def _InvokeGitkitApi(self, method, params=None, need_service_account=True):
        """Invokes Gitkit API, with optional access token for service account.

    Args:
      method: string, the api method name.
      params: dict of optional parameters for the API.
      need_service_account: false if service account is not needed.

    Raises:
      GitkitClientError: if the request is bad.
      GitkitServerError: if Gitkit can not handle the request.

    Returns:
      API response as dict.
    """
        body = simplejson.dumps(params) if params else None
        req = urllib_request.Request(self.google_api_url + method)
        req.add_header('Content-type', 'application/json')
        if need_service_account:
            if self.credentials:
                access_token = self.credentials.get_access_token().access_token
            elif self.service_account_email and self.service_account_key:
                access_token = self._GetAccessToken()
            else:
                raise errors.GitkitClientError(
                    'Missing service account credentials')
            req.add_header('Authorization', 'Bearer ' + access_token)
        try:
            binary_body = body.encode('utf-8') if body else None
            raw_response = urllib_request.urlopen(req, binary_body).read()
        except urllib_request.HTTPError as err:
            if err.code == 400:
                raw_response = err.read()
            else:
                raise
        return self._CheckGitkitError(raw_response)