### Development Environment Setup Source: https://github.com/newsdataapi/python-client/blob/main/README.md Commands for cloning the repository and setting up the development environment using uv. ```bash git clone https://github.com/bytesview/python-client cd python-client uv sync # creates .venv, installs runtime + dev deps from uv.lock ``` -------------------------------- ### Installation with uv Source: https://github.com/newsdataapi/python-client/blob/main/README.md Installs the newsdataapi package using uv. ```bash uv add newsdataapi ``` -------------------------------- ### Installation with pip Source: https://github.com/newsdataapi/python-client/blob/main/README.md Installs the newsdataapi package using pip. ```bash pip install newsdataapi ``` -------------------------------- ### Quickstart using context manager Source: https://github.com/newsdataapi/python-client/blob/main/README.md Demonstrates basic usage of the NewsDataApiClient with a context manager to fetch latest articles about Bitcoin. ```python from newsdataapi import NewsDataApiClient with NewsDataApiClient("YOUR_API_KEY") as client: response = client.latest_api(q="bitcoin", country="us", language="en") for article in response["results"]: print(article["title"], "- ", article["link"]) ``` -------------------------------- ### Quickstart without context manager Source: https://github.com/newsdataapi/python-client/blob/main/README.md Demonstrates basic usage of the NewsDataApiClient without a context manager, requiring manual closing of the client. ```python from newsdataapi import NewsDataApiClient client = NewsDataApiClient("YOUR_API_KEY") try: response = client.latest_api(q="bitcoin", country="us", language="en") for article in response["results"]: print(article["title"], "- ", article["link"]) finally: client.close() ``` -------------------------------- ### Saving response to CSV Source: https://github.com/newsdataapi/python-client/blob/main/README.md Example of saving API response to a CSV file using the client. ```python client = NewsDataApiClient(apikey, folder_path="./out") client.save_to_csv(response, filename="latest_news") ``` -------------------------------- ### Running Tests Source: https://github.com/newsdataapi/python-client/blob/main/README.md Commands for executing unit, integration, and all tests. ```bash uv run pytest # unit tests only (default) PYTEST_TOKEN= uv run pytest -m integration # live-API tests PYTEST_TOKEN= uv run pytest -m "" # all tests ``` -------------------------------- ### NewsDataApiClient Configuration Source: https://github.com/newsdataapi/python-client/blob/main/README.md Illustrates various configuration options for the NewsDataApiClient. ```python client = NewsDataApiClient( apikey="...", request_timeout=30, # seconds; default 30 max_retries=5, # default 5 retry_backoff=2.0, # base seconds, exponential; default 2.0 retry_backoff_max=60.0, # cap on a single retry sleep; default 60.0 pagination_delay=1.0, # seconds between pages; default 1.0 max_result=None, # cap on merged results in scroll mode; default None (no cap) max_pages=None, # cap on pages yielded in paginate mode; default None (no cap) proxies={"https": "..."}, # passed to requests.Session.get accept_language="en", # Accept-Language header include_headers=False, # if True, returned dicts include response_headers base_url="...", # override for staging / proxied environments session=my_session, # inject your own requests.Session folder_path="./out", # default folder for save_to_csv; default None ) ``` -------------------------------- ### Linting and Type Checking Source: https://github.com/newsdataapi/python-client/blob/main/README.md Commands for running linter (ruff) and type checker (mypy). ```bash uv run ruff check src/ tests/ examples/ uv run mypy src/ ``` -------------------------------- ### Consuming an endpoint - Paginate Source: https://github.com/newsdataapi/python-client/blob/main/README.md Fetches news articles page by page using a generator, stopping after a specified number of pages. ```python # 3. Iterate one response per page (a generator). for page in client.latest_api(q="news", paginate=True, max_pages=5): process(page["results"]) ``` -------------------------------- ### Error handling Source: https://github.com/newsdataapi/python-client/blob/main/README.md Demonstrates how to catch various exceptions that can occur when using the NewsDataApiClient. ```python from newsdataapi import ( NewsdataAPIError, NewsdataAuthError, NewsdataNetworkError, NewsdataRateLimitError, ) try: client.latest_api(q="news") except NewsdataAuthError as e: print(f"bad API key (HTTP {e.status_code})") except NewsdataRateLimitError as e: print(f"rate limited; retry after {e.retry_after}s") except NewsdataAPIError as e: print(f"API error {e.status_code}: {e.response_body}") except NewsdataNetworkError as e: print(f"network failure: {e.original}") ``` -------------------------------- ### Standalone save_to_csv function Source: https://github.com/newsdataapi/python-client/blob/main/README.md Demonstrates importing and using the save_to_csv function directly. ```python from newsdataapi import save_to_csv save_to_csv(response, folder_path="./out", filename="latest_news") ``` -------------------------------- ### Consuming an endpoint - Auto-merge Source: https://github.com/newsdataapi/python-client/blob/main/README.md Fetches news articles and automatically merges results from multiple pages up to a specified maximum. ```python # 2. Auto-merge — follow nextPage cursors and return one combined dict. merged = client.latest_api(q="news", scroll=True, max_result=200) ``` -------------------------------- ### Consuming an endpoint - Single request Source: https://github.com/newsdataapi/python-client/blob/main/README.md Fetches news articles using a single API request. ```python # 1. Single request (the default). response = client.latest_api(q="news") ``` -------------------------------- ### Save results to CSV Source: https://github.com/newsdataapi/python-client/blob/main/README.md Saves the fetched news articles to a CSV file. ```python client.save_to_csv(response, folder_path="./out", filename="latest_news") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.