Skip to main content

Official Python SDK for Lynkr

Project description

Lynkr Python SDK

PyPI version Python versions License Code style: black

Official Python SDK for the Lynkr Service. This SDK provides a simple and intuitive interface to interact with Lynkr's schema generation and action execution endpoints.

Features

  • Simple and intuitive API
  • Comprehensive error handling
  • Type hints for better IDE integration
  • JSON schema validation
  • Extensive documentation

Installation

pip install lynkr

Quick Start

import os
from lynkr.client import LynkrClient

# Set your API key directly
client = LynkrClient(api_key="your_api_key")

# Or set your API key as an environment variable
# os.environ["LYNKR_API_KEY"] = "your_api_key"
# client = LynkrClient()

# Get a schema for a natural language request
ref_id, schema = client.get_schema("Show me my current orders in my Wealthsimple account")

# Print the schema details
print(f"Reference ID: {ref_id}")
print(f"Required fields: {schema.get_required_fields()}")
print(f"Schema JSON: {schema.to_json()}")

# Fill in the schema data
schema_data = {
    "service_email": "john@example.com",
    "service_password": "veryverysecure",
}

# Validate the data against the schema
validation_errors = schema.validate(schema_data)
if validation_errors:
    print(f"Validation errors: {validation_errors}")
else:
    # Execute the action with the filled schema data
    # The ref_id from the previous call is stored in the client
    result = client.execute_action(schema_data=schema_data)
    # Or, if you want to specify a different ref_id
    # result = client.execute_action(schema_data=schema_data, ref_id=ref_id)
    print(f"Action result: {result}")

Usage

Initializing the Client

You can initialize the client by providing your API key directly or by setting it as an environment variable:

# Option 1: Pass the API key directly
client = LynkrClient(api_key="your_api_key")

# Option 2: Use environment variable
import os
os.environ["LYNKR_API_KEY"] = "your_api_key"
client = LynkrClient()

# Customize base URL (optional)
client = LynkrClient(
    api_key="your_api_key",
    base_url="https://custom-api.lynkr.ca",
    timeout=60  # Custom timeout in seconds
)

Getting a Schema

Get a schema for a natural language request:

ref_id, schema = client.get_schema("Place an order for 100 shares of GOOG on my Wealthsimple account.")

The get_schema method returns a tuple containing:

  • A reference ID string (used for the follow-up execute_action call)
  • A Schema object that provides helper methods to work with the schema

The reference ID is also stored within the client object for convenience.

Working with the Schema

The Schema object provides several useful methods:

# Get the schema as a dictionary
schema_dict = schema.to_dict()

# Get the schema as a formatted JSON string
schema_json = schema.to_json(indent=2)

# Get a list of required fields
required_fields = schema.get_required_fields()

# Get the type of a specific field
field_type = schema.get_field_type("report_format")

# Validate data against the schema
errors = schema.validate(your_data)
if not errors:
    print("Data is valid!")
else:
    print(f"Validation errors: {errors}")

Executing an Action

Once you have filled in the schema data, you can execute the action:

# Fill in the schema with the required data
schema_data = {
    "service_email": "john@example.com",
    "service_password": "veryverysecure",
    "security_id": "sec-s-76a7155242e8477880cbb43269235cb6",
    "limit_price": 5.00,
    "quantity": 100,
    "order_type": "buy_quantity",
    "order_sub_type": "limit",
    "time_in_force": "day"
}

# Execute the action using the stored ref_id from the previous get_schema call
result = client.execute_action(schema_data=schema_data)

# Or provide a specific ref_id
result = client.execute_action(schema_data=schema_data, ref_id="custom_ref_id")

# Process the result
print(f"Result: {result}")

Error Handling

The SDK uses custom exceptions to provide clear error messages:

from lynkr.client import LynkrClient
from lynkr.exceptions import ApiError, ValidationError

try:
    client = LynkrClient(api_key="invalid_key")
    ref_id, schema = client.get_schema("Some request")
except ValidationError as e:
    print(f"Validation error: {e}")
except ApiError as e:
    print(f"API error ({e.status_code}): {e.message}")

Advanced Configuration

Request Timeout

Set a custom timeout for API requests:

client = LynkrClient(
    api_key="your_api_key",
    timeout=60  # 60 seconds
)

Custom Base URL

Use a different API endpoint:

client = LynkrClient(
    api_key="your_api_key",
    base_url="https://staging-api.lynkr.ca"
)

Complete Example

Here's a complete example showing a full workflow:

from lynkr.client import LynkrClient
from lynkr.exceptions import ApiError, ValidationError

# Initialize client
client = LynkrClient(api_key="your_api_key")

try:
    # Get schema for sending an email
    ref_id, schema = client.get_schema("I want to send an email")

    # Print schema details
    print(f"Required fields: {schema.get_required_fields()}")

    # Prepare data
    schema_data = {
        "x-api-key": "your_email_provider_api_key",
        "sender_address": "noreply@example.com",
        "receiver_address": "recipient@example.com",
        "subject": "Hello from Lynkr SDK",
        "html": "<p>This is a test email sent using the Lynkr SDK</p>"
    }

    # Validate data
    errors = schema.validate(schema_data)
    if errors:
        print(f"Validation errors: {errors}")
    else:
        # Execute the action
        response = client.execute_action(schema_data=schema_data)
        print(f"Email sent successfully: {response}")

except ValidationError as e:
    print(f"Validation error: {e}")
except ApiError as e:
    print(f"API error: {e}")
    if e.status_code:
        print(f"Status code: {e.status_code}")
    if e.response:
        print(f"Response details: {e.response}")

License

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

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

lynkr-0.1.4.tar.gz (16.7 kB view details)

Uploaded Source

Built Distribution

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

lynkr-0.1.4-py3-none-any.whl (14.1 kB view details)

Uploaded Python 3

File details

Details for the file lynkr-0.1.4.tar.gz.

File metadata

  • Download URL: lynkr-0.1.4.tar.gz
  • Upload date:
  • Size: 16.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.17

File hashes

Hashes for lynkr-0.1.4.tar.gz
Algorithm Hash digest
SHA256 85822c6e2108fa521ab502f76fdd0f34c2ea450fefa4fb4ccddfd4df58919bce
MD5 ff01b88592ce7864e110580c9fe86c98
BLAKE2b-256 7c0c8c8e1d57e8b69b97a56944f18c6cc418630ae0d1a28a9ea65b6f0c033386

See more details on using hashes here.

File details

Details for the file lynkr-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: lynkr-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 14.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.17

File hashes

Hashes for lynkr-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 2685c994621326294ee4b8c85458a5b559b4d7fae9f66c400a31f20562be571f
MD5 a379258f715c0c02cd1c56483dd12dd0
BLAKE2b-256 5129ba9ccd8b22fe0cc2d2eec25c2bec88b5d68ab5805f858bd9b6a9dcb9a8fd

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