repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | import json |
| #2 | import logging |
| #3 | import os |
| #4 | import uuid |
| #5 | |
| #6 | import requests |
| #7 | |
| #8 | from embedchain.constants import CONFIG_DIR, CONFIG_FILE |
| #9 | |
| #10 | logger = logging.getLogger(__name__) |
| #11 | |
| #12 | |
| #13 | class Client: |
| #14 | def __init__(self, api_key=None, host="https://apiv2.embedchain.ai"): |
| #15 | self.config_data = self.load_config() |
| #16 | self.host = host |
| #17 | |
| #18 | if api_key: |
| #19 | if self.check(api_key): |
| #20 | self.api_key = api_key |
| #21 | self.save() |
| #22 | else: |
| #23 | raise ValueError( |
| #24 | "Invalid API key provided. You can find your API key on https://app.embedchain.ai/settings/keys." |
| #25 | ) |
| #26 | else: |
| #27 | if "api_key" in self.config_data: |
| #28 | self.api_key = self.config_data["api_key"] |
| #29 | logger.info("API key loaded successfully!") |
| #30 | else: |
| #31 | raise ValueError( |
| #32 | "You are not logged in. Please obtain an API key from https://app.embedchain.ai/settings/keys/" |
| #33 | ) |
| #34 | |
| #35 | @classmethod |
| #36 | def setup(cls): |
| #37 | """ |
| #38 | Loads the user id from the config file if it exists, otherwise generates a new |
| #39 | one and saves it to the config file. |
| #40 | |
| #41 | :return: user id |
| #42 | :rtype: str |
| #43 | """ |
| #44 | os.makedirs(CONFIG_DIR, exist_ok=True) |
| #45 | |
| #46 | if os.path.exists(CONFIG_FILE): |
| #47 | with open(CONFIG_FILE, "r") as f: |
| #48 | data = json.load(f) |
| #49 | if "user_id" in data: |
| #50 | return data["user_id"] |
| #51 | |
| #52 | u_id = str(uuid.uuid4()) |
| #53 | with open(CONFIG_FILE, "w") as f: |
| #54 | json.dump({"user_id": u_id}, f) |
| #55 | |
| #56 | @classmethod |
| #57 | def load_config(cls): |
| #58 | if not os.path.exists(CONFIG_FILE): |
| #59 | cls.setup() |
| #60 | |
| #61 | with open(CONFIG_FILE, "r") as config_file: |
| #62 | return json.load(config_file) |
| #63 | |
| #64 | def save(self): |
| #65 | self.config_data["api_key"] = self.api_key |
| #66 | with open(CONFIG_FILE, "w") as config_file: |
| #67 | json.dump(self.config_data, config_file, indent=4) |
| #68 | |
| #69 | logger.info("API key saved successfully!") |
| #70 | |
| #71 | def clear(self): |
| #72 | if "api_key" in self.config_data: |
| #73 | del self.config_data["api_key"] |
| #74 | with open(CONFIG_FILE, "w") as config_file: |
| #75 | json.dump(self.config_data, config_file, indent=4) |
| #76 | self.api_key = None |
| #77 | logger.info("API key deleted successfully!") |
| #78 | else: |
| #79 | logger.warning("API key not found in the configuration file.") |
| #80 | |
| #81 | def update(self, api_key): |
| #82 | if self.check(api_key): |
| #83 | self.api_key = api_key |
| #84 | self.save() |
| #85 | logger.info("API key updated successfully!") |
| #86 | else: |
| #87 | logger.warning("Invalid API key provided. API key not updated.") |
| #88 | |
| #89 | def check(self, api_key): |
| #90 | validation_url = f"{self.host}/api/v1/accounts/api_keys/validate/" |
| #91 | response = requests.post(validation_url, headers={"Authorization": f"Token {api_key}"}) |
| #92 | if response.status_code == 200: |
| #93 | return True |
| #94 | else: |
| #95 | logger.warning(f"Response from API: {response.text}") |
| #96 | logger.warning("Invalid API key. Unable to validate.") |
| #97 | return False |
| #98 | |
| #99 | def get(self): |
| #100 | return self.api_key |
| #101 | |
| #102 | def __str__(self): |
| #103 | return self.api_key |
| #104 |