status-react/test/appium/tests/cloudbase_test_api.py

64 lines
2.0 KiB
Python
Raw Normal View History

2022-10-20 08:07:16 +00:00
from os import environ
import json
import requests
from io import BytesIO
from time import sleep
from sauceclient import SauceClient, SauceException
2022-11-03 11:26:53 +00:00
from tests.conftest import option
2022-10-20 08:07:16 +00:00
try:
import http.client as http_client
from urllib.parse import urlencode
except ImportError:
import httplib as http_client
from urllib import urlencode
sauce_username = environ.get('SAUCE_USERNAME')
sauce_access_key = environ.get('SAUCE_ACCESS_KEY')
sauce = SauceClient(sauce_username, sauce_access_key)
2022-11-03 11:26:53 +00:00
if option.datacenter == 'us-west-1':
apibase = 'saucelabs.com'
elif option.datacenter == 'eu-central-1':
apibase = 'eu-central-1.saucelabs.com'
else:
raise NotImplementedError("Unknown SauceLabs datacenter")
2022-10-20 08:07:16 +00:00
def request(method, url, body=None, content_type='application/json'):
"""This is to monkey patch further this method in order to use apibase"""
headers = sauce.make_auth_headers(content_type)
connection = http_client.HTTPSConnection(apibase)
connection.request(method, url, body, headers=headers)
response = connection.getresponse()
data = response.read()
connection.close()
if response.status not in [200, 201]:
raise SauceException('{}: {}.\nSauce Status NOT OK'.format(
response.status, response.reason), response=response)
return json.loads(data.decode('utf-8'))
2022-11-03 11:26:53 +00:00
2022-10-20 08:07:16 +00:00
sauce.request = request
2022-11-03 11:26:53 +00:00
def upload_from_url(apk_path=str()):
2022-10-20 08:07:16 +00:00
response = requests.get(apk_path, stream=True)
response.raise_for_status()
apk_name = apk_path.split("/")[-1]
file = BytesIO(response.content)
del response
for _ in range(3):
try:
2022-11-03 11:26:53 +00:00
requests.post('https://' + apibase + '/rest/v1/storage/'
2022-10-20 08:07:16 +00:00
+ sauce_username + '/' + apk_name + '?overwrite=true',
auth=(sauce_username, sauce_access_key),
data=file,
headers={'Content-Type': 'application/octet-stream'})
break
except ConnectionError:
sleep(10)