How to use assert_poeditor_response_code method in toolium

Best Python code snippet using toolium_python

poeditor.py

Source:poeditor.py Github

copy

Full Screen

...95 params = {"api_token": get_poeditor_api_token(),96 "id": project_info['id']}97 r = send_poeditor_request(ENDPOINT_POEDITOR_LIST_LANGUAGES, "POST", params, 200)98 response_data = r.json()99 assert_poeditor_response_code(response_data, "200")100 language_codes = [lang['code'] for lang in response_data['result']['languages']]101 assert not len(language_codes) == 0, "ERROR: Not languages found in POEditor"102 logger.info('POEditor languages in "%s" project: %s %s' % (project_info['name'], len(language_codes),103 language_codes))104 return language_codes105def search_terms_with_string(context=None, lang=None):106 """107 Saves POEditor terms for a given existing language in that project108 :param context: behave context (deprecated)109 :param lang: a valid language existing in that POEditor project110 :return: N/A (saves it to context.poeditor_terms)111 """112 if context:113 logger.warning('Deprecated context parameter has been sent to search_terms_with_string method. Please, '114 'configure dataset global variables instead of passing context to search_terms_with_string.')115 project_info = get_poeditor_project_info_by_name()116 language_codes = get_poeditor_language_codes(project_info)117 language = get_valid_lang(language_codes, lang)118 dataset.poeditor_terms = get_all_terms(project_info, language)119 if context:120 # Save terms in context for backwards compatibility121 context.poeditor_terms = dataset.poeditor_terms122def export_poeditor_project(project_info, lang, file_type):123 """124 Export all texts in project to a given file type125 :param project_info: project info126 :param lang: language configured in POEditor project that will be exported127 :param file_type: There are more available formats to download but only one is supported now: json128 :return: poeditor terms129 """130 assert file_type in ['json'], "Only json file type is supported at this moment"131 params = {"api_token": get_poeditor_api_token(),132 "id": project_info['id'],133 "language": lang,134 "type": file_type}135 r = send_poeditor_request(ENDPOINT_POEDITOR_EXPORT_PROJECT, "POST", params, 200)136 response_data = r.json()137 assert_poeditor_response_code(response_data, "200")138 filename = response_data['result']['url'].split('/')[-1]139 r = send_poeditor_request(ENDPOINT_POEDITOR_DOWNLOAD_FILE + '/' + filename, "GET", {}, 200)140 poeditor_terms = r.json()141 logger.info('POEditor terms in "%s" project with "%s" language: %s' % (project_info['name'], lang,142 len(poeditor_terms)))143 return poeditor_terms144def save_downloaded_file(poeditor_terms):145 """146 Saves POEditor terms to a file in output dir147 :param poeditor_terms: POEditor terms148 """149 file_path = get_poeditor_file_path()150 with open(file_path, 'w') as f:151 json.dump(poeditor_terms, f, indent=4)152 logger.info('POEditor terms have been saved in "%s" file' % file_path)153def assert_poeditor_response_code(response_data, status_code):154 """155 Check status code returned in POEditor response156 :param response_data: data received in poeditor API response as a dictionary157 :param status_code: expected status code158 """159 assert response_data['response']['code'] == status_code, f"{response_data['response']['code']} status code \160 has been received instead of {status_code} in POEditor response body. Response body: {response_data}"161def get_country_from_config_file():162 """163 Gets the country to use later from config checking if it's a valid one in POEditor164 :return: country165 """166 try:167 country = dataset.toolium_config.get('TestExecution', 'language').lower()168 except NoOptionError:169 assert False, "There is no language configured in test, add it to config or use step with parameter lang_id"170 return country171def get_valid_lang(language_codes, lang=None):172 """173 Check if language provided is a valid one configured and returns the POEditor matched lang174 :param language_codes: valid POEditor language codes175 :param lang: a language from config or from lang parameter176 :return: lang matched from POEditor177 """178 lang = lang if lang else get_country_from_config_file()179 if lang in language_codes:180 matching_lang = lang181 elif lang.split('-')[0] in language_codes:182 matching_lang = lang.split('-')[0]183 else:184 assert False, f"Language {lang} is not included in valid codes: {', '.join(language_codes)}"185 return matching_lang186def get_poeditor_projects():187 """188 Get the list of the projects configured in POEditor189 :return: POEditor projects list190 """191 params = {"api_token": get_poeditor_api_token()}192 r = send_poeditor_request(ENDPOINT_POEDITOR_LIST_PROJECTS, "POST", params, 200)193 response_data = r.json()194 assert_poeditor_response_code(response_data, "200")195 projects = response_data['result']['projects']196 projects_names = [project['name'] for project in projects]197 logger.info('POEditor projects: %s %s' % (len(projects_names), projects_names))198 return projects199def send_poeditor_request(endpoint, method, params, status_code):200 """201 Send a request to the POEditor API202 :param endpoint: endpoint path203 :param method: HTTP method to be used in the request204 :param params: parameters to be sent in the request205 :param code: expected status code206 :return: response207 """208 try:209 base_url = dataset.project_config['poeditor']['base_url']210 except KeyError:211 base_url = 'https://api.poeditor.com'212 url = f'{base_url}/{endpoint}'213 r = requests.request(method, url, data=params)214 assert r.status_code == status_code, f"{r.status_code} status code has been received instead of {status_code} \215 in POEditor response calling to {url}. Response body: {r.json()}"216 return r217def get_all_terms(project_info, lang):218 """219 Get all terms for a given language configured in POEditor220 :param project_info: project_info221 :param lang: a valid language configured in POEditor project222 :return: the list of terms223 """224 params = {"api_token": get_poeditor_api_token(),225 "id": project_info['id'],226 "language": lang}227 r = send_poeditor_request(ENDPOINT_POEDITOR_LIST_TERMS, "POST", params, 200)228 response_data = r.json()229 assert_poeditor_response_code(response_data, "200")230 terms = response_data['result']['terms']231 logger.info('POEditor terms in "%s" project with "%s" language: %s' % (project_info['name'], lang, len(terms)))232 return terms233def load_poeditor_texts(context=None):234 """235 Download POEditor texts and save in output folder if the config exists or use previously downloaded texts236 :param context: behave context (deprecated parameter)237 """238 if context:239 logger.warning('Deprecated context parameter has been sent to load_poeditor_texts method. Please, '240 'configure POEditor global variables instead of passing context to load_poeditor_texts.')241 if get_poeditor_api_token():242 # Try to get poeditor mode param from toolium config first243 poeditor_mode = dataset.toolium_config.get_optional('TestExecution', 'poeditor_mode')...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run toolium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful