#3337 Add a new /-whoami API endpoint
Merged 5 years ago by pingou. Opened 5 years ago by pingou.

file modified
+8 -6
@@ -122,14 +122,14 @@ 

      ''' Checks if the user provided an API token with its request and if

      this token allows the user to access the endpoint desired.

      '''

+     if authenticated():

+         return

+ 

      flask.g.token = None

-     flask.g.user = None

+     flask.g.fas_user = None

      token = None

      token_str = None

  

-     if authenticated():

-         return

- 

      if 'Authorization' in flask.request.headers:

          authorization = flask.request.headers['Authorization']

          if 'token' in authorization:
@@ -261,6 +261,7 @@ 

  

  @API.route('/version/')

  @API.route('/version')

+ @API.route('/-/version')

  def api_version():

      '''

      API Version
@@ -269,7 +270,7 @@ 

  

      ::

  

-         GET /api/0/version

+         GET /api/0/-/version

  

      Sample response

      ^^^^^^^^^^^^^^^
@@ -457,6 +458,7 @@ 

  

  @API.route('/error_codes/')

  @API.route('/error_codes')

+ @API.route('/-/error_codes')

  def api_error_codes():

      '''

      Error codes
@@ -465,7 +467,7 @@ 

  

      ::

  

-         GET /api/0/error_codes

+         GET /api/0/-/error_codes

  

      Sample response

      ^^^^^^^^^^^^^^^

file modified
+2 -2
@@ -24,7 +24,7 @@ 

  from pagure.api import (API, api_method, api_login_required, APIERROR,

                          get_authorized_api_project, get_request_data)

  from pagure.config import config as pagure_config

- from pagure.utils import is_repo_committer, is_true

+ from pagure.utils import authenticated, is_repo_committer, is_true

  

  

  _log = logging.getLogger(__name__)
@@ -852,7 +852,7 @@ 

          raise pagure.exceptions.APIError(

              404, error_code=APIERROR.EPULLREQUESTSDISABLED)

  

-     if flask.g.token and flask.g.token.project \

+     if not authenticated() and flask.g.token and flask.g.token.project \

              and repo != flask.g.token.project:

          raise pagure.exceptions.APIError(

              401, error_code=APIERROR.EINVALIDTOK)

@@ -45,15 +45,22 @@ 

                                              content_type='application/json'):

              self.assertEqual(pagure.api.get_request_data()['foo'], 'bar')

  

-     def test_api_version(self):

+     def test_api_version_old_url(self):

          """ Test the api_version function.  """

- 

          output = self.app.get('/api/0/version')

          self.assertEqual(output.status_code, 200)

          data = json.loads(output.get_data(as_text=True))

          self.assertEqual(data['version'], pagure.__api_version__)

          self.assertEqual(sorted(data.keys()), ['version'])

  

+     def test_api_version_new_url(self):

+         """ Test the api_version function at its new url.  """

+         output = self.app.get('/api/0/-/version')

+         self.assertEqual(output.status_code, 200)

+         data = json.loads(output.get_data(as_text=True))

+         self.assertEqual(data['version'], pagure.__api_version__)

+         self.assertEqual(sorted(data.keys()), ['version'])

+ 

      def test_api_project_tags(self):

          """ Test the api_project_tags function.  """

          tests.create_projects(self.session)

@@ -1006,11 +1006,11 @@ 

          # Valid token, wrong project

          output = self.app.post(

              '/api/0/test2/pull-request/1/subscribe', headers=headers)

-         self.assertEqual(output.status_code, 401)

+         self.assertEqual(output.status_code, 404)

          data = json.loads(output.get_data(as_text=True))

-         self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,

+         self.assertEqual(pagure.api.APIERROR.ENOREQ.name,

                           data['error_code'])

-         self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])

+         self.assertEqual(pagure.api.APIERROR.ENOREQ.value, data['error'])

  

          # No input

          output = self.app.post(

Is the dash in the URL to avoid collision with a project name?

@pingou Why not use /-/<action> for APIs? It's not possible for projects to be named just -.

Is the dash in the URL to avoid collision with a project name?

Yes :)

Why not use /-/<action> for APIs? It's not possible for projects to be named just -.

Good idea, I'll do that and it'll allow us to port some of the existing endpoints to that namespace as well (like the version endpoint, which exists and isn't black-listed...)

rebased onto 668a626039041a73c27425f9a456b76fb2cf4ce1

5 years ago

rebased onto e59df4e9a55c07d41dbb9cad34ef875a05b007e6

5 years ago

Pretty please pagure-ci rebuild

rebased onto 72b266f80111436d3b837e01a58c417875570a31

5 years ago

rebased onto 6cb865f18a40be81b32de013f8022a26769ae260

5 years ago

rebased onto 0d95043a3df5a654cf97a5318f738fbdd806cb7b

5 years ago

Pretty please pagure-ci rebuild

rebased onto c3564974a5f016bcb9af7f6af02b7168d609e447

5 years ago

rebased onto 56f3a7429a63a3b2106a1dc9fb93df4697a1e8a2

5 years ago

Pretty please pagure-ci rebuild

rebased onto 08bc4d632c899917f1640f0bfa8a01278f85eff9

5 years ago

rebased onto 02d9b10ea07d9061eb99a852a6244090186ef37d

5 years ago

Do you have ETA when It could be merged?

rebased onto fc8b3f1

5 years ago

Pretty please pagure-ci rebuild

Pull-Request has been merged by pingou

5 years ago