{% comment %} {% endcomment %}{% load i18n quickapi_base %}

{% trans 'Requests' %}

{% trans 'Authentication' %}

{% trans 'When developing a Django-app for browser you can not think about it.' %} {% trans 'Authentication will occur on the session key as it usually works in' %} Django.

{% trans 'Authorized access to the execute methods from third-party applications and scripts is carried out according to the following rules.' %}

{% trans 'Normal authorization - is the transmission in the clear parameters username and password directly inside the POST request or inside jsonData.' %}

{% trans 'Structure of requests' %}

{% trans 'Any request should be is mandatory key method and optional language.' %}

{% trans 'In POST request can additionally be keys:' %}username, password.

{% trans 'Other keys are regarded as parameters of the method.' %}

{% trans 'When using difficult-structured parameters, all keys except the language, must be packed in one jsonData (JSON encoded as ' %} URL Encoding). {% trans 'This only works with POST requests.' %}

{% trans 'For transfer of reserved names username, password and language in the role of parameters for the method, you must wrap all the parameters in the key kwargs inside jsonData.' %}

### GET ###
curl "{{ api_url }}?method=quickapi.test"
curl "{{ api_url }}?method=quickapi.test&code=302&redirect=/login/"
curl "{{ api_url }}?method=quickapi.test&language=ru"

### POST ###
curl "{{ api_url }}" -d "method=quickapi.test&language=en"
curl "{{ api_url }}" -d "method=quickapi.test&username=test&password=test"

### POST + JSON ###
curl "{{ api_url }}" -d "jsonData=%7B%22method%22%3A%22quickapi.test%22%2C%22kwargs%22%3A%7B%22code%22%3A301%7D%7D"
curl "{{ api_url }}" -d "jsonData=%7B%22method%22%3A%22quickapi.test%22%2C%22kwargs%22%3A%7B%22code%22%3A301%7D%7D&language=en"

{% trans 'Usage ready clients' %}

jQuery

<script src="/static/quickapi/{% get_version "quickapi" %}/js/jquery.quickapi.min.js"></script>
<script>
// {% trans 'returns quickapi.test on /api/' %}
$.quickAPI();

// {% trans 'advanced usage' %}
$.quickAPI({
    url: "/api/", 
    data: {
        method: "quickapi.test",
        kwargs: { code: 200},
    },
    type: "GET",    // `POST` {% trans 'by default' %}
    async: false,   // `true` {% trans 'by default' %}
    timeout: 1000,  // `3000` {% trans 'by default' %}
    language: 'ru', // `window.LANGUAGE_CODE` {% trans 'by default' %}
    simple_request: true // {% trans 'by default returns jsonData' %}
    callback: function(json, status, xhr) {},
    handlerShowAlert: function(head, msg, cls, cb) {},
})
</script>

Python

from quickapi.client import BaseClient

kw = {
    'url': '{{ api_url }}',
    # {% trans 'You can set all important parameters:' %}
    # `username`, `password`, `timeout`, `cookie_filename`...
}

# {% trans 'One instance for all called methods' %}
api = BaseClient(**kw)

# {% trans 'Turn on the output response headers' %}
api.print_info = True

# {% trans 'Not an authorized user' %}
data = api.method('quickapi.test')

# {% trans 'Installation file with cookies' %}
api.set_cookiejar('cookies.txt')

api.username = 'login'
api.password = 'passw'

# {% trans 'You can enable Basic authorization' %}
api.use_basic_auth = True

# {% trans 'The attempted user authorization' %}
data = api.method('quickapi.test')

# {% trans 'Further authentication will take place with the help of cookies, and the data is better to hide' %}
api.username = None
api.password = None

data = api.method('quickapi.test', code=200)

# {% trans 'Test call failures' %}
data = api.method('quickapi.test', code=302)
data = api.method('quickapi.test', code=400)
data = api.method('quickapi.test', code=401)
data = api.method('quickapi.test', code=500)
data = api.method('quickapi.test', bugparam=1)
data = api.method('bug.method.name', code=200)