Общие сведения можно найти в посте Python: модуль requests.
В примере ниже – пример его использования.
Функция cloud_put()
выполняет деплой данных (XML и DLL файлы) в базу данных Cassandra через API нашего приложения:
def cloud_put(logger, user, password, base_url, deploy_urls_list, deploy_files_list, deploy_names_list): """Make API request with PUT Executes session.put() for each URL and FILE in deploy_files_list and deploy_names_list lists Called in deploy_ci() function""" session = requests.Session() session.auth = (user, password) session.headers.update({'user-agent': 'RDSmanager'}) # check count in each element, must be identically logger.logger.info('Counting items for deploy...') if not len(deploy_urls_list) == len(deploy_files_list): logger.logger.info('ERROR: items in `deploy_urls_list` not equal items in `deploy_files_list`') sys.exit(1) elif not len(deploy_files_list) == len(deploy_names_list): logger.logger.info('ERROR: items in `deploy_files_list` not equal items in `deploy_names_list`') sys.exit(2) else: logger.logger.info('Count seems OK.') # checking connection try: data_check = session.get(base_url) data_check.raise_for_status() except requests.exceptions.ConnectionError as error: logger.logger.info('ERROR: %s' % error) sys.exit(5) # m_url - URL with UUID, e.g. https://www.dev.domain.com/cloudlibrary/data/4ac11ad6-a391-455c-bd6b-d205b655926b # m_file - file on local filesystem to deploy, e.g. d:RDSbuilddirPlugins_xmlsProductGroupEditor.unity3d # m_name - just module name, e.g. UnityUI for m_url, m_file, m_name in zip(deploy_urls_list, deploy_files_list, deploy_names_list): logger.logger.info('Deploying file: %snURL: %s File: %s' % (m_name, m_url, m_file)) try: with open(m_file, 'rb') as infile: data_put = session.put(m_url, data=infile) data_put.raise_for_status() logger.logger.info('Result code: %sn' % data_put.status_code) except requests.exceptions.HTTPError as error: logger.logger.info('ERROR: %s' % error) sys.exit(7) except requests.exceptions.TooManyRedirects: logger.logger.info('Authentication failed!') sys.exit(6)
Она в свою очередь вызывается из функции deploy_ci()
:
def deploy_ci(logger, rds_basedir, config_name): """Build-server deployment part""" logger.logger.info('Running CI server deploy') # set of defaults, if variables not found defaults = {'base_url': 'https://www.dev.domain.com/cloudlibrary/', 'user': 'user', 'password': 'pass', } ... # running deploy with session.put() in cloud_put() function cloud_put(logger, user, password, base_url, deploy_urls_list, deploy_files_list, deploy_names_list)
И пример работы:
... 09-Jun-2015 14:52:47 Deploying file: UnityUI 09-Jun-2015 14:52:47 URL: https://www.dev.domain.com/cloudlibrary/data/4b363b65-51f3-4f41-bf54-a4c2d80e7c88 File: C:UsersAdministratorDocumentsInstallsbamboo-homexml-databuild-dir1310731572865-1703937DLLUnityUI.dll 09-Jun-2015 14:52:47 Result code: 200 09-Jun-2015 14:52:47 09-Jun-2015 14:52:47 Deploying file: UnityUI.xml 09-Jun-2015 14:52:47 URL: https://www.dev.domain.com/cloudlibrary/data/9594d836-8bbf-4aa1-98ad-dea27fe16629 File: C:RDSmanagerconfbuild_xmlsdebug9594d836-8bbf-4aa1-98ad-dea27fe16629 09-Jun-2015 14:52:47 Result code: 200 ...