class APIClientTestCase(TestCase): """ A Test case for the API Client. """ def setUp(self): class TempHotel(AbstractResource): def __str__(self): return "hotel" self.client = APIClient() self.test_hotel = TempHotel() def tearDown(self): pass def test_do_request(self, manual_url=None): """Test a request directly""" url = self.client.construct_resource_url(self.test_hotel, action="list") or manual_url print(url) response = self.client.request(url) self.assertEqual(response.status, 200) def test_resource_locator_creation(self): """Test that URL construction works properly""" url = self.client.construct_resource_url(self.test_hotel, action="list") self.test_do_request(manual_url=url)
def __init__(self, account='', login='', password=''): self.Account, self.Login, self.Password = account, login, password self._client = APIClient() self._data = str() self._AccessId = str() self._SecretKey = str()
def test_password_auth(email, password): with session_scope() as db_session: create_account(db_session, email, password) start_time = time.time() # Check that the account exists while time.time() - start_time < TEST_MAX_DURATION_SECS: client = APIClient.from_email(email)[0] if client is not None: break time.sleep(TEST_GRANULARITY_CHECK_SECS) if client is None: assert False, "Account namespace should have been created" # Now, compute how much time it takes to start syncing the account start_time = time.time() got_messages = False while time.time() - start_time < TEST_MAX_DURATION_SECS: messages = client.get_messages() if len(messages) != 0: got_messages = True break time.sleep(TEST_GRANULARITY_CHECK_SECS) assert got_messages, "Messages should have been found" print "test_password_auth %s %f" % (email, time.time() - start_time) # remove the account with session_scope() as db_session: # remove_account(db_session, email) pass
class AbstractBaseResourceTestCase(TestCase): """ A Test case for the Abstract Base Resource """ def setUp(self): self.client = APIClient() self.hotel = Hotel() def tearDown(self): pass def test_not_implemented(self): """Test that when a Resource class has not implemented methods, the right exceptions are raised.""" self.assertRaises(NotImplementedError, self.hotel.create) self.assertRaises(NotImplementedError, self.hotel.delete) self.assertRaises(NotImplementedError, self.hotel.update) def test_not_allowed(self): """Test that when a Resource class does not allow certain operations, proper Exceptions are raised.""" hotel = Hotel() hotel.allow_create = False hotel.allow_delete = False hotel.allow_update = False self.assertRaises(ResourceMethodNotAllowed, hotel.create) self.assertRaises(ResourceMethodNotAllowed, hotel.delete) self.assertRaises(ResourceMethodNotAllowed, hotel.update) def test_construct_resource_request_url(self): url = self.hotel.construct_resource_request_url("list") response = self.client.request(url) self.assertEqual(response.status, 200)
def setUp(self): class TempHotel(AbstractResource): def __str__(self): return "hotel" self.client = APIClient() self.test_hotel = TempHotel()
def __init__(self, client_id, username="", password="", token="", code=""): self._ClientId = client_id self._Username = username self._Password = password self._Token = token self._Code = code self._client = APIClient() self._client.UserAgent = self._UserAgent self._data = ""
def sign_in(self): self.user = self.userLineEdit.text() self.pw = self.passwordLineEdit.text() self.conn = APIClient(user=self.user, pw=self.pw) if self.rememberMeCheckBox.isChecked(): self.config.set_login_info(self.user, self.pw) else: self.config.erase_login_info() resp = self.conn.get_my_capabilities() if resp.code != 200: # Catch the error specified by the API.. tree = ElementTree.fromstring(resp.read()) elt_err = tree.find(r"./erreur") if elt_err.text: msg = elt_err.text else: # Error returned by the server (all other cases).. msg = str(resp) # Then display the error in a message box.. return QMessageBox.warning(self, r"Warning", msg) tree = ElementTree.fromstring(resp.read()) # On connection success, get user's capability informations.. elt_extract = tree.find(r"./cle_api_rfu/extraction_rfu") if elt_extract.text == r"oui": self.conn.extract = True elt_extract_lim = tree.find(r"./cle_api_rfu/extraction_rfu_limite") if elt_extract_lim.text is not None: self.conn.extract_lim = int(elt_extract_lim.text) elt_update = tree.find(r"./cle_api_rfu/mise_a_jour_rfu") if elt_update.text == r"oui": self.conn.update = True # Then.. self.accept() self.opened.emit()
async def run(parameters): loop = asyncio.get_event_loop() clients = {} async with aiohttp.ClientSession(loop=loop) as session: for region in parameters["regions"]: clients[region] = APIClient(region, session, parameters["key"], parameters["data_path"]) # Await until ctrl+c while not lissandra.exiting: await asyncio.sleep(1) # Exiting, shutdown nicely for client in clients.values(): client.exiting = True for client in clients.values(): await client.shutdown.wait()
def f(): for email, password in passwords: with session_scope() as db_session: create_account(db_session, email, password) client = None ns = None start_time = time.time() while time.time() - start_time < TEST_MAX_DURATION_SECS: time.sleep(TEST_GRANULARITY_CHECK_SECS) client, ns = APIClient.from_email(email) if client is not None: break assert client, ("Creating account from password file" " should have been faster") format_test_result("namespace_creation_time", ns["provider"], email, start_time) # wait a little time for the sync to start. It's necessary # because a lot of tests rely on stuff setup at the beginning # of the sync (for example, a folder hierarchy). start_time = time.time() sync_started = False while time.time() - start_time < TEST_MAX_DURATION_SECS: msgs = client.get_messages() if len(msgs) > 0: sync_started = True break time.sleep(TEST_GRANULARITY_CHECK_SECS) assert sync_started, ("The initial sync should have started") data = {"email": ns["email_address"], "provider": ns["provider"]} start_time = time.time() fn(client, data) format_test_result(fn.__name__, ns["provider"], ns["email_address"], start_time) with session_scope() as db_session: # delete account pass
class GeoFoncierAPILogin(QDialog, gui_dlg_login): closed = pyqtSignal() opened = pyqtSignal() def __init__(self, parent=None): super(GeoFoncierAPILogin, self).__init__(parent) self.setupUi(self) self.config = Configuration() self.conn = None if self.config.user and self.config.pw: self.userLineEdit.setText(self.config.user) self.passwordLineEdit.setText(self.config.pw) self.rememberMeCheckBox.setChecked(True) self.signInPushButton.clicked.connect(self.sign_in) def closeEvent(self, event): self.closed.emit() def sign_in(self): self.user = self.userLineEdit.text() self.pw = self.passwordLineEdit.text() self.conn = APIClient(user=self.user, pw=self.pw) if self.rememberMeCheckBox.isChecked(): self.config.set_login_info(self.user, self.pw) else: self.config.erase_login_info() resp = self.conn.get_my_capabilities() if resp.code != 200: # Catch the error specified by the API.. tree = ElementTree.fromstring(resp.read()) elt_err = tree.find(r"./erreur") if elt_err.text: msg = elt_err.text else: # Error returned by the server (all other cases).. msg = str(resp) # Then display the error in a message box.. return QMessageBox.warning(self, r"Warning", msg) tree = ElementTree.fromstring(resp.read()) # On connection success, get user's capability informations.. elt_extract = tree.find(r"./cle_api_rfu/extraction_rfu") if elt_extract.text == r"oui": self.conn.extract = True elt_extract_lim = tree.find(r"./cle_api_rfu/extraction_rfu_limite") if elt_extract_lim.text is not None: self.conn.extract_lim = int(elt_extract_lim.text) elt_update = tree.find(r"./cle_api_rfu/mise_a_jour_rfu") if elt_update.text == r"oui": self.conn.update = True # Then.. self.accept() self.opened.emit()
def __init__(self, symbol=None, **kwargs): APIClient.__init__(self, symbol, **kwargs) self.tech_methods = {'RSI': self.get_rsi, 'WR': self.get_willr, 'SMA': self.get_sma, 'EMA': self.get_ema, 'WMA': self.get_wma, 'STOCH': self.get_stoch, 'ADX': self.get_adx, 'ROC': self.get_roc, 'CCI': self.get_cci, 'TRIX': self.get_trix, 'MACD': self.get_macd, 'DX': self.get_dx}
class Megaplan(object): """ """ debug = False HOST = '{account}.megaplan.ru/' SIGNATURE = '{method}\n{md5content}\n{contenttype}\n{date}\n{host}{uri}' _CommonApi = 'BumsCommonApiV01/' _ProjectApi = 'BumsProjectApiV01/' _StaffApi = 'BumsStaffApiV01/' _TaskApi = 'BumsTaskApiV01/' _TimeApi = 'BumsTimeApiV01/' _TradeApi = 'BumsTradeApiV01/' AUTHORIZE = _CommonApi + 'User/' COMMENT = _CommonApi + 'Comment/' DEAL = _TradeApi + 'Deal/' DEPARTMENT = _StaffApi + 'Department/' EMPLOYEE = _StaffApi + 'Employee/' EVENT = _TimeApi + 'Event/' FAVORITE = _CommonApi + 'Favorite/' INFORMER = _CommonApi + 'Informer/' PROJECT = _ProjectApi + 'Project/' SEARCH = _CommonApi + 'Search/' SEVERITY = _TaskApi + 'Severity/' TASK = _TaskApi + 'Task/' TODOLIST = _TimeApi + 'TodoList/' code = 'utf-8' _FolderType = ( 'incoming', 'responsible', 'executor', 'owner', 'auditor', 'all', ) _StatusType = ( 'actual', 'inprocess', 'new', 'overdue', 'done', 'delayed', 'completed', 'failed', 'any', ) _ActionType = ( 'act_accept_task', 'act_reject_task', 'act_accept_work', 'act_reject_work', 'act_done', 'act_pause', 'act_resume', 'act_cancel', 'act_expire', 'act_renew', ) _SubjectType = ( 'task', 'project', ) _OrderType = ( 'asc', 'desc', ) @property def Account(self): return self._Account @Account.setter def Account(self, account): self._Account = account self._host = self.HOST.format(account=account) self._MPQuery = 'https://{host}{uri}'.format(host=self._host, uri='{uri}') @property def Login(self): return self._Login @Login.setter def Login(self, login): self._Login = login @property def Password(self): return self._Password @Password.setter def Password(self, password): self._Password = password def __init__(self, account='', login='', password=''): self.Account, self.Login, self.Password = account, login, password self._client = APIClient() self._data = str() self._AccessId = str() self._SecretKey = str() def _TimeAsRfc822(self, dt): return formatdate( mktime_tz(parsedate_tz(dt.strftime('%a, %d %b %Y %H:%M:%S')))) def _GetResponseObject(f): def wrapper(self): obj = JSON2Obj(self._data) if 'error' == obj.status['code']: if 'message' in obj.status: raise Exception(obj.status['message']) return f(self, obj) return wrapper @_GetResponseObject def _AuthorizeHandle(self, obj): self._AccessId = obj.data['AccessId'] self._SecretKey = obj.data['SecretKey'] if self.debug: self._MPQuery = 'http://{host}'.format(host=self._host) + '{uri}' def _Authorize(self): uri = self.AUTHORIZE + 'authorize.api' md5pass = md5.new(self.Password).hexdigest() params = {'Login': self.Login, 'Password': md5pass} self._data = \ self._client.Request(self._MPQuery.format(uri=uri), params) self._AuthorizeHandle() def _Auth(f): def wrapper(self, *args, **kwargs): if (self._AccessId == '') or (self._SecretKey == ''): self._Authorize() return f(self, *args, **kwargs) return wrapper def _GetSignature(self, method, uri, params={}): self._rfcdate = self._TimeAsRfc822(datetime.now()) self._md5content = '' contenttype = '' if 'POST' == method: self._md5content = md5.new(urlencode(params)).hexdigest() contenttype = 'application/x-www-form-urlencoded' sign = { 'method': method, 'md5content': self._md5content, 'contenttype': contenttype, 'date': self._rfcdate, 'host': self._host, 'uri': uri } q = self.SIGNATURE.format(**sign) h = hmac.HMAC(self._SecretKey.encode(self.code), q, sha1) return base64.encodestring(h.hexdigest()).strip() def _GetHeaders(self, uri, params={}): method = 'GET' if params: method = 'POST' signature = self._GetSignature(method, uri, params) header = { 'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.0; ru; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language': 'ru,en-us;q=0.7,en;q=0.3', 'Accept-Encoding': 'gzip,deflate', 'Accept-Charset': 'utf-8;q=0.7,*;q=0.7', 'Keep-Alive': '300', 'Connection': 'keep-alive', 'Date': self._rfcdate, 'X-Authorization': '{0}:{1}'.format(self._AccessId, signature) } if method == 'GET': header['Accept'] = 'application/json' elif method == 'POST': header['Content-MD5'] = self._md5content return header @_GetResponseObject def _ResponseHandle(self, obj): return obj @_Auth def _GetData(self, uri, params={}): headers = self._GetHeaders(uri, params) self._data = self._client.Request(self._MPQuery.format(uri=uri), params=params, headers=headers) if self._client.Status in (400, 401, 403, 404, 500): raise ClientError('{0} {1}'.format(self._client.Status, self._client.Reason)) return self._ResponseHandle() def GetData(self): return self._data def Tasks(self, Folder='all', Status='any', FavoritesOnly=False, Search=''): """ input: Folder='all': string = ('incoming' (входящие), 'responsible' (ответственный), 'executor' (соисполнитель), 'owner' (исходящие), 'auditor' (аудируемые), 'all' (все)) # Папка Status='any': string = ('actual' (актуальные), 'inprocess' (в процессе), 'new' (новые), 'overdue' (просроченные), 'done' (условно завершенные), 'delayed' (отложенные), 'completed' (завершенные), 'failed' (проваленные), 'any' (любые)) # Статус FavoritesOnly=0: integer = (0, 1) # Только избранное Search='': string = 'любая строка' # Строка поиска output: tasks<array>: Id: integer # ID задачи Name: string # Название Status: string # Статус Deadline: datetime # Дедлайн Owner: object(Id, Name) # Постановщик (сотрудник) Responsible: object(Id, Name) # Ответственный (сотрудник) Severity: object(Id, Name) # Важность SuperTask: object(Id, Name) # Надзадача Project: object(Id, Name) # Проект Favorite: integer # В избранном TimeCreated: datetime # Время создания """ if (Folder not in self._FolderType) or (Status not in self._StatusType): raise AttributeError('Invalid parameter value') uri = '{0}list.api?Folder={1}&Status={2}&FavoritesOnly={3}&Search={4}' uri = uri.format(self.TASK, Folder, Status, int(FavoritesOnly), Search) return self._GetData(uri) def TaskCard(self, Id): """ input: Id: integer # ID задачи. Обязательный параметр output: task: Id: integer # ID задачи Name: string # Название Statement: string # Суть задачи Status: string # Статус Deadline: datetime # Дедлайн DeadlineType: string # Тип дедлайна Owner: object (Id, Name) # Постановщик (сотрудник) Responsible: object (Id, Name) # Ответственный (сотрудник) Executors: array<object> (Id, Name) # Соисполнители (сотрудники) Auditors: array<object> (Id, Name) # Аудиторы (сотрудники) Severity: object (Id, Name) # Важность SuperTask: object (Id, Name) # Надзадача Project: object (Id, Name) # Проект SubTasks: array<object>(Id, Name, Owner, Responsible, Deadline, Favorite (версия 2011.02+)) # Подзадачи Favorite: integer # В избранном TimeCreated: datetime # Время создания Customer: object(Id, Name, Status, Type) # Заказчик (код, название, статус, тип) """ uri = '{0}card.api?Id={1}'.format(self.TASK, Id) return self._GetData(uri) def TaskCreate(self, **kwargs): """ input: kwargs: Model[Name]: string # Название Model[Deadline]: datetime # Дедлайн (дата со временем) Model[DeadlineDate]: date # Дедлайн (только дата) Model[DeadlineType]: string # Тип дедлайна Model[Responsible]: integer # Код ответственного Model[Executors]: array<integer> # Коды соисполнителей Model[Auditors]: array<integer> # Коды аудиторов Model[Severity]: integer # Код важности Model[SuperTask]: string # Код надзадачи (если число) или код проекта (если строка в формате ‘p<код проекта>’ Model[Customer]: integer # Код заказчика Model[IsGroup]: integer = (0, 1) # Массовая задача (каждому соисполнителю будет создана своя задача) Model[Statement]: string # Суть задачи output: Id: integer # ID задачи Name: string # Название задачи """ uri = '{0}create.api'.format(self.TASK) return self._GetData(uri, kwargs) def TaskEdit(self, Id, **kwargs): """ input: Id: integer # ID задачи. Обязательный параметр kwargs: Model[Name]: string # Название Model[Deadline]: datetime # Дедлайн (дата со временем) Model[DeadlineDate]: date # Дедлайн (только дата) Model[DeadlineType]: string # Тип дедлайна Model[Owner]: integer # Код постановщика Model[Responsible]: integer # Код ответственного Model[Executors]: array<integer> # Коды соисполнителей Model[Auditors]: array<integer> # Коды аудиторов Model[Severity]: integer # Код важности Model[SuperTask]: string # Код надзадачи (если число) или код проекта (если строка в формате ‘p<код проекта>’ Model[Customer]: integer # Код заказчика Model[Statement]: string # Суть задачи output: None """ uri = '{0}edit.api'.format(self.TASK) kwargs['Id'] = str(Id) self._GetData(uri, kwargs) def TaskAction(self, Id, Action): """ input: Id: integer # ID задачи. Обязательный параметр Action: string = ('act_accept_task' (принять задачу), 'act_reject_task' (отклонить задачу), 'act_accept_work' (принять работу), 'act_reject_work' (отклонить работу), 'act_done' (завершить задачу), 'act_pause' (поставить на паузу), 'act_resume' (возобновить задачу), 'act_cancel' (снять задачу), 'act_expire' (провалить задачу), 'act_renew' (возобновить задачу)) output: None """ if (Action not in self._ActionType): raise AttributeError('Invalid parameter value') uri = '{0}action.api'.format(self.TASK) params = {'Id': str(Id), 'Action': Action} return self._GetData(uri, params) def TaskAvailableActions(self, Id): """ input: Id: integer # ID задачи. Обязательный параметр output: actions: array act_accept_task - исполнитель принимает задачу act_reject_task - исполнитель отклоняет задачу act_accept_work - постановщик принимает выполненную задачу act_reject_work - постановщик отклоняет выполненную задачу act_done - исполнитель заканчивает работу, задача условно завершена act_pause - временно приостановить выполнение задачи act_resume - продолжить выполнение приостановленной задачи act_cancel - отменить задачу act_expire - провалить задачу act_renew - принять ранее отклоненную задачу """ uri = '{0}availableActions.api?Id={1}'.format(self.TASK, Id) return self._GetData(uri) def TaskMarkAsFavorite(self, Id, Value=True): """ input: Id: integer # ID задачи. Обязательный параметр Value: integer = (1 (пометить как избранное), 0 (убрать из избранного)) output: None """ if Value: self.FavoriteAdd('task', Id) else: self.FavoriteRemove('task', Id) def Projects(self, Folder='all', Status='any', FavoritesOnly=False, Search=''): """ input: Folder='all': string = ('incoming' (входящие), 'responsible' (ответственный), 'executor' (соисполнитель), 'owner' (исходящие), 'auditor' (аудируемые), 'all' (все)) # Папка Status='any': string = ('actual' (актуальные), 'inprocess' (в процессе), 'new' (новые), 'overdue' (просроченные), 'done' (условно завершенные), 'delayed' (отложенные), 'completed' (завершенные), 'failed' (проваленные), 'any' (любые)) # Статус FavoritesOnly=0: integer = (0, 1) # Только избранное Search='': string = 'любая строка' # Строка поиска output: projects<array>: Id: integer # ID проекта Name: string # Название Status: string # Статус Deadline: datetime # Дедлайн Owner: object(Id, Name) # Владелец (сотрудник) Responsible: object(Id, Name) # Менеджер (сотрудник) Severity: object(Id, Name) # Важность SuperProject: object(Id, Name) # Надпроект Favorite: integer # В избранном TimeCreated: datetime # Время создания """ if (Folder not in self._FolderType) or (Status not in self._StatusType): raise AttributeError('Invalid parameter value') uri = '{0}list.api?Folder={1}&Status={2}&FavoritesOnly={3}&Search={4}' uri = uri.format(self.PROJECT, Folder, Status, int(FavoritesOnly), Search) return self._GetData(uri) def ProjectCard(self, Id): """ input: Id: integer # ID проекта. Обязательный параметр output: project: Id: integer # ID проекта Name: string # Название Statement: string # Описание проекта Status: string # Статус Deadline: datetime # Дедлайн DeadlineType: string # Тип дедлайна Owner: object(Id, Name) # Создатель (сотрудник) Responsible: object(Id, Name) # Менеджер (сотрудник) Executors: array<object> (Id, Name) # Команда проекта (сотрудники) Auditors: array<object> (Id, Name) # Аудиторы (сотрудники) Severity: object(Id, Name) # Важность SuperProject: object(Id, Name) # Надпроект SubProjects: array<object>(Id, Name, Owner, Responsible, Deadline, Favorite (версия 2011.02+)) # Подпроекты Tasks: array<object> (Id, Name, Owner, Responsible, Deadline, Favorite (версия 2011.02+)) # Задачи проекта TimeCreated: datetime # Время создания Customer: object(Id, Name, Status, Type) # Заказчик (код, название, статус, тип) """ uri = '{0}card.api?Id={1}'.format(self.PROJECT, Id) return self._GetData(uri) def ProjectCreate(self, **kwargs): """ input: kwargs: Model[Name]: string # Название Model[Deadline]: datetime # Дедлайн (дата со временем) Model[DeadlineDate]: date # Дедлайн (только дата) Model[DeadlineType]: string # Тип дедлайна Model[Responsible]: integer # Код менеджера Model[Executors]: array<integer> # Коды участников проекта Model[Auditors]: array<integer> # Коды аудиторов Model[Severity]: integer # Код важности Model[SuperProject]: integer # Код надпроекта Model[Customer]: integer # Код заказчика Model[Statement]: string # Описание проекта output: Id: integer # ID проекта Name: string # Название проекта У сотрудника может не быть прав на создание проекта. В этом случае команда вернет 403-ю ошибку и следующий ответ: { "status": { "code":"error", "message":"You can not create projects" } } Таким образом, эту команду можно использовать не только для создания проекта, но и для проверки наличия прав на создание проекта (например, чтобы решить, показывать в приложении кнопку "Создать проект" или нет). """ uri = '{0}create.api'.format(self.PROJECT) return self._GetData(uri, kwargs) def ProjectEdit(self, Id, **kwargs): """ input: Id: integer # ID проекта. Обязательный параметр kwargs: Model[Name]: string # Название Model[Deadline]: datetime # Дедлайн (дата со временем) Model[DeadlineDate]: date # Дедлайн (только дата) Model[DeadlineType]: string # Тип дедлайна Model[Owner]: integer # Код постановщика Model[Responsible]: integer # Код менеджера Model[Executors]: array<integer> # Коды участников проекта Model[Auditors]: array<integer> # Коды аудиторов Model[Severity]: integer # Код важности Model[SuperProject]: integer # Код надпроекта Model[Customer]: integer # Код заказчика Model[Statement]: string # Описание проекта output: None """ uri = '{0}edit.api'.format(self.PROJECT) kwargs['Id'] = str(Id) self._GetData(uri, kwargs) def ProjectAction(self, Id, Action): """ input: Id: integer # ID проекта. Обязательный параметр Action: string = ('act_accept_work' (принять работу), 'act_reject_work' (отклонить работу), 'act_done' (завершить проект), 'act_pause' (поставить на паузу), 'act_resume' (возобновить проект), 'act_cancel' (снять проект), 'act_expire' (провалить проект), 'act_renew' (возобновить проект)) output: None """ if (Action not in self._ActionType): raise AttributeError('Invalid parameter value') uri = '{0}action.api'.format(self.PROJECT) params = {'Id': str(Id), 'Action': Action} return self._GetData(uri, params) def ProjectAvailableActions(self, Id): """ input: Id: integer # ID проекта. Обязательный параметр output: actions: array actions: act_accept_work - постановщик принимает выполненный проект act_reject_work - постановщик отклоняет выполненный проект act_done - исполнитель заканчивает работу, проект условно завершен act_pause - временно приостановить выполнение проекта act_resume - продолжить выполнение приостановленного проекта act_cancel - отменить проект act_expire - провалить проект act_renew - открыть проект заново """ uri = '{0}availableActions.api?Id={1}'.format(self.PROJECT, Id) return self._GetData(uri) def ProjectMarkAsFavorite(self, Id, Value=True): """ input: Id: integer # ID проекта. Обязательный параметр Value: integer = (1 (пометить как избранное), 0 (убрать из избранного)) output: None """ if Value: self.FavoriteAdd('project', Id) else: self.FavoriteRemove('project', Id) def Severities(self): """ input: None output: Id: integer # ID важности Name: string # Название """ uri = '{0}list.api'.format(self.SEVERITY) return self._GetData(uri) def Employees(self, Department=0, OrderBy='name', OrderDir='asc'): """ input: Department=0: integer # Id отдела OrderBy='name': string = ('name', 'department', 'position') # Параметр для сортировки OrderDir='asc': string = ('asc', 'desc') # Направление сортировки output: employees<array>: Id: integer # ID сотрудника Name: string # Полное имя LastName: string # Фамилия FirstName: string # Имя MiddleName: string # Отчество Position: object(Id, Name) # Должность Department: object(Id, Name) # Отдел Phones: array # Телефоны Email: string # E-mail Status: object(Id, Name) # Статус TimeCreated: datetime # Время создания """ uri = '{0}list.api?Department={1}&OrderBy={2}&OrderDir={3}' uri = uri.format(self.EMPLOYEE, Department, OrderBy, OrderDir) return self._GetData(uri) def EmployeeCard(self, Id): """ input: Id: integer # ID сотрудника output: employee: Id: integer # ID сотрудника Name: string # Полное имя LastName: string # Фамилия FirstName: string # Имя MiddleName: string # Отчество Gender: string # Пол Position: object(Id, Name) # Должность Department: object(Id, Name) # Отдел Birthday: date # Дата рождения HideMyBirthday: boolean # Скрывать дату рождения Age: integer # Возраст Phones: array # Телефоны Email: string # E-mail Icq: string # ICQ Skype: string # Skype Jabber: string # Jabber Address: object(Id, City, Street, House) # Адрес Behaviour: string # График работы Inn: string # ИНН PassportData: string # Паспортные данные AboutMe: string # О себе ChiefsWithoutMe: array<object>(Id, Name) # Начальники SubordinatesWithoutMe: array<object>(Id, Name) # Подчиненные Coordinators: array<object>(Id, Name) # Координаторы Status: object(Id, Name) # Статус AppearanceDay: date # Дата принятия на работу FireDay: date # Дата увольнения TimeCreated: datetime # Время создания Avatar: string # Адрес аватара сотрудника Photo: string # Адрес большого фото сотрудника """ uri = '{0}card.api?Id={1}'.format(self.EMPLOYEE, Id) return self._GetData(uri) def EmployeeCreate(self, **kwargs): """ input: kwargs: Model[LastName]: string # Фамилия Model[FirstName]: string # Имя Model[MiddleName]: string # Отчество Model[Gender]: string = ('male', 'femail') # Пол Model[Position]: string # Должность. Обязательный параметр Model[Birthday]: date # Дата рождения Model[HideMyBirthday]: boolean # Скрывать дату рождения Model[Email]: string # E-mail Model[Icq]: string # ICQ Model[Skype]: string # Skype Model[Jabber]: string # Jabber Model[Behaviour]: string # График работы Model[PassportData]: string # Паспортные данные Model[Inn]: string # ИНН Model[AboutMe]: string # О себе Model[Status]: string = ('in-office', 'out-of-office') # Статус Model[AppearanceDay]: date # Дата принятия на работу Address[City]: string # Город Address[Street]: string # Улица Address[House]: string # Дом output: Id: integer # ID сотрудника Name: string # Имя сотрудника """ uri = '{0}create.api'.format(self.EMPLOYEE) return self._GetData(uri, kwargs) def EmployeeEdit(self, Id, **kwargs): """ input: Id: integer # ID сотрудника. Обязательный параметр kwargs: Model[LastName]: string # Фамилия Model[FirstName]: string # Имя Model[MiddleName]: string # Отчество Model[Gender]: string = ('male', 'femail') # Пол Model[Position]: string # Должность Model[Birthday]: date # Дата рождения Model[HideMyBirthday]: boolean # Скрывать дату рождения Model[Email]: string # E-mail Model[Icq]: string # ICQ Model[Skype]: string # Skype Model[Jabber]: string # Jabber Model[Behaviour]: string # График работы Model[PassportData]: string # Паспортные данные Model[Inn]: string # ИНН Model[AboutMe]: string # О себе Model[Status]: string = ('in-office', 'out-of-office') # Статус Model[AppearanceDay]: date # Дата принятия на работу Address[City]: string # Город Address[Street]: string # Улица Address[House]: string # Дом output: None """ uri = '{0}edit.api'.format(self.EMPLOYEE) kwargs['Id'] = str(Id) self._GetData(uri, kwargs) def EmployeeAvailableActions(self, Id): """ input: Id: integer # ID сотрудника. Обязательный параметр output: actions: array actions: act_can_fire - уволить act_edit - редактировать """ uri = '{0}availableActions.api?Id={1}'.format(self.EMPLOYEE, Id) return self._GetData(uri) def Departments(self): """ input: None output: departments<array>: Id: integer # ID отдела Name: string # Название отдела Head: object(Id, Name) # Начальник отдела Employees: array<object(Id, Name)> # Список сотрудников отдела EmployeesCount: integer # Количество сотрудников в отделе """ uri = '{0}list.api'.format(self.DEPARTMENT) return self._GetData(uri) def Comments(self, SubjectType, SubjectId, Order='asc'): """ input: SubjectType: string = ('task' (задача), 'project' (проект)) # Тип комментируемого объекта SubjectId: integer # ID комментируемого объекта Order='asc': string = ('asc' (по возрастанию), 'desc' (по убыванию)) # Направление сортировки по дате (по умолчанию asc) output: comments<array>: Id: integer # ID комментария Text: string # Текст комментария Work: integer # Кол-во потраченных минут, которое приплюсовано к комментируемому объекту (задаче или проекту) WorkDate: date # Дата, на которую списаны потраченные часы TimeCreated: datetime # Время создания Author: object(Id, Name) # Автор комментария (сотрудник) Avatar: string # Адрес аватара автора """ if (SubjectType not in self._SubjectType) or \ (Order not in self._OrderType): raise AttributeError('Invalid parameter value') uri = '{0}list.api?SubjectType={1}&SubjectId={2}&Order={3}' uri = uri.format(self.COMMENT, SubjectType, SubjectId, Order) return self._GetData(uri) def CommentCreate(self, SubjectType, SubjectId, **kwargs): """ input: SubjectType: string = ('task' (задача), 'project' (проект)) # Тип комментируемого объекта SubjectId: integer # ID комментируемого объекта kwargs: Model[Text]: string # Текст комментария Model[Work]: integer # Кол-во потраченных часов, которое приплюсуется к комментируемому объекту (задача или проект) Model[WorkDate]: date # Дата, на которую списывать потраченные часы Model[Attaches]: array # Приложенный файл, должен передаваться POST-запросом Model[Attaches][Content]: string # Данные(контент файла), закодированные с использованием MIME base64 Model[Attaches][Name]: string # Имя файла (будет фигурировать при выводе комментария) output: Id: integer # ID комментария Text: string # Текст комментария Work: integer # Кол-во потраченных минут, которое приплюсуется к комментируемому объекту (задача или проект) WorkDate: date # Дата, на которую списывать потраченные часы TimeCreated: datetime # Время создания """ if SubjectType not in self._SubjectType: raise AttributeError('Invalid parameter value') uri = '{0}create.api'.format(self.COMMENT) kwargs['SubjectId'] = str(SubjectId) kwargs['SubjectType'] = SubjectType return self._GetData(uri, kwargs) def Favorites(self): """ input: None output: Tasks<array>: Tasks: array # Список задач, см. структуру в "Список задач" Projects: array # Список проектов, см. структуру в "Список проектов" """ uri = '{0}list.api'.format(self.FAVORITE) return self._GetData(uri) def FavoriteAdd(self, SubjectType, SubjectId): """ input: SubjectType: string = ('task' (задача), 'project' (проект)) # Тип объекта SubjectId: integer # ID объекта output: None """ if SubjectType not in self._SubjectType: raise AttributeError('Invalid parameter value') uri = '{0}add.api'.format(self.FAVORITE) params = {'SubjectId': str(SubjectId), 'SubjectType': SubjectType} self._GetData(uri, params) def FavoriteRemove(self, SubjectType, SubjectId): """ input: SubjectType: string = ('task' (задача), 'project' (проект)) # Тип объекта SubjectId: integer # ID объекта output: None """ if SubjectType not in self._SubjectType: raise AttributeError('Invalid parameter value') uri = '{0}remove.api'.format(self.FAVORITE) params = {'SubjectId': str(SubjectId), 'SubjectType': SubjectType} self._GetData(uri, params) def Search(self, qs): """ input: qs: string # Текст для поиска # Если параметр qs не указан либо пустой, то будет возвращена ошибка # "Empty query". Если же, результатов соответствующих запросу # не найдено, то в выходных данных будет возвращена ошибка # "No results". output: Employees: array # Список сотрудников, см. структуру в "Список сотрудников" Tasks: array # Список задач, см. структуру в "Список задач" Projects: array # Список проектов, см. структуру в "Список проектов" """ uri = '{0}quick.api'.format(self.SEARCH) params = {'qs': qs} return self._GetData(uri, params) def Notifications(self): """ input: None output: notifications<array>: Id: integer # ID уведомления Subject: object(Id,Name,Type) # Предмет уведомления (см. пояснение ниже) Content: string или object(Subject,Text,Author) # Содержимое уведомления (см. пояснение ниже) TimeCreated: datetime # Время создания уведомления # см. http://wiki.megaplan.ru/API_notifications Subject - это модель данных, с которой связано уведомление (задача, проект, сотрудник или комментарий). Если, например, уведомление о том, что поставлена задача, то в Subject будет идентификатор задачи. Subject содержит следующие аттрибуты: Id - идентификатор модели Name - название модели Type - тип модели (task/project/employee/comment). Структура Content зависит от типа Subject. Для всех типов, кроме комментариев, это простой текст уведомления. В случае с комментариями Content содержит следующие аттрибуты: Subject - предмет комментирования (задача или проект) с вложенной структурой, аналогичной Subject в самом уведомлении Text - текст комментария Author - автор комментария (Id и Name) """ uri = '{0}notifications.api'.format(self.INFORMER) return self._GetData(uri) def NotificationDeactivate(self, Id): """ input: Id: integer # ID уведомления. Обязательный параметр output: None """ uri = '{0}deactivateNotification.api'.format(self.INFORMER) params = {'Id': str(Id)} self._GetData(uri, params) def Approvals(self): """ input: None output: Tasks: array # Список задач, см. структуру в "Список задач" Projects: array # Список проектов, см. структуру в "Список проектов" """ uri = '{0}approvals.api'.format(self.INFORMER) return self._GetData(uri) def TodoLists(self): """ input: None output: Id: integer # Id списка дел Name: string # Название списка дел TodoCount: integer # Количество незавершенных дел в списке """ uri = '{0}list.api'.format(self.TODOLIST) return self._GetData(uri) def TodoListCreate(self, Name): """ input: Name: string # Название списка дел output: Id: integer # Id созданного списка """ uri = '{0}create.api'.format(self.TODOLIST) params = {'Name': Name} return self._GetData(uri, params) def TodoListEdit(self, Id, Name): """ input: Id: integer # Id списка дел Name: string # Новое имя списка output: Id: integer # Id измененного списка """ uri = '{0}edit.api'.format(self.TODOLIST) params = {'Id': Id, 'Name': Name} return self._GetData(uri, params) def TodoListDelete(self, Id): """ input: Id: integer # Id списка дел output: Id: integer # Id удаленного списка """ uri = '{0}delete.api'.format(self.TODOLIST) params = {'Id': Id} return self._GetData(uri, params) def Events(self): """ input: None output: Id: integer # Id события Name: string # Название события TimeCreated: datetime # Дата и время создания StartTime: datetime# Начало события Duration: integer # Продолжительность события IsPersonal: boolean # Личное дело? EventCategory: string # Категория события Participants: array # Список участников Contractors: array # Список контрагентов HasTodo: boolean # Имеет дела? HasCommunication: boolean # Имеет коммуникации? """ uri = '{0}list.api'.format(self.EVENT) return self._GetData(uri) def FromOnlineStoreCreate(self, CommerceInfo): """ input: CommerceInfo: string # Данные в формате CommerceML 2. output: Deals: array # Идентификаторы сделок, созданные в системе """ uri = '{0}createFromOnlineStore.api'.format(self.DEAL) params = {'CommerceInfo': CommerceInfo} return self._GetData(uri, params)
class Metrika(object): """ Class for the API of Yandex Metrika """ HOST = "https://api-metrika.yandex.ru/" OAUTH_TOKEN = "https://oauth.yandex.ru/token" _COUNTERS = "counters" _COUNTER = "counter/%d" _GOALS = _COUNTER + "/goals" _GOAL = _COUNTER + "/goal/%d" _FILTERS = _COUNTER + "/filters" _FILTER = _COUNTER + "/filter/%d" _OPERATIONS = _COUNTER + "/operations" _OPERATION = _COUNTER + "/operation/%d" _GRANTS = _COUNTER + "/grants" _GRANT = _COUNTER + "/grant/%s" _DELEGATES = "delegates" _DELEGATE = "delegate/%s" _ACCOUNTS = "accounts" _ACCOUNT = "account/%s" _STAT = "stat" _STAT_TRAFFIC = _STAT + "/traffic" _STAT_TRAFFIC_SUMMARY = _STAT_TRAFFIC + "/summary" _STAT_TRAFFIC_DEEPNESS = _STAT_TRAFFIC + "/deepness" _STAT_TRAFFIC_HOURLY = _STAT_TRAFFIC + "/hourly" _STAT_TRAFFIC_LOAD = _STAT_TRAFFIC + "/load" _STAT_SOURCES = _STAT + "/sources" _STAT_SOURCES_SUMMARY = _STAT_SOURCES + "/summary" _STAT_SOURCES_SITES = _STAT_SOURCES + "/sites" _STAT_SOURCES_SEARCH_ENGINES = _STAT_SOURCES + "/search_engines" _STAT_SOURCES_PHRASES = _STAT_SOURCES + "/phrases" _STAT_SOURCES_MARKETING = _STAT_SOURCES + "/marketing" _STAT_SOURCES_DIRECT = _STAT_SOURCES + "/direct" _STAT_SOURCES_DIRECT_SUMMARY = _STAT_SOURCES_DIRECT + "/summary" _STAT_SOURCES_DIRECT_PLATFORMS = _STAT_SOURCES_DIRECT + "/platforms" _STAT_SOURCES_DIRECT_REGIONS = _STAT_SOURCES_DIRECT + "/regions" _STAT_SOURCES_TAGS = _STAT_SOURCES + "/tags" _STAT_CONTENT = _STAT + "/content" _STAT_CONTENT_POPULAR = _STAT_CONTENT + "/popular" _STAT_CONTENT_ENTRANCE = _STAT_CONTENT + "/entrance" _STAT_CONTENT_EXIT = _STAT_CONTENT + "/exit" _STAT_CONTENT_TITLES = _STAT_CONTENT + "/titles" _STAT_CONTENT_URL_PARAM = _STAT_CONTENT + "/url_param" _STAT_CONTENT_USER_VARS = _STAT_SOURCES + "/user_vars" _STAT_CONTENT_ECOMMERCE = _STAT_CONTENT + "/ecommerce" _STAT_GEO = _STAT + "/geo" _STAT_DEMOGRAPHY = _STAT + "/demography" _STAT_DEMOGRAPHY_AGE_GENDER = _STAT_DEMOGRAPHY + "/age_gender" _STAT_DEMOGRAPHY_STRUCTURE = _STAT_DEMOGRAPHY + "/structure" _STAT_TECH = _STAT + "/tech" _STAT_TECH_BROWSERS = _STAT_TECH + "/browsers" _STAT_TECH_OS = _STAT_TECH + "/os" _STAT_TECH_DISPLAY = _STAT_TECH + "/display" _STAT_TECH_MOBILE = _STAT_TECH + "/mobile" _STAT_TECH_FLASH = _STAT_TECH + "/flash" _STAT_TECH_SILVERLIGHT = _STAT_TECH + "/silverlight" _STAT_TECH_DOTNET = _STAT_TECH + "/dotnet" _STAT_TECH_JAVA = _STAT_TECH + "/java" _STAT_TECH_COOKIES = _STAT_TECH + "/cookies" _STAT_TECH_JAVASCRIPT = _STAT_TECH + "/javascript" _UserAgent = "yametrikapy" @property def UserAgent(self): return self._UserAgent @UserAgent.setter def UserAgent(self, user_agent): self._UserAgent = user_agent def __init__(self, client_id, username="", password="", token="", code=""): self._ClientId = client_id self._Username = username self._Password = password self._Token = token self._Code = code self._client = APIClient() self._client.UserAgent = self._UserAgent self._data = "" def _GetResponseObject(f): """ """ def wrapper(self): obj = JSON2Obj(self._data) if hasattr(obj, "errors"): raise APIException("\n".join([error["text"] for error in obj.errors])) if hasattr(obj, "error"): raise APIException(obj.error) return f(self, obj) return wrapper @_GetResponseObject def _AuthorizeHandle(self, obj): if hasattr(obj, "access_token"): self._Token = obj.access_token def _Authorize(self): params = {"grant_type": "authorization_code" if self._Code else "password", "client_id": self._ClientId} if self._Code: params["code"] = self._Code else: params["username"] = self._Username params["password"] = self._Password self._data = self._client.request("POST", self.OAUTH_TOKEN, params=params) self._AuthorizeHandle() def _Auth(f): def wrapper(self, *args, **kwargs): if not self._Token: self._Authorize() return f(self, *args, **kwargs) return wrapper def _GetHeaders(self): header = { "User-Agent": self._UserAgent, "Accept": "application/x-yametrika+json", "Accept-Language": "ru,en-us;q=0.7,en;q=0.3", "Accept-Encoding": "gzip,deflate", "Accept-Charset": "utf-8;q=0.7,*;q=0.7", "Keep-Alive": "300", "Connection": "keep-alive", "Authorization": "OAuth %s" % self._Token, } return header @_GetResponseObject def _ResponseHandle(self, obj): return obj @_Auth def _GetData(self, method, uri, params={}): headers = self._GetHeaders() self._data = self._client.request(method, uri, params=params, headers=headers) if self._client.Status == 400: raise BadRequestError("%d %s" % (self._client.Status, "Check your request")) if self._client.Status == 401: raise UnauthorizedError("%d: %s" % (self._client.Status, "Check your token")) if self._client.Status == 403: raise ForbiddenError("%d: %s" % (self._client.Status, "Check your access rigths to object")) if self._client.Status == 404: raise NotFoundError("%d: %s" % (self._client.Status, "Resource not found")) if self._client.Status == 405: allowed = self._client.GetHeader("Allowed") raise MethodNotAllowedError("%d: %s\nUse %s" % (self._client.Status, "Method not allowed", allowed)) return self._ResponseHandle() def _GetURI(self, methodname, params=""): uri = "%s%s.json" % (self.HOST, methodname) if params: uri += "?%s" % params return uri def GetData(self): return self._data def _get_all_pages(attr_data, *attrs): def wrapper(f): def func(self, *args, **kwargs): obj = f(self, *args, **kwargs) result = BaseClass() setattr(result, attr_data, []) attr = getattr(result, attr_data) attr.extend(getattr(obj, attr_data)) if attrs: for a in attrs: if hasattr(obj, a): setattr(result, a, getattr(obj, a)) while hasattr(obj, "links") and "next" in obj.links: obj = self._GetData("GET", obj.links["next"]) attr.extend(getattr(obj, attr_data)) return result return func return wrapper # Counters @_get_all_pages("counters") def GetCounterList(self, type="", permission="", ulogin="", field=""): """ Returns a list of existing counters available to the user """ uri = self._GetURI(self._COUNTERS) params = {"type": type, "permission": permission, "ulogin": ulogin, "field": field} return self._GetData("GET", uri, params) def GetCounter(self, id, field=""): """ Returns information about the specified counter """ uri = self._GetURI(self._COUNTER % id) params = {"field": field} return self._GetData("GET", uri, params) def AddCounter(self, name, site, **kwargs): """ Creates a counter with the specified parameters """ uri = self._GetURI(self._COUNTERS) kwargs["name"] = name kwargs["site"] = site params = {"counter": kwargs} return self._GetData("POST", uri, json.dumps(params)) def EditCounter(self, id, **kwargs): """ Modifies the data for the specified counter """ uri = self._GetURI(self._COUNTER % id) params = {"counter": kwargs} return self._GetData("PUT", uri, json.dumps(params)) def DeleteCounter(self, id): """ Removes the specified counter """ uri = self._GetURI(self._COUNTER % id) return self._GetData("DELETE", uri) def CheckCounter(self, id): """ Check counter """ uri = "%s/check" % self._COUNTER uri = self._GetURI(uri % id) return self._GetData("GET", uri) # Goals def GetCounterGoalList(self, id): """ Returns information about the goals of counter """ uri = self._GetURI(self._GOALS % id) return self._GetData("GET", uri) def GetCounterGoal(self, id, goal_id): """ Returns information about the specified goal of counter. """ uri = self._GetURI(self._GOAL % (id, goal_id)) return self._GetData("GET", uri) def AddCounterGoal(self, id, name, type, depth, conditions=[], flag=""): """ Creates the goal of counter """ uri = self._GetURI(self._GOALS % id) params = {"goal": {"name": name, "type": type, "depth": depth, "flag": flag, "conditions": conditions}} return self._GetData("POST", uri, json.dumps(params)) def EditCounterGoal(self, id, goal_id, name, type, depth, conditions=[], flag=""): """ Changes the settings specified goal of counter """ uri = self._GetURI(self._GOAL % (id, goal_id)) params = {"goal": {"name": name, "type": type, "depth": depth, "conditions": conditions, "flag": flag}} return self._GetData("PUT", uri, json.dumps(params)) def DeleteCounterGoal(self, id, goal_id): """ Removes the goal of counter """ uri = self._GetURI(self._GOAL % (id, goal_id)) return self._GetData("DELETE", uri) # Filters def GetCounterFilterList(self, id): """ Returns information about the filter of counter """ uri = self._GetURI(self._FILTERS % id) return self._GetData("GET", uri) def GetCounterFilter(self, id, filter_id): """ Returns information about the specified filter of counter """ uri = self._GetURI(self._FILTER % (id, filter_id)) return self._GetData("GET", uri) def AddCounterFilter(self, id, action, attr, type, value, status): """ Creates a filter of counter """ uri = self._GetURI(self._FILTERS % id) params = {"filter": {"action": action, "attr": attr, "type": type, "value": value, "status": status}} return self._GetData("POST", uri, json.dumps(params)) def EditCounterFilter(self, id, filter_id, action, attr, type, value, status): """ Modifies the configuration of the specified filter of counter """ uri = self._GetURI(self._FILTER % (id, filter_id)) params = {"filter": {"action": action, "attr": attr, "type": type, "value": value, "status": status}} return self._GetData("PUT", uri, json.dumps(params)) def DeleteCounterFilter(self, id, filter_id): """ Removes the filter of counter """ uri = self._GetURI(self._FILTER % (id, filter_id)) return self._GetData("DELETE", uri) # Operations def GetCounterOperationList(self, id): """ Returns information about the operations of counter """ uri = self._GetURI(self._OPERATIONS % id) return self._GetData("GET", uri) def GetCounterOperation(self, id, operation_id): """ Returns information about the specified operation of counter """ uri = self._GetURI(self._OPERATION % (id, operation_id)) return self._GetData("GET", uri) def AddCounterOperation(self, id, action, attr, value, status): """ Create an operation for counter """ uri = self._GetURI(self._OPERATIONS % id) params = {"operation": {"action": action, "attr": attr, "value": value, "status": status}} return self._GetData("POST", uri, json.dumps(params)) def EditCounterOperation(self, id, operation_id, action, attr, value, status): """ Modifies the configuration of the specified operation of counter """ uri = self._GetURI(self._OPERATION % (id, operation_id)) params = {"operation": {"action": action, "attr": attr, "value": value, "status": status}} return self._GetData("PUT", uri, json.dumps(params)) def DeleteCounterOperation(self, id, operation_id): """ Removes an operation of counter """ uri = self._GetURI(self._OPERATION % (id, operation_id)) return self._GetData("DELETE", uri) # Grants def GetCounterGrantList(self, id): """ Returns information about the permissions to manage the counter and statistics """ uri = self._GetURI(self._GRANTS % id) return self._GetData("GET", uri) def GetCounterGrant(self, id, user_login): """ Returns information about a specific permit to control the counter and statistics """ uri = self._GetURI(self._GRANT % (id, user_login)) return self._GetData("GET", uri) def AddCounterGrant(self, id, user_login, perm, comment=""): """ Creates a permission to manage the counter and statistics """ uri = self._GetURI(self._GRANTS % id) params = {"grant": {"perm": perm, "user_login": user_login, "comment": comment}} return self._GetData("POST", uri, json.dumps(params)) def EditCounterGrant(self, id, user_login, perm, comment="", comment_remove=0): """ Modifies the configuration of the specified permission to manage the counter and statistics """ uri = self._GetURI(self._GRANT % (id, user_login)) params = {"grant": {"perm": perm, "comment": comment, "comment_remove": comment_remove}} return self._GetData("PUT", uri, json.dumps(params)) def DeleteCounterGrant(self, id, user_login): """ Removes the permissions to manage the counter and statistics """ uri = self._GetURI(self._GRANT % (id, user_login)) return self._GetData("DELETE", uri) # Delegates def GetDelegates(self): """ Returns list of delegates who have been granted full access to the account of the current user """ uri = self._GetURI(self._DELEGATES) return self._GetData("GET", uri) def AddDelegate(self, user_login, comment=""): """ Modifies the list of delegates for the current user account """ uri = self._GetURI(self._DELEGATES) params = {"delegate": {"user_login": user_login, "comment": comment}} return self._GetData("POST", uri, json.dumps(params)) def EditDelegates(self, delegates): """ Adds a user login in the list of delegates for the current account """ uri = self._GetURI(self._DELEGATES) params = {"delegates": delegates} return self._GetData("PUT", uri, json.dumps(params)) def DeleteDelegate(self, user_login): """ Removes the user's login from the list of delegates for the current account """ uri = self._GetURI(self._DELEGATE % user_login) return self._GetData("DELETE", uri) # Accounts def GetAccounts(self): """ Returns a list of accounts, the delegate of which is the current user """ uri = self._GetURI(self._ACCOUNTS) return self._GetData("GET", uri) def EditAccounts(self, accounts): """ Modifies the list of accounts whose delegate is the current user. Account list is updated in accordance with the list of usernames input structure. ! If the input structure does not specify a login user delegated by the current user, full access to the this user account will be revoked. ! If the input structure of the specified user's login, not included in the current list of accounts, full access to the account of this user NOT available. """ uri = self._GetURI(self._ACCOUNTS) params = {"accounts": accounts} return self._GetData("PUT", uri, json.dumps(params)) def DeleteAccount(self, user_login): """ Removes the user's login from the list of accounts, which are delegate is the current user. ! When you delete a user name from the list of accounts full access to your account will be revoked. """ uri = self._GetURI(self._ACCOUNT % user_login) return self._GetData("DELETE", uri) # Statistics @_get_all_pages("data", "totals") def GetStatTrafficSummary(self, id, goal_id=None, date1="", date2="", group="day", per_page=100): """ Returns data about traffic of site """ uri = self._GetURI(self._STAT_TRAFFIC_SUMMARY) params = {"id": id, "date1": date1, "date2": date2, "group": group, "per_page": str(per_page)} if not goal_id is None: params["goal_id"] = str(goal_id) return self._GetData("GET", uri, params) def GetStatTrafficDeepness(self, id, goal_id=None, date1="", date2=""): """ Returns data on the number of pages viewed and time visitors spent on the site """ uri = self._GetURI(self._STAT_TRAFFIC_DEEPNESS) params = {"id": id, "date1": date1, "date2": date2} if not goal_id is None: params["goal_id"] = goal_id return self._GetData("GET", uri, params) def GetStatTrafficHourly(self, id, goal_id=None, date1="", date2=""): """ Returns data on the distribution of traffic on the site by time of day, for each hourly of period """ uri = self._GetURI(self._STAT_TRAFFIC_HOURLY) params = {"id": id, "date1": date1, "date2": date2} if not goal_id is None: params["goal_id"] = goal_id return self._GetData("GET", uri, params) @_get_all_pages("data", "totals") def GetStatTrafficLoad(self, id, date1="", date2="", group="day", per_page=100): """ Returns the maximum number of requests (alarms counter) per second and the maximum number of online visitors each day selected time period """ uri = self._GetURI(self._STAT_TRAFFIC_LOAD) params = {"id": id, "date1": date1, "date2": date2, "group": group, "per_page": per_page} return self._GetData("GET", uri, params) def GetStatSourcesSummary(self, id, goal_id=None, date1="", date2="", sort="visits", reverse=1): """ Returns the conversion data from all sources on the site, where installed the specified counter """ uri = self._GetURI(self._STAT_SOURCES_SUMMARY) params = {"id": id, "date1": date1, "date2": date2, "sort": sort, "reverse": reverse} if not goal_id is None: params["goal_id"] = goal_id return self._GetData("GET", uri, params) @_get_all_pages("data", "totals") def GetStatSourcesSites( self, id, goal_id=None, date1="", date2="", table_mode="plain", per_page=100, sort="visits", reverse=1 ): """ Returns the conversion data from other sites on the web site, where installed the specified counter """ uri = self._GetURI(self._STAT_SOURCES_SITES) params = { "id": id, "date1": date1, "date2": date2, "table_mode": table_mode, "per_page": per_page, "sort": sort, "reverse": reverse, } if not goal_id is None: params["goal_id"] = goal_id return self._GetData("GET", uri, params) @_get_all_pages("data", "totals") def GetStatSourcesSearchEngines( self, id, goal_id=None, date1="", date2="", table_mode="plain", per_page=100, sort="visits", reverse=1 ): """ Returns the conversion data from the search engine's website, where installed the specified counter """ uri = self._GetURI(self._STAT_SOURCES_SEARCH_ENGINES) params = { "id": id, "date1": date1, "date2": date2, "table_mode": table_mode, "per_page": per_page, "sort": sort, "reverse": reverse, } if not goal_id is None: params["goal_id"] = goal_id return self._GetData("GET", uri, params) @_get_all_pages("data", "totals") def GetStatSourcesPhrases( self, id, goal_id=None, se_id=None, date1="", date2="", per_page=100, sort="visits", reverse=1 ): """ Returns information about the search phrases that visitors find link to the site with installed a counter """ uri = self._GetURI(self._STAT_SOURCES_PHRASES) params = {"id": id, "date1": date1, "date2": date2, "sort": sort, "reverse": reverse} if not goal_id is None: params["goal_id"] = goal_id if not se_id is None: params["se_id"] = se_id return self._GetData("GET", uri, params) @_get_all_pages("data", "totals") def GetStatSourcesMarketing( self, id, goal_id=None, date1="", date2="", table_mode="plain", per_page=100, sort="visits", reverse=1 ): """ Returns the conversion data from the advertising system on the site, where installed the specified counter """ uri = self._GetURI(self._STAT_SOURCES_MARKETING) params = { "id": id, "date1": date1, "date2": date2, "table_mode": table_mode, "per_page": per_page, "sort": sort, "reverse": reverse, } if not goal_id is None: params["goal_id"] = goal_id return self._GetData("GET", uri, params) @_get_all_pages("data", "totals") def GetStatSourcesDirectSummary( self, id, goal_id=None, date1="", date2="", table_mode="plain", per_page=100, sort="visits", reverse=1 ): """ Returns record of advertising campaigns Yandex.Direct, ad which visitors to a site """ uri = self._GetURI(self._STAT_SOURCES_DIRECT_SUMMARY) params = { "id": id, "date1": date1, "date2": date2, "table_mode": table_mode, "per_page": per_page, "sort": sort, "reverse": reverse, } if not goal_id is None: params["goal_id"] = goal_id return self._GetData("GET", uri, params) @_get_all_pages("data", "totals") def GetStatSourcesDirectPlatforms( self, id, goal_id=None, date1="", date2="", per_page=100, sort="visits", reverse=1 ): """ Returns a report on areas with which the transition through advertisements on the advertiser's site """ uri = self._GetURI(self._STAT_SOURCES_DIRECT_PLATFORMS) params = {"id": id, "date1": date1, "date2": date2, "per_page": per_page, "sort": sort, "reverse": reverse} if not goal_id is None: params["goal_id"] = goal_id return self._GetData("GET", uri, params) @_get_all_pages("data", "totals") def GetStatSourcesDirectRegions( self, id, goal_id=None, date1="", date2="", table_mode="plain", per_page=100, sort="visits", reverse=1 ): """ Returns information about membership of visitors who clicked on the site through advertisements to a particular geographical region """ uri = self._GetURI(self._STAT_SOURCES_DIRECT_REGIONS) params = { "id": id, "date1": date1, "date2": date2, "table_mode": table_mode, "per_page": per_page, "sort": sort, "reverse": reverse, } if not goal_id is None: params["goal_id"] = goal_id return self._GetData("GET", uri, params) @_get_all_pages("data") def GetStatSourcesTags( self, id, goal_id=None, date1="", date2="", table_mode="plain", per_page=100, sort="visits", reverse=1 ): """ Returns information about visits to a site on the links, which contain any of the four most frequently used tags: utm, openstat, from, glcid """ uri = self._GetURI(self._STAT_SOURCES_TAGS) params = { "id": id, "date1": date1, "date2": date2, "table_mode": table_mode, "per_page": per_page, "sort": sort, "reverse": reverse, } if not goal_id is None: params["goal_id"] = goal_id return self._GetData("GET", uri, params) @_get_all_pages("data") def GetStatContentPopular( self, id, mirror_id="", date1="", date2="", table_mode="plain", per_page=100, sort="visits", reverse=1 ): """ Returns attendance rating web pages in descending order of display """ uri = self._GetURI(self._STAT_CONTENT_POPULAR) params = { "id": id, "mirror_id": mirror_id, "date1": date1, "date2": date2, "table_mode": table_mode, "per_page": per_page, "sort": sort, "reverse": reverse, } return self._GetData("GET", uri, params) @_get_all_pages("data") def GetStatContentEntrance( self, id, goal_id=None, mirror_id="", date1="", date2="", table_mode="plain", per_page=100, sort="visits", reverse=1, ): """ Returns information about entry points to the site (the first pages of visits) """ uri = self._GetURI(self._STAT_CONTENT_ENTRANCE) params = { "id": id, "mirror_id": mirror_id, "date1": date1, "date2": date2, "table_mode": table_mode, "per_page": per_page, "sort": sort, "reverse": reverse, } if not goal_id is None: params["goal_id"] = goal_id return self._GetData("GET", uri, params) @_get_all_pages("data") def GetStatContentExit( self, id, mirror_id="", date1="", date2="", table_mode="plain", per_page=100, sort="visits", reverse=1 ): """ Returns information about exits from the site (the last pages of visits) """ uri = self._GetURI(self._STAT_CONTENT_EXIT) params = { "id": id, "mirror_id": mirror_id, "date1": date1, "date2": date2, "table_mode": table_mode, "per_page": per_page, "sort": sort, "reverse": reverse, } return self._GetData("GET", uri, params) @_get_all_pages("data", "totals") def GetStatContentTitles(self, id, date1="", date2="", per_page=100, sort="visits", reverse=1): """ Returns the rating of attendance page of the site showing their titles (from the tag title) """ uri = self._GetURI(self._STAT_CONTENT_TITLES) params = {"id": id, "date1": date1, "date2": date2, "per_page": per_page, "sort": sort, "reverse": reverse} return self._GetData("GET", uri, params) @_get_all_pages("data") def GetStatContentUrlParam( self, id, date1="", date2="", table_mode="plain", per_page=100, sort="visits", reverse=1 ): """ Returns data about the parameters mentioned Metrika in the URL visited site pages """ uri = self._GetURI(self._STAT_CONTENT_URL_PARAM) params = { "id": id, "date1": date1, "date2": date2, "table_mode": table_mode, "per_page": per_page, "sort": sort, "reverse": reverse, } return self._GetData("GET", uri, params) @_get_all_pages("data") def GetStatContentUserVars( self, id, goal_id=None, date1="", date2="", table_mode="plain", per_page=100, sort="visits", reverse=1 ): """ Returns information about the user parameters, passed through the counter code """ uri = self._GetURI(self._STAT_CONTENT_USER_VARS) params = { "id": id, "date1": date1, "date2": date2, "table_mode": table_mode, "per_page": per_page, "sort": sort, "reverse": reverse, } if not goal_id is None: params["goal_id"] = goal_id return self._GetData("GET", uri, params) @_get_all_pages("data") def GetStatContentECommerce( self, id, goal_id, date1="", date2="", table_mode="plain", per_page=100, sort="visits", reverse=1 ): """ Returns information about ordering from an online store, passed through the counter code """ uri = self._GetURI(self._STAT_CONTENT_ECOMMERCE) params = { "id": id, "goal_id": goal_id, "date1": date1, "date2": date2, "table_mode": table_mode, "per_page": per_page, "sort": sort, "reverse": reverse, } return self._GetData("GET", uri, params) @_get_all_pages("data", "totals") def GetStatGeo( self, id, goal_id=None, date1="", date2="", table_mode="plain", per_page=100, sort="visits", reverse=1 ): """ Returns information about users belonging to the geographical regions. List of regions can be grouped by regions, countries and continents. """ uri = self._GetURI(self._STAT_GEO) params = { "id": id, "date1": date1, "date2": date2, "table_mode": table_mode, "per_page": per_page, "sort": sort, "reverse": reverse, } if not goal_id is None: params["goal_id"] = goal_id return self._GetData("GET", uri, params) def GetStatDemographyAgeGender(self, id, goal_id=None, date1="", date2=""): """ Returns the data separately by sex and age of visitors """ uri = self._GetURI(self._STAT_DEMOGRAPHY_AGE_GENDER) params = {"id": id, "date1": date1, "date2": date2} if not goal_id is None: params["goal_id"] = goal_id return self._GetData("GET", uri, params) def GetStatDemographyStructure(self, id, goal_id=None, date1="", date2=""): """ Returns merged data by sex and age """ uri = self._GetURI(self._STAT_DEMOGRAPHY_STRUCTURE) params = {"id": id, "date1": date1, "date2": date2} if not goal_id is None: params["goal_id"] = goal_id return self._GetData("GET", uri, params) @_get_all_pages("data", "totals") def GetStatTechBrowsers( self, id, goal_id=None, date1="", date2="", table_mode="plain", per_page=100, sort="visits", reverse=1 ): """ Returns data about the visitor's browser """ uri = self._GetURI(self._STAT_TECH_BROWSERS) params = { "id": id, "date1": date1, "date2": date2, "table_mode": table_mode, "per_page": per_page, "sort": sort, "reverse": reverse, } if not goal_id is None: params["goal_id"] = goal_id return self._GetData("GET", uri, params) @_get_all_pages("data", "totals") def GetStatTechOs( self, id, goal_id=None, date1="", date2="", table_mode="plain", per_page=100, sort="visits", reverse=1 ): """ Returns data about the operating systems of visitors """ uri = self._GetURI(self._STAT_TECH_OS) params = { "id": id, "date1": date1, "date2": date2, "table_mode": table_mode, "per_page": per_page, "sort": sort, "reverse": reverse, } if not goal_id is None: params["goal_id"] = goal_id return self._GetData("GET", uri, params) @_get_all_pages("data", "totals", "data_group") def GetStatTechDisplay( self, id, goal_id=None, date1="", date2="", table_mode="plain", per_page=100, sort="visits", reverse=1 ): """ Returns data on the display resolution of site visitors """ uri = self._GetURI(self._STAT_TECH_DISPLAY) params = { "id": id, "date1": date1, "date2": date2, "table_mode": table_mode, "per_page": per_page, "sort": sort, "reverse": reverse, } if not goal_id is None: params["goal_id"] = goal_id return self._GetData("GET", uri, params) @_get_all_pages("data", "totals", "data_group") def GetStatTechMobile( self, id, goal_id=None, date1="", date2="", table_mode="plain", per_page=100, sort="visits", reverse=1 ): """ Returns data about visitors who come to the site from mobile devices """ uri = self._GetURI(self._STAT_TECH_MOBILE) params = { "id": id, "date1": date1, "date2": date2, "table_mode": table_mode, "per_page": per_page, "sort": sort, "reverse": reverse, } if not goal_id is None: params["goal_id"] = goal_id return self._GetData("GET", uri, params) @_get_all_pages("data", "totals") def GetStatTechFlash( self, id, goal_id=None, date1="", date2="", table_mode="plain", per_page=100, sort="visits", reverse=1 ): """ Returns data about the versions of Flash-plugin on visitors' computers """ uri = self._GetURI(self._STAT_TECH_FLASH) params = { "id": id, "date1": date1, "date2": date2, "table_mode": table_mode, "per_page": per_page, "sort": sort, "reverse": reverse, } if not goal_id is None: params["goal_id"] = goal_id return self._GetData("GET", uri, params) @_get_all_pages("data", "totals") def GetStatTechSilverlight( self, id, goal_id=None, date1="", date2="", table_mode="plain", per_page=100, sort="visits", reverse=1 ): """ Returns data on the distribution of different versions of plugin Silverlight """ uri = self._GetURI(self._STAT_TECH_SILVERLIGHT) params = { "id": id, "date1": date1, "date2": date2, "table_mode": table_mode, "per_page": per_page, "sort": sort, "reverse": reverse, } if not goal_id is None: params["goal_id"] = goal_id return self._GetData("GET", uri, params) @_get_all_pages("data", "totals") def GetStatTechDotNet( self, id, goal_id=None, date1="", date2="", table_mode="plain", per_page=100, sort="visits", reverse=1 ): """ Returns version information .NET framework on visitors' computers """ uri = self._GetURI(self._STAT_TECH_DOTNET) params = { "id": id, "date1": date1, "date2": date2, "table_mode": table_mode, "per_page": per_page, "sort": sort, "reverse": reverse, } if not goal_id is None: params["goal_id"] = goal_id return self._GetData("GET", uri, params) def GetStatTechJava(self, id, goal_id=None, date1="", date2="", sort="visits", reverse=1): """ Returns data on the availability of the Java platform on visitors' computers """ uri = self._GetURI(self._STAT_TECH_JAVA) params = {"id": id, "date1": date1, "date2": date2, "sort": sort, "reverse": reverse} if not goal_id is None: params["goal_id"] = goal_id return self._GetData("GET", uri, params) def GetStatTechCookies(self, id, goal_id=None, date1="", date2="", sort="visits", reverse=1): """ Returns data about visits visitors with disabled Cookies """ uri = self._GetURI(self._STAT_TECH_COOKIES) params = {"id": id, "date1": date1, "date2": date2, "sort": sort, "reverse": reverse} if not goal_id is None: params["goal_id"] = goal_id return self._GetData("GET", uri, params) def GetStatTechJavascript(self, id, goal_id=None, date1="", date2="", sort="visits", reverse=1): """ Returns data about visits visitors with disabled JavaScript (ECMAScript) """ uri = self._GetURI(self._STAT_TECH_JAVASCRIPT) params = {"id": id, "date1": date1, "date2": date2, "sort": sort, "reverse": reverse} if not goal_id is None: params["goal_id"] = goal_id return self._GetData("GET", uri, params)
def __init__(self, key_file=None, cert_file=None): self._client = APIClient(key_file, cert_file) self._client.UserAgent = self._UserAgent self._data = ''
class Direct(object): _VersionAPI = 4 HOST = 'https://soap.direct.yandex.ru/json-api/v4/' Locale = 'en' _UserAgent = 'yadirectpy' @property def UserAgent(self): return self._UserAgent @UserAgent.setter def UserAgent(self, user_agent): self._UserAgent = user_agent @property def VersionAPI(self): return self._VersionAPI @VersionAPI.setter def VersionAPI(self, version_api): self._VersionAPI = version_api self.HOST = 'https://soap.direct.yandex.ru/json-api/v%d/' % version_api def __init__(self, key_file=None, cert_file=None): self._client = APIClient(key_file, cert_file) self._client.UserAgent = self._UserAgent self._data = '' def _GetResponseObject(f): def wrapper(self): obj = JSON2Obj(self._data) if hasattr(obj, 'error_code'): raise APIException(obj.error_code, obj.error_str, obj.error_detail) return f(self, obj) return wrapper def _GetHeaders(self): header = { 'User-Agent': self._UserAgent, 'Accept': 'application/json', 'Accept-Language': 'ru,en-us;q=0.7,en;q=0.3', 'Accept-Encoding': 'gzip,deflate', 'Accept-Charset': 'utf-8;q=0.7,*;q=0.7', 'Keep-Alive': '300', 'Connection': 'keep-alive' } return header @_GetResponseObject def _ResponseHandle(self, obj): return obj def _GetData(self, method, uri, params={}): headers = self._GetHeaders() self._data = self._client.Request(method, uri, params=params, headers=headers) return self._ResponseHandle() def GetData(self): return self._data def _GetParams(self, method, params): return {'method': method, 'param': params, 'locale': self.Locale} def _Method(self, **kwargs): if kwargs.get('params'): params = self._GetParams(self.method, kwargs['params']) else: params = self._GetParams(self.method, kwargs) return self._GetData('POST', self.HOST, json.dumps(params, ensure_ascii=False)) def __getattr__(self, attr): self.method = attr return self._Method
def setUp(self): self.client = APIClient() self.hotel = Hotel()