Skip to main content

Python SDK for Grabba API

Project description

Grabba Python SDK

Grabba Python SDK provides a simple and intuitive interface for scheduling web data extraction jobs, retrieving job results, and managing your extraction workflows. All SDK types are now implemented as Pydantic BaseModels for full JSON compatibility and built-in validation.

Installation

Install the SDK using pip:

pip install grabba

Basic Setup

Import the Client and Required Types

from grabba import Grabba, Job, JobNavigationType, JobSchedulePolicy, JobTaskType

Note: All types such as Job, JobTask, etc., are now Pydantic BaseModels. This means that they support methods like .model_dump() and .json() for serialization, and Enum fields are automatically converted to their literal values during JSON encoding.

Initialize a Client Instance

grabba = Grabba(api_key="your-api-key", region="US")  # Optional: Defaults to US

Methods

extract

extract(job: Job) -> Dict

Schedules a new web data extraction job.

Parameters:

  • job: A Job object containing the extraction configuration.

Returns:

  • A dictionary containing the response status, message, and job_result.

Example:

from grabba import Job, JobSchedulePolicy, JobNavigationType, JobTaskType

job = Job(
    url="https://docs.grabba.dev/home",
    schedule={
        "policy": JobSchedulePolicy.IMMEDIATELY  # Enum values will be serialized as literals
    },
    navigation={
        "type": JobNavigationType.NONE
    },
    tasks=[{
        "type": JobTaskType.WEB_PAGE_AS_MARKDOWN,
        "options": {"only_main_content": True}
    }],
)

# Note: Since Job is a Pydantic model, you may also print its JSON representation:
print(job.json())

response = grabba.extract(job)
print(f"Job completed with status: {response['status']}")

schedule_job

schedule_job(job_id: str) -> Dict

Schedules an existing job for execution.

Parameters:

  • job_id: The ID of the job to schedule.

Returns:

  • A dictionary containing the response status, message, and job_result.

Example:

response = grabba.schedule_job("12345")
print(f"Job completed with status: {response['status']}")

get_jobs

get_jobs() -> GetJobsResponse

Retrieves a list of all jobs associated with the API key.

Returns:

  • A list of Job objects.

Example:

jobs = grabba.get_jobs()
for job in jobs:
    print(job.model_dump())

get_job

get_job(job_id: str) -> GetJobResponse

Retrieves details of a specific job by its ID.

Parameters:

  • job_id: The ID of the job to retrieve.

Returns:

  • A JobDetail object containing job details.

Example:

job = grabba.get_job("12345")
print(job.model_dump())

get_job_result

get_job_result(job_result_id: str) -> JobResult

Retrieves the results of a specific job by its result ID.

Parameters:

  • job_result_id: The ID of the job result to retrieve.

Returns:

  • A JobResult object.

Example:

result = grabba.get_job_result("67890")
print(result.model_dump())

delete_job (New in 0.0.4)

delete_job(job_id: str) -> Dict

Deletes a specific job by its ID.

Parameters:

  • job_id: The ID of the job to delete.

Returns:

  • A dictionary containing the response status and message.

Example:

response = grabba.delete_job("12345")
print(f"Job deletion status: {response['status']}")

Error Handling

The SDK throws errors for:

  • Invalid API keys
  • Failed API requests
  • Missing or invalid parameters

Example:

try:
    response = grabba.extract(job)
    if response["status"] == "success":
        print("Results data:", response["job_result"]["data"])
    else:
        print("Error message:", response["message"])
except Exception as err:
    print("Error:", err)

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.


License

This project is licensed under the MIT License. See the LICENSE file for details.


Additional Notes

  • Pydantic Serialization:
    All SDK models are now Pydantic BaseModels, so you can use .json() or .model_dump() to serialize your models with Enums automatically converted to their literal values.
  • Enum Configuration:
    In our BaseModels, we use json_encoders in the Config class to ensure Enum fields (such as JobSchedulePolicy and JobTaskType) are output as their literal values.
  • Type Safety:
    With Pydantic, all input data is validated against the model definitions, which helps catch errors early.

Feel free to adjust these examples as needed, and let us know if you have any questions or further changes!


Change Log

Version 0.0.4 (Latest)

  • Improved Job Task Handling: Enhanced task validation and error handling for better reliability.
  • New API Method - ``: Now you can delete jobs using their job_id.
  • Performance Optimizations: Improved response times by optimizing API requests and serialization.
  • Bug Fixes: Fixed minor serialization issues with nested Pydantic models.
  • Added delete_job(job_id: str) -> BaseResponse
  • Added delete_job_result(job_result_id: str) -> BaseResponse

Version 0.0.3

  • Pydantic Integration: All SDK types are now Pydantic BaseModels, enabling JSON serialization and validation.
  • Enum Field Optimization: Enums are automatically serialized to their literal values.
  • Better Error Handling: More descriptive error messages and improved exception handling.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

grabba-0.0.4.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

grabba-0.0.4-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file grabba-0.0.4.tar.gz.

File metadata

  • Download URL: grabba-0.0.4.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.12.3 Linux/6.11.0-19-generic

File hashes

Hashes for grabba-0.0.4.tar.gz
Algorithm Hash digest
SHA256 ca5256783438eff6a925a9834aa9e5885469b2c128616f6e1f9b45c6a99803aa
MD5 d7637e5c7a33ada9e32faee75fd6ddcb
BLAKE2b-256 e3c07c3b32db3715f29b42f935cba01f339eb9b7bed20a3c0eb0ff25fb0789bd

See more details on using hashes here.

File details

Details for the file grabba-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: grabba-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.12.3 Linux/6.11.0-19-generic

File hashes

Hashes for grabba-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 da68685896603795ab8cfd49f526afaf4eb7fe57abd5f0f08aaa34cea41518ea
MD5 ceedc85dc30d30d6f0bafcd42e1b172a
BLAKE2b-256 89028b8cb5fb285feb662d36e7c4f9f3a418315f57df091318b5f955a6504d0c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page