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.10.tar.gz (18.9 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.10-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: lynkr-0.1.10.tar.gz
  • Upload date:
  • Size: 18.9 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.10.tar.gz
Algorithm Hash digest
SHA256 e6fa38c66c31a194b01dc255a090e10052560cc7385b89073851fc7b8b843868
MD5 35f955fabb714d03e992e5ee6a462932
BLAKE2b-256 1b9012f6a78770f330df30ecbf28caca79c078f4112153eec9e50e471543de9e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lynkr-0.1.10-py3-none-any.whl
  • Upload date:
  • Size: 15.8 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.10-py3-none-any.whl
Algorithm Hash digest
SHA256 54bb9492374a99e60d5192e6be4dbd46ed54823c220f6599091b95186cedcb9a
MD5 db7de971f1d19c585ce13d2a6e666085
BLAKE2b-256 c79804d0716fb6082fcca36dfc710f77ed8e063fd12d5fdd7284173d4f3dd743

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