Beispiel #1
0
def get_paginator(token, get_params, filter_params, list_name):
    suffix = '{}/count'.format(list_name)
    url = CORE_API_CP1.format(suffix)

    payload = {'access_token': token}
    payload.update(filter_params)

    request_method = wrap_request_method(requests.get, url, params=payload)

    content = handle_request_errors(request_method,
                                    'get {} count'.format(list_name))
    if 'error_message' in content:
        return content

    items_count = int(content['result'])

    per_page = PAGINATOR_PER_PAGE
    total_pages = ((items_count + per_page) - 1) // per_page

    page_num = parse_page_num(get_params, total_pages)

    return {
        'has_previous': page_num > 1,
        'previous_page_number': page_num - 1,
        'number': page_num,
        'has_next': page_num < total_pages,
        'next_page_number': page_num + 1,
        'num_pages': total_pages
    }
Beispiel #2
0
def get_cities_from_platform():
    url = CORE_API_V1.format("cities")
    request_method = wrap_request_method(requests.get, url)

    content = handle_request_errors(request_method, 'get cities list')
    if 'error_message' in content:
        return content

    cities = [construct_city(city) for city in content['result']]
    return cities
Beispiel #3
0
def get_integrations_from_platform():
    url = CORE_DEMO_API.format('integrations')
    request_method = wrap_request_method(requests.get, url)

    content = handle_request_errors(request_method, 'get integrations list')
    if 'error_message' in content:
        return content

    integrations = [
        construct_integration(integration) for integration in content['result']
    ]
    return integrations
Beispiel #4
0
def get_tts_filename(phrase, from_cache=False):
    suffix = 'tts/phrase/{}'.format(quote(phrase))
    if from_cache:
        suffix += '/cache'

    url = DIALOG_API.format(suffix)
    request_method = wrap_request_method(requests.get, url)

    content = handle_request_errors(request_method, 'get tts filename')
    if 'error_message' in content:
        return content

    return content['result']
Beispiel #5
0
def change_integration(token, integration_id, params):
    fields = ['city_id', 'phone_number', 'sip', 'redirect_to_operator_sip']

    url = CORE_API_CP1.format('integrations/{}'.format(integration_id))
    request_method = wrap_request_method(requests.put, url, json={
        'access_token': token,
        **pre_process_params(fields, params)
    })

    content = handle_request_errors(request_method, 'change integration')
    if 'error_message' in content:
        return content

    return content['result']
Beispiel #6
0
def change_partner(token, partner_id, params):
    fields = ['login', 'password', 'name', 'organization', 'address', 'contact_phones', 'description', 'emails']

    url = CORE_API_CP1.format('partners/{}'.format(partner_id))
    request_method = wrap_request_method(requests.put, url, json={
        'access_token': token,
        **pre_process_params(fields, params)
    })

    content = handle_request_errors(request_method, 'change partner')
    if 'error_message' in content:
        return content

    return content['result']
Beispiel #7
0
def request_location(city_id, tag):
    url = GEO_API.format('location')
    request_method = wrap_request_method(requests.post,
                                         url,
                                         json={
                                             "city": city_id,
                                             "tag": tag,
                                         })

    content = handle_request_errors(request_method, 'request location')
    if 'error_message' in content:
        return content

    return content['result']
Beispiel #8
0
def authorize(username, password):
    url = CORE_API_CP1.format('oauth/authorize')
    request_method = wrap_request_method(requests.post,
                                         url,
                                         json={
                                             'username': username,
                                             'password': password,
                                             'grant_type': 'password',
                                             'client_id': 'AlPlatform'
                                         })

    content = handle_request_errors(request_method, 'authorize')
    if 'error_message' in content:
        return content

    # return token
    return content['access_token']
Beispiel #9
0
def change_order(token, order_id, params):
    with patch('custom_admin.services.modify.requests.put') as mocked_put:
        mocked_put.return_value.json = lambda: extract_mock_file('change', 'order')
        mocked_put.return_value.status_code = 200

        fields = ['url', 'type', 'integration_id', 'method']

        url = CORE_API_CP1.format('orders/{}'.format(order_id))
        request_method = wrap_request_method(requests.put, url, json={
            'access_token': token,
            **pre_process_params(fields, params)
        })

        content = handle_request_errors(request_method, 'change order')
        if 'error_message' in content:
            return content

        return content['result']
Beispiel #10
0
def request_tagging(city_id, query_text):
    url = SYNTAX_API.format('tagging')
    request_method = wrap_request_method(requests.post,
                                         url,
                                         json={
                                             'text':
                                             query_text,
                                             'tag_types': [{
                                                 "tag_type": "geo",
                                                 "city_id": city_id
                                             }],
                                         })

    content = handle_request_errors(request_method, 'request tagging')
    if 'error_message' in content:
        return content

    return content['result']
Beispiel #11
0
def is_authenticated(token):
    # core doesn't have method to check validity of a token, using 'partners' endpoint instead
    url = CORE_API_CP1.format('partners')

    payload = {
        'access_token': token,
    }
    request_method = wrap_request_method(requests.get, url, params=payload)

    content = handle_request_errors(request_method,
                                    'get partners (unauthenticated)')

    if 'error_message' not in content:
        return True
    elif 'error' in content['response'] and content['response'][
            'error'] == 'invalid_token':
        return False
    else:
        return content
Beispiel #12
0
def get_list(token, list_name, page_num, filter_params, order_params):
    url = CORE_API_CP1.format(list_name)
    if 'integration_id' in filter_params:
        url += '/integration/{}'.format(filter_params['integration_id'])
    elif 'partner_id' in filter_params:
        url += '/partner/{}'.format(filter_params['partner_id'])

    payload = {
        'access_token': token,
        'limit': PAGINATOR_PER_PAGE,
        'offset': PAGINATOR_PER_PAGE * (page_num - 1)
    }
    payload.update(order_params)
    request_method = wrap_request_method(requests.get, url, params=payload)

    content = handle_request_errors(request_method, 'get {}'.format(list_name))
    if 'error_message' in content:
        return content

    return content['result']