Skip to content

Commit

Permalink
fix set_remote error and add multion_api_key param in login (MULTI-ON#49
Browse files Browse the repository at this point in the history
)
  • Loading branch information
NamanGarg20 authored Nov 18, 2023
1 parent 688c71f commit 58c61f4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
38 changes: 28 additions & 10 deletions sdks/multion-py/multion/multion.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ def verify_user(self, use_api=False):
if use_api:
if self.api_key is not None:
headers["X_MULTION_API_KEY"] = self.api_key
else:
return False
if self.token is not None:
headers["Authorization"] = f"Bearer {self.token['access_token']}"
print("HEADERS", headers)
if not headers:
return False

Expand All @@ -87,7 +88,9 @@ def verify_user(self, use_api=False):
print(f"An error occurred while verifying user: {str(response)}")
return False

def login(self, use_api=False):
def login(self, use_api=False, multion_api_key = None):
if multion_api_key:
self.api_key = multion_api_key
if use_api:
valid_api_key = self.verify_user(use_api)
if valid_api_key:
Expand All @@ -96,7 +99,7 @@ def login(self, use_api=False):
else:
self.issue_api_key()
return
print("Verify User", self.verify_user())

valid_token = self.verify_user()
if valid_token:
print("Logged in.")
Expand Down Expand Up @@ -204,14 +207,17 @@ def post(self, url, data, sessionId=None):

attempts = 0
while attempts < 5: # tries up to 5 times
response = requests.post(url, json=data, headers=headers)
try:
response = requests.post(url, json=data, headers=headers)
except requests.exceptions.RequestException as e:
print(f"Request failed due to an error: {e}")
break

if response.ok: # checks if status_code is 200-400
try:
return response.json()["response"]["data"]
except json.JSONDecodeError:
print("JSONDecodeError: The server didn't respond with valid JSON.")

break # if response is valid then exit loop
elif response.status_code == 401: # token has expired
print("Invalid token. Refreshing...")
Expand All @@ -236,7 +242,7 @@ def post(self, url, data, sessionId=None):
attempts += 1

# If we've exhausted all attempts and not returned, raise an error
if attempts == 1:
if attempts == 5:
print(f"Request failed with status code: {response.status_code}")
print(f"Response text: {response.text}")
raise Exception("Failed to get a valid response after 5 attempts")
Expand Down Expand Up @@ -375,7 +381,13 @@ def get_screenshot(self, response, height=None, width=None):
display(img)

def get_remote(self):
response = requests.get(f"{self.api_url}/is_remote")
if self.token is None and self.api_key is None:
raise Exception(
"You must log in or provide an API key before making API calls."
)

headers = self.set_headers()
response = requests.get(f"{self.api_url}/is_remote", headers=headers)
if response.status_code == 200:
data = response.json()
return data["is_remote"]
Expand All @@ -384,9 +396,15 @@ def get_remote(self):
return False

def set_remote(self, value: bool):
if self.token is None and self.api_key is None:
raise Exception(
"You must log in or provide an API key before making API calls."
)

headers = self.set_headers()
data = {"value": value}
url = f"{self.api_url}/is_remote"
response = requests.post(url, json=data)
response = requests.post(url, json=data, headers=headers)
if response.ok: # checks if status_code is 200-400
try:
data = response.json()
Expand Down Expand Up @@ -437,8 +455,8 @@ def get_api_key():


# Expose the login and post methods at the module level
def login(use_api=False):
_multion_instance.login(use_api)
def login(use_api=False, multion_api_key = None):
_multion_instance.login(use_api, multion_api_key)


def post(url, data):
Expand Down
2 changes: 1 addition & 1 deletion sdks/multion-py/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "multion"
version = "0.2.17"
version = "0.2.18"
description = "MULTION API"
authors = ["Div Garg"]
include = ["multion/.secrets.json, multion/*.py"]
Expand Down

0 comments on commit 58c61f4

Please sign in to comment.