Exemple #1
0
    def _get_link(self, method_name=None, params=None, method_type="GET", is_json=True):
        """Method returns responsibility link for requested method.
        While choice PUT method, it automatically changing header 'Content-type' into "application/json".

        Can't be use with SOAP client"""
        if self.api_type == "REST":
            self.client.setup(method=method_type)

            def _ret(_params):
                if not params:
                    return ''
                return_params = '?'
                for key in _params:
                    return_params += '{}={}&'.format(key, _params[key])
                return return_params[:-1]

            if method_type == "GET":
                link = self.base_url + method_name + _ret(params)
            elif method_type == "PUT":
                self.client.setup(post=dumps(params).encode())
                if is_json:
                    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0',
                               'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                               'Accept-Language': 'ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3',
                               'Accept-Encoding': 'gzip, deflate',
                               'Connection': 'keep-alive',
                               "Content-Type": "application/json"}
                    self.client.setup(headers=headers)
                link = self.base_url + method_name
            else:
                raise ParameterError("Невозможный тип запроса")
            return link
        elif self.api_type == "SOAP":
            raise AccessError
Exemple #2
0
def getMsgConsumer(id: ModuleID) -> 'MsgConsumer':
    # wait untill all consumers are instantiated
    globalvars.consumersReadyEvent.wait()
    for consumer in globalvars.msgConsumers:
        if consumer.id == id:
            return consumer
    raise ParameterError("Unknown module ID " + str(id))
Exemple #3
0
 def convert_param(self, name, spec, value):
     try:
         return self.validate(name, spec, self.convert(name, spec, value))
     except (TypeError, ValueError) as error:
         if self.log:
             self.log.exception('ParameterError: %s: %s: %r', name, error,
                                value)
         raise ParameterError(name, '{name}: {error}', error=error)
Exemple #4
0
 def _check_datetime(dt):
     """Checking format of datetime attr. Returns datetime in ISO"""
     if not re.search(pattern=r'\d{4}-\d{2}-\d{2}', string=dt):
         if not re.search(pattern=r'\d{2}.\d{2}.\d{4}', string=dt):
             raise ParameterError('Неверный формат даты')
         else:
             return datetime.strptime(dt, "%d.%m.%Y").isoformat()
     else:
         return datetime.strptime(dt, "%Y-%m-%d").isoformat()
Exemple #5
0
 def create_detail_request(self, period):
     """Creates detail request. Returns requestId"""
     if not re.search(pattern=r'\d{4}-\d{2}-\d{2}', string=period):
         if not re.search(pattern=r'\d{2}.\d{2}.\d{4}', string=period):
             raise ParameterError('Неверный формат даты, нужен гггг-мм-дд или дд.мм.гггг')
         else:
             bill_date = datetime.strptime(period, "%d.%m.%Y").isoformat()
     else:
         bill_date = datetime.strptime(period, "%Y-%m-%d").isoformat()
     url = self._get_link('/request/postpaidDetail', {'ctn': self.ctn, 'billDate': bill_date})
     return self._get_results(url)
Exemple #6
0
    def convert_params(self, specification, params):
        converted = {}
        for name, spec in specification.items():
            value = params.get(name, None)

            if value is None or value == '' and self.ignore_empty_params and '' not in spec.get(
                    'enum', ()):
                if spec.get('required', False):
                    raise ParameterError(
                        name,
                        '{missing} required parameter: {name!r}',
                        missing='misssing' if value is None else 'empty')

                value = spec.get('default', None)

                if value is None:
                    converted[name] = None
                    continue

            converted[name] = self.convert_param(name, spec, value)

        return converted
Exemple #7
0
 def replace_sim(self, sim):
     if not self.ctn:
         raise ParameterError('CTN обязателен')
     params = {'ctn': self.ctn, 'serialNumber': sim}
     return self._get_results('replaceSIM', params)