<commit_before># coding: utf-8

# Copyright (c) 2001-2014, Canal TP and/or its affiliates. All rights reserved.
#
# This file is part of Navitia,
#     the software to build cool stuff with public transport.
#
# Hope you'll enjoy and contribute to this project,
#     powered by Canal TP (www.canaltp.fr).
# Help us simplify mobility and open public transport:
#     a non ending quest to the responsive locomotion way of traveling!
#
# LICENCE: This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Stay tuned using
# twitter @navitia
# IRC #navitia on freenode
# https://groups.google.com/d/forum/navitia
# www.navitia.io
from __future__ import absolute_import, print_function, unicode_literals, division
from jormungandr import cache, app
import pybreaker
import logging
import json
import requests as requests
from jormungandr.ptref import FeedPublisher
from jormungandr.parking_space_availability.bss.stands import Stands, StandsStatus
from jormungandr.parking_space_availability.bss.common_bss_provider import CommonBssProvider, BssProxyError


DEFAULT_CYKLEO_FEED_PUBLISHER = {'id': 'cykleo', 'name': 'cykleo', 'license': 'Private', 'url': 'www.cykleo.fr'}


class CykleoProvider(CommonBssProvider):
    def __init__(
        self,
        url,
        network,
        username,
        password,
        operators={'cykleo'},
        verify_certificate=False,
        service_id=None,
        organization_id=None,
        timeout=2,
        feed_publisher=DEFAULT_CYKLEO_FEED_PUBLISHER,
        **kwargs
    ):
        self.url = url
        self.network = network.lower()
        self.service_id = service_id
        self.organization_id = organization_id
        self.verify_certificate = verify_certificate
        self.username = username
        self.password = password
        self.operators = [o.lower() for o in operators]
        self.timeout = timeout
        self.breaker = pybreaker.CircuitBreaker(
            fail_max=kwargs.get('circuit_breaker_max_fail', app.config['CIRCUIT_BREAKER_MAX_CYKLEO_FAIL']),
            reset_timeout=kwargs.get(
                'circuit_breaker_reset_timeout', app.config['CIRCUIT_BREAKER_CYKLEO_TIMEOUT_S']
            ),<commit_msg>Doc: add how-to debug to cykleo connector in jormun<commit_after># coding: utf-8

# Copyright (c) 2001-2014, Canal TP and/or its affiliates. All rights reserved.
#
# This file is part of Navitia,
#     the software to build cool stuff with public transport.
#
# Hope you'll enjoy and contribute to this project,
#     powered by Canal TP (www.canaltp.fr).
# Help us simplify mobility and open public transport:
#     a non ending quest to the responsive locomotion way of traveling!
#
# LICENCE: This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Stay tuned using
# twitter @navitia
# IRC #navitia on freenode
# https://groups.google.com/d/forum/navitia
# www.navitia.io
from __future__ import absolute_import, print_function, unicode_literals, division
from jormungandr import cache, app
import pybreaker
import logging
import json
import requests as requests
from jormungandr.ptref import FeedPublisher
from jormungandr.parking_space_availability.bss.stands import Stands, StandsStatus
from jormungandr.parking_space_availability.bss.common_bss_provider import CommonBssProvider, BssProxyError


DEFAULT_CYKLEO_FEED_PUBLISHER = {'id': 'cykleo', 'name': 'cykleo', 'license': 'Private', 'url': 'www.cykleo.fr'}


class CykleoProvider(CommonBssProvider):
    """
    class managing calls to Cykleo external service providing real-time BSS stands availability

    The service requires authentication

    curl example to check/test that external service is working:
    # first get a token
    curl -H 'Content-Type: application/json' -H 'Accept: application/json'
         -d '{"username": "{username}", "password": "{password}", "sphere": "VLS"}'
         -X POST '{url}/bo/auth'
    # then retrieve the whole message referential using the 'access_token' value obtained
    curl -H 'Authorization: Bearer {access_token}' -d 'organization_id={organization_id}'
         -X GET '{url}/bo/stations/availability'

    All variables are provided on connector's init, except for the {access_token}
    Then the connector matches 'station/assetStation/commercialNumber' in Cykleo API with
    the 'ref' value in OSM.

    So in practice it will look like:
    curl -H 'Content-Type: application/json' -H 'Accept: application/json'
         -d '{"username": "toto@bobit<EMAIL>", "password": "<PASSWORD>", "sphere": "VLS"}'
         -X POST 'https://url.cykleo.com/bo/auth
    curl -H 'Authorization: Bearer fzkS.Ekb4S.QA' -d 'organization_id=42'
         -X GET 'https://url.cykleo.com/bo/stations/availability'
    """

    def __init__(
        self,
        url,
        network,
        username,
        password,
        operators={'cykleo'},
        verify_certificate=False,
        service_id=None,
        organization_id=None,
        timeout=2,
        feed_publisher=DEFAULT_CYKLEO_FEED_PUBLISHER,
        **kwargs
    ):
        self.url = url
        self.network = network.lower()
        self.service_id = service_id
        self.organization_id = organization_id
        self.verify_certificate = verify_certificate
        self.username = username
        self.password = password
        self.operators = [o.lower() for o in operators]
        self.timeout = timeout
        self.breaker = pybreaker.CircuitBreaker(
            fail_max=kwargs.get('circuit_breaker_max_fail', app.config['CIRCUIT_BREAKER_MAX_CYKLEO_FAIL']),
            reset_timeout=kwargs.get(
                'circuit_breaker_reset_timeout', app.config['CIRCUIT_BREAKER_CYKLEO_TIMEOUT_S']
            ),
{'fraction_non_alphanumeric': 0.08531976744186047, 'fraction_numerical': 0.003633720930232558, 'mean_word_length': 3.491514360313316, 'pattern_counts': {'":': 6, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 4, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3141883', 'n_tokens_mistral': 2073, 'n_tokens_neox': 1989, 'n_words': 664}