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: Addressed comments from Patch Set 1 Created Oct. 18, 2018, 4:23 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):
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
401 Returns
402 -------
403 iterable
404 Of workflow ids that match the name provided.
405
406 """
407 url = self.base_url + self._UrlPaths.GET_WORKFLOW_IDS
408
409 response = self._execute(url, params={'name': name})
410
411 if response.status_code != self._SuccessCodes.GET_WORKFLOW_IDS:
412 raise XTMCloudException(response.status_code, response.content,
413 'extracting workflow ids')
414
415 valid_ids = []
416 for item in json.loads(response.content.encode('utf-8')):
417 if name.lower().replace(' ', '') == \
418 item['name'].lower().replace(' ', ''):
419 valid_ids.append(item['id'])
420 return valid_ids
421
391 422
392 def get_token(username, password, user_id): 423 def get_token(username, password, user_id):
393 """Generate an API token from username and password. 424 """Generate an API token from username and password.
394 425
395 Parameters 426 Parameters
396 ---------- 427 ----------
397 username: str 428 username: str
398 The username used to generate the token. 429 The username used to generate the token.
399 password: str 430 password: str
400 The password used to generate the token. 431 The password used to generate the token.
(...skipping 22 matching lines...) Expand all
423 headers = {'content-type': 'application/json'} 454 headers = {'content-type': 'application/json'}
424 455
425 response = requests.post(url, data=request_body, headers=headers) 456 response = requests.post(url, data=request_body, headers=headers)
426 457
427 if response.status_code == 200: 458 if response.status_code == 200:
428 return json.loads(response.text)['token'].encode() 459 return json.loads(response.text)['token'].encode()
429 460
430 raise XTMCloudException(response.status_code, 461 raise XTMCloudException(response.status_code,
431 response.text.encode('utf-8'), 462 response.text.encode('utf-8'),
432 'generating token') 463 '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