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.5.tar.gz (16.8 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.5-py3-none-any.whl (14.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: lynkr-0.1.5.tar.gz
  • Upload date:
  • Size: 16.8 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.5.tar.gz
Algorithm Hash digest
SHA256 98f0d4ca4509a41ee297b21c8644378a63cc0db58306f5bcaa0e9258b0e24155
MD5 864ae3525bfe7a6bf95b475379605766
BLAKE2b-256 8a7fb7bc4b3bbd36b017680187754d1811225fe2d8b9f6d43d2d6a2d3d595a27

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lynkr-0.1.5-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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 040267c5827a07a037a1b94ce154cb5d2676d05ccd2b03c31fcf6da01edab765
MD5 2c5856c563c7b4ebb8a4c72fd29c1e72
BLAKE2b-256 3a28e562faa004f95566be76ab7dcf95bd404e0edfa87cb6ffc30748946b0dbe

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