-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9f50a6b
commit 8262acc
Showing
4 changed files
with
147 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from dataclasses import dataclass | ||
from typing import Any, Union | ||
|
||
from requests import Response, Session | ||
|
||
|
||
@dataclass | ||
class AdHocExecutor: | ||
session: Session | ||
headers: dict[str, Any] | ||
base_url: str | ||
timeout: float | ||
|
||
def get_with(self, url_path: str) -> Response: | ||
return self.session.get( | ||
f"{self.base_url}/{url_path}", headers=self.headers, timeout=self.timeout | ||
) | ||
|
||
def post_with( | ||
self, url_path: str, json: Union[dict[str, Any], None] = None | ||
) -> Response: | ||
return self.session.post( | ||
f"{self.base_url}/{url_path}", | ||
json=json, | ||
headers=self.headers, | ||
timeout=self.timeout, | ||
) | ||
|
||
def patch_with( | ||
self, url_path: str, json: Union[dict[str, Any], None] = None | ||
) -> Response: | ||
return self.session.patch( | ||
f"{self.base_url}/{url_path}", | ||
json=json, | ||
headers=self.headers, | ||
timeout=self.timeout, | ||
) | ||
|
||
def put_with( | ||
self, url_path: str, json: Union[dict[str, Any], None] = None | ||
) -> Response: | ||
return self.session.put( | ||
f"{self.base_url}/{url_path}", | ||
json=json, | ||
headers=self.headers, | ||
timeout=self.timeout, | ||
) | ||
|
||
def delete_with(self, url_path: str) -> Response: | ||
return self.session.delete( | ||
f"{self.base_url}/{url_path}", headers=self.headers, timeout=self.timeout | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import os | ||
from typing import Optional | ||
|
||
from requests import Response | ||
|
||
from alice import Config, Onboarding | ||
from alice.onboarding.models.ad_hoc_executor import AdHocExecutor | ||
|
||
|
||
def onboarding_ad_hoc(api_key: str, verbose: Optional[bool] = False) -> None: | ||
config = Config(api_key=api_key, verbose=verbose) | ||
onboarding = Onboarding.from_config(config) | ||
|
||
# Create User (Backend without user_id) | ||
def create_user(executor: AdHocExecutor) -> Response: | ||
return executor.post_with(url_path="user") | ||
|
||
response = onboarding.ad_hoc(create_user).unwrap_or_raise() | ||
user_id = response.json().get("user_id") | ||
print(f"{user_id=}") | ||
|
||
# Get User State (Backend with user_id) | ||
def get_user_state(executor: AdHocExecutor) -> Response: | ||
return executor.get_with(url_path="/user/state") | ||
|
||
response = onboarding.ad_hoc(get_user_state, user_id=user_id).unwrap_or_raise() | ||
state = response.json() | ||
print(f"{state=}") | ||
|
||
|
||
if __name__ == "__main__": | ||
api_key = os.environ.get("ONBOARDING_API_KEY") | ||
if api_key is None: | ||
raise AssertionError( | ||
"Please configure your ONBOARDING_API_KEY to run the example" | ||
) | ||
print("Running onboarding example...") | ||
onboarding_ad_hoc(api_key=api_key, verbose=True) |