Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: cms/translations/xtm/xtm_api.py

Issue 29908581: Issue #7036 - [XTM Integration] Add support for providing workflow name (Closed)
Patch Set: Created Oct. 12, 2018, 2:03 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 # This file is part of the Adblock Plus web scripts, 1 # This file is part of the Adblock Plus web scripts,
2 # Copyright (C) 2006-present eyeo GmbH 2 # Copyright (C) 2006-present eyeo GmbH
3 # 3 #
4 # Adblock Plus is free software: you can redistribute it and/or modify 4 # Adblock Plus is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 3 as 5 # it under the terms of the GNU General Public License version 3 as
6 # published by the Free Software Foundation. 6 # published by the Free Software Foundation.
7 # 7 #
8 # Adblock Plus is distributed in the hope that it will be useful, 8 # Adblock Plus is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 class XTMCloudAPI(object): 54 class XTMCloudAPI(object):
55 55
56 _AUTHORIZATION_TMP = 'XTM-Basic {0}' 56 _AUTHORIZATION_TMP = 'XTM-Basic {0}'
57 57
58 class _UrlPaths: 58 class _UrlPaths:
59 CREATE = 'projects' 59 CREATE = 'projects'
60 DOWNLOAD = 'projects/{0}/files/download' 60 DOWNLOAD = 'projects/{0}/files/download'
61 UPLOAD = 'projects/{0}/files/upload' 61 UPLOAD = 'projects/{0}/files/upload'
62 GET_TARGET_LANG = 'projects/{0}/metrics' 62 GET_TARGET_LANG = 'projects/{0}/metrics'
63 ADD_TARGET_LANG = 'projects/{0}/target-languages' 63 ADD_TARGET_LANG = 'projects/{0}/target-languages'
64 GET_WORKFLOW_IDS = 'workflows'
64 65
65 _MATCH_TYPE = { 66 _MATCH_TYPE = {
66 False: 'NO_MATCH', 67 False: 'NO_MATCH',
67 True: 'MATCH_NAMES', 68 True: 'MATCH_NAMES',
68 } 69 }
69 70
70 class _SuccessCodes: 71 class _SuccessCodes:
71 CREATE = 201 72 CREATE = 201
72 UPLOAD = 200 73 UPLOAD = 200
73 DOWNLOAD = 200 74 DOWNLOAD = 200
74 GET_TARGET_LANGS = 200 75 GET_TARGET_LANGS = 200
75 ADD_TARGET_LANGS = 200 76 ADD_TARGET_LANGS = 200
77 GET_WORKFLOW_IDS = 200
76 78
77 def __init__(self, token): 79 def __init__(self, token):
78 """Constructor. 80 """Constructor.
79 81
80 Parameters 82 Parameters
81 ---------- 83 ----------
82 token: str 84 token: str
83 Token used to authenticate with the API. 85 Token used to authenticate with the API.
84 86
85 """ 87 """
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after
381 project_id, 383 project_id,
382 ) 384 )
383 headers = {'content-type': 'application/json'} 385 headers = {'content-type': 'application/json'}
384 386
385 response = self._execute(url, data=data, headers=headers) 387 response = self._execute(url, data=data, headers=headers)
386 388
387 if response.status_code != self._SuccessCodes.ADD_TARGET_LANGS: 389 if response.status_code != self._SuccessCodes.ADD_TARGET_LANGS:
388 raise XTMCloudException(response.status_code, response.content, 390 raise XTMCloudException(response.status_code, response.content,
389 'adding target languages to project') 391 'adding target languages to project')
390 392
393 def get_workflows_by_name(self, name, exact=True):
394 """Get workflows with a specific name.
395
396 Parameters
397 ----------
398 name: str
399 The name of the workflow we're looking for.
400 exact: bool
Vasily Kuznetsov 2018/10/16 14:52:54 Do we actually use this?
Tudor Avram 2018/10/18 16:29:23 No, we don't. We're only interested in exact match
401 Whether we're looking for exact matches only or if partial ones
402 are ok, too. Default True.
403
404 Returns
405 -------
406 iterable
407 Of workflow ids that match the name.
408
409 """
410 url = self.base_url + self._UrlPaths.GET_WORKFLOW_IDS
411
412 response = self._execute(url, params={'name': name})
413
414 if response.status_code != self._SuccessCodes.GET_WORKFLOW_IDS:
415 raise XTMCloudException(response.status_code, response.content,
416 'extracting workflow ids')
417
418 if not exact:
419 return [item['id']
420 for item in json.loads(response.content.encode('utf-8'))]
421
422 valid_ids = []
423 for item in json.loads(response.content.encode('utf-8')):
424 if name.lower().replace(' ', '') == \
425 item['name'].lower().replace(' ', ''):
426 valid_ids.append(item['id'])
427 return valid_ids
428
391 429
392 def get_token(username, password, user_id): 430 def get_token(username, password, user_id):
393 """Generate an API token from username and password. 431 """Generate an API token from username and password.
394 432
395 Parameters 433 Parameters
396 ---------- 434 ----------
397 username: str 435 username: str
398 The username used to generate the token. 436 The username used to generate the token.
399 password: str 437 password: str
400 The password used to generate the token. 438 The password used to generate the token.
(...skipping 22 matching lines...) Expand all
423 headers = {'content-type': 'application/json'} 461 headers = {'content-type': 'application/json'}
424 462
425 response = requests.post(url, data=request_body, headers=headers) 463 response = requests.post(url, data=request_body, headers=headers)
426 464
427 if response.status_code == 200: 465 if response.status_code == 200:
428 return json.loads(response.text)['token'].encode() 466 return json.loads(response.text)['token'].encode()
429 467
430 raise XTMCloudException(response.status_code, 468 raise XTMCloudException(response.status_code,
431 response.text.encode('utf-8'), 469 response.text.encode('utf-8'),
432 'generating token') 470 'generating token')
OLDNEW
« no previous file with comments | « cms/translations/xtm/utils.py ('k') | tests/test_xtm_api.py » ('j') | tests/utils.py » ('J')

Powered by Google App Engine
This is Rietveld