| 21 | response.raise_for_status() |
| 22 | |
| 23 | class Api: |
| 24 | def __init__(self, api_url: Optional[str] = None, create_response_files: bool = True) -> None: |
| 25 | """ |
| 26 | Initializes the API client with a specified server URL and an option to create response files. |
| 27 | |
| 28 | :param api_url: The base URL for the API server. If not specified, defaults to "http://localhost:8000". |
| 29 | :param create_response_files: Indicates if the client should create response files for each API call. This is useful for debugging or development purposes. Defaults to True. |
| 30 | """ |
| 31 | DEFAULT_API_URL = "http://localhost:8000" |
| 32 | self._api_url = remove_after_first_slash(api_url) if api_url else DEFAULT_API_URL |
| 33 | self._create_response_files = create_response_files # Flag for creating response files |
| 34 | if not self.is_api_running(): |
| 35 | raise ApiException(f"API at {self._api_url} is not running. Please check if the API is up and running.") |
| 36 | |
| 37 | def _write_json(self, filename, data): |
| 38 | """ |
| 39 | Writes data to a JSON file specified by the filename. This method only runs if the create_response_files flag is True. |
| 40 | |
| 41 | :param filename: The filename for the JSON file to be created. |
| 42 | :param data: The data to be written to the file. |
| 43 | """ |
| 44 | if self._create_response_files: |
| 45 | path = _create_filename(filename) |
| 46 | write_json_response(path, data) |
| 47 | print(f"View {filename} response at: ./{path}") |
| 48 | |
| 49 | def _make_api_url(self, path): |
| 50 | return f"{self._api_url}/{path}" |
| 51 | |
| 52 | |
| 53 | def is_api_running(self) -> bool: |
| 54 | """ |
| 55 | Checks if the API is running by performing a health check on the "/api" endpoint. |
| 56 | |
| 57 | :return: True if the health check is successful, otherwise False. |
| 58 | """ |
| 59 | try: |
| 60 | response = requests.get(self._make_api_url("api")) |
| 61 | # Check if the response status code is 200 (OK) |
| 62 | return response.status_code == 200 |
| 63 | except ConnectionError: |
| 64 | raise ApiException("""API at {} is not running. |
| 65 | Check the network connection, or verify if the API is running on a different endpoint. In case the API is running on a different endpoint, you can pass the endpoint as follows: |
| 66 | api = Api('https://example.com')""".format(self._api_url)) |
| 67 | |
| 68 | def create_async_task(self, data, scraper_name=None): |
| 69 | """ |
| 70 | Submits an asynchronous task to the server. |
| 71 | |
| 72 | :param data: The data to be processed by the task. |
| 73 | :param scraper_name: The name of the scraper to use for the task. If not provided, the server will use the default scraper. |
| 74 | :return: The created task object. |
| 75 | """ |
| 76 | url = self._make_api_url("api/tasks/create-task-async") |
| 77 | payload = { |
| 78 | "data": data, |
| 79 | "scraper_name": scraper_name, |
| 80 | } |
nothing calls this directly
no outgoing calls
no test coverage detected