Blame progit/api/__init__.py

Pierre-Yves Chibon 33b534
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 0537b7
Pierre-Yves Chibon 0537b7
"""
Pierre-Yves Chibon 0537b7
 (c) 2015 - Copyright Red Hat Inc
Pierre-Yves Chibon 0537b7
Pierre-Yves Chibon 0537b7
 Authors:
Pierre-Yves Chibon 0537b7
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 0537b7
Pierre-Yves Chibon 0537b7
API namespace version 0.
Pierre-Yves Chibon 0537b7
Pierre-Yves Chibon 0537b7
"""
Pierre-Yves Chibon 0537b7
Pierre-Yves Chibon 0537b7
import flask
Pierre-Yves Chibon 0537b7
Pierre-Yves Chibon 0537b7
API = flask.Blueprint('api_ns', __name__, url_prefix='/api/0')
Pierre-Yves Chibon 0537b7
Pierre-Yves Chibon 0537b7
Pierre-Yves Chibon 0537b7
from progit import __api_version__, APP, SESSION
Pierre-Yves Chibon 0537b7
import progit
Pierre-Yves Chibon 0537b7
import progit.lib
Pierre-Yves Chibon 0537b7
Pierre-Yves Chibon 0537b7
Pierre-Yves Chibon 0537b7
@API.route('/version/')
Pierre-Yves Chibon 0537b7
@API.route('/version')
Pierre-Yves Chibon 0537b7
def api_version():
Pierre-Yves Chibon 0537b7
    '''
Pierre-Yves Chibon 0537b7
    API Version
Pierre-Yves Chibon 0537b7
    -----------
Pierre-Yves Chibon 0537b7
    Display the most recent api version.
Pierre-Yves Chibon 0537b7
Pierre-Yves Chibon 0537b7
    ::
Pierre-Yves Chibon 0537b7
Pierre-Yves Chibon 0537b7
        /api/version
Pierre-Yves Chibon 0537b7
Pierre-Yves Chibon 0537b7
    Accepts GET queries only.
Pierre-Yves Chibon 0537b7
Pierre-Yves Chibon 0537b7
    Sample response:
Pierre-Yves Chibon 0537b7
Pierre-Yves Chibon 0537b7
    ::
Pierre-Yves Chibon 0537b7
Pierre-Yves Chibon 0537b7
        {
Pierre-Yves Chibon 0537b7
          "version": "1"
Pierre-Yves Chibon 0537b7
        }
Pierre-Yves Chibon 0537b7
Pierre-Yves Chibon 0537b7
    '''
Pierre-Yves Chibon 0537b7
    return flask.jsonify({'version': __api_version__})
Pierre-Yves Chibon 0537b7
Pierre-Yves Chibon 0537b7
Pierre-Yves Chibon 0537b7
@API.route('/users/')
Pierre-Yves Chibon 0537b7
@API.route('/users')
Pierre-Yves Chibon 0537b7
def api_users():
Pierre-Yves Chibon 0537b7
    '''
Pierre-Yves Chibon 0537b7
    List users
Pierre-Yves Chibon 0537b7
    -----------
Pierre-Yves Chibon 0537b7
    Returns the list of all users that have logged into this progit instances.
Pierre-Yves Chibon 0537b7
    This can then be used as input for autocompletion in some forms/fields.
Pierre-Yves Chibon 0537b7
Pierre-Yves Chibon 0537b7
    ::
Pierre-Yves Chibon 0537b7
Pierre-Yves Chibon 0537b7
        /api/users
Pierre-Yves Chibon 0537b7
Pierre-Yves Chibon 0537b7
    Accepts GET queries only.
Pierre-Yves Chibon 0537b7
Pierre-Yves Chibon 0537b7
    Sample response:
Pierre-Yves Chibon 0537b7
Pierre-Yves Chibon 0537b7
    ::
Pierre-Yves Chibon 0537b7
Pierre-Yves Chibon 0537b7
        {
Pierre-Yves Chibon 0537b7
          "users": ["user1", "user2"]
Pierre-Yves Chibon 0537b7
        }
Pierre-Yves Chibon 0537b7
Pierre-Yves Chibon 0537b7
    '''
Pierre-Yves Chibon 0537b7
    pattern = flask.request.args.get('pattern', None)
Pierre-Yves Chibon 0537b7
    if pattern is not None and not pattern.endswith('*'):
Pierre-Yves Chibon 0537b7
        pattern += '*'
Pierre-Yves Chibon 0537b7
Pierre-Yves Chibon 0537b7
    return flask.jsonify(
Pierre-Yves Chibon 0537b7
        {
Pierre-Yves Chibon 0537b7
            'users': [
Pierre-Yves Chibon 0537b7
                user.username
Pierre-Yves Chibon a1a039
                for user in progit.lib.search_user(
Pierre-Yves Chibon 0537b7
                    SESSION, pattern=pattern)
Pierre-Yves Chibon 0537b7
            ]
Pierre-Yves Chibon 0537b7
        }
Pierre-Yves Chibon 0537b7
    )
Pierre-Yves Chibon 89a23a
Pierre-Yves Chibon 89a23a
Pierre-Yves Chibon 89a23a
@API.route('/<repo>/tags')</repo>
Pierre-Yves Chibon 89a23a
@API.route('/<repo>/tags/')</repo>
Pierre-Yves Chibon 89a23a
@API.route('/fork/<username>/<repo>/tags')</repo></username>
Pierre-Yves Chibon 89a23a
@API.route('/fork/<username>/<repo>/tags/')</repo></username>
Pierre-Yves Chibon 89a23a
def api_project_tags(repo, username=None):
Pierre-Yves Chibon 89a23a
    '''
Pierre-Yves Chibon 89a23a
    List all the tags of a project
Pierre-Yves Chibon 89a23a
    ------------------------------
Pierre-Yves Chibon 89a23a
    Returns the list of all tags of the specified project.
Pierre-Yves Chibon 89a23a
Pierre-Yves Chibon 89a23a
    ::
Pierre-Yves Chibon 89a23a
Pierre-Yves Chibon 89a23a
        /api/<repo>/tags</repo>
Pierre-Yves Chibon 89a23a
Pierre-Yves Chibon 89a23a
        /api/fork/<username>/<repo>/tags</repo></username>
Pierre-Yves Chibon 89a23a
Pierre-Yves Chibon 89a23a
    Accepts GET queries only.
Pierre-Yves Chibon 89a23a
Pierre-Yves Chibon 89a23a
    Sample response:
Pierre-Yves Chibon 89a23a
Pierre-Yves Chibon 89a23a
    ::
Pierre-Yves Chibon 89a23a
Pierre-Yves Chibon 89a23a
        {
Pierre-Yves Chibon 89a23a
          "tags": ["tag1", "tag2"]
Pierre-Yves Chibon 89a23a
        }
Pierre-Yves Chibon 89a23a
Pierre-Yves Chibon 89a23a
    '''
Pierre-Yves Chibon 89a23a
    pattern = flask.request.args.get('pattern', None)
Pierre-Yves Chibon 89a23a
    if pattern is not None and not pattern.endswith('*'):
Pierre-Yves Chibon 89a23a
        pattern += '*'
Pierre-Yves Chibon 89a23a
Pierre-Yves Chibon 89a23a
    project = progit.lib.get_project(SESSION, repo, username)
Pierre-Yves Chibon 89a23a
    if not project:
Pierre-Yves Chibon 89a23a
        output = {'output': 'notok', 'error': 'Project not found'}
Pierre-Yves Chibon 89a23a
        jsonout = flask.jsonify(output)
Pierre-Yves Chibon 89a23a
        jsonout.status_code = 404
Pierre-Yves Chibon 89a23a
        return jsonout
Pierre-Yves Chibon 89a23a
Pierre-Yves Chibon 89a23a
    return flask.jsonify(
Pierre-Yves Chibon 89a23a
        {
Pierre-Yves Chibon 89a23a
            'tags': [
Pierre-Yves Chibon 89a23a
                tag.tag
Pierre-Yves Chibon 89a23a
                for tag in progit.lib.get_tags_of_project(
Pierre-Yves Chibon 89a23a
                    SESSION, project, pattern=pattern)
Pierre-Yves Chibon 89a23a
            ]
Pierre-Yves Chibon 89a23a
        }
Pierre-Yves Chibon 89a23a
    )