Skip to main content

Cliente Python para a API do StackSpot

Project description

StackSpot Client Python

A Python client library for interacting with the StackSpot API. This library provides a simple and intuitive interface to authenticate, execute commands, and retrieve results from the StackSpot platform.

Features

  • 🔐 Automatic authentication handling
  • 🔄 Automatic token refresh
  • ⚡ Retry mechanism for failed requests
  • 🛡️ Comprehensive error handling
  • 📦 Support for different response types
  • 🧪 Type hints for better IDE support

Installation

You can install the package using pip:

pip install stackspot-client

Quick Start

import os

from dotenv import load_dotenv
from stackspot_client import StackSpotConfig, StackSpotError, QuickCommands

load_dotenv()

def main():
    # Configuração do cliente
    config = StackSpotConfig(base_url=os.getenv("base_url"),auth_url=os.getenv("auth_url"),
        client_id=os.getenv("client_id"),client_secret=os.getenv("client_secret")
    )

    try:

        command = QuickCommands(config)

        execution_id = command.execute_command(
            
         # Endpoint informed when creating the quick command
        '/v1/quick-commands/create-execution/ola-mundo-',
            ' O mundo é um moinho'
        )

        result = command.get_execution_result(execution_id)

        if result:
            print("\n📋 Resultado:")
            print("=" * 50)
            print(result['steps'][0]['step_result']['answer'])
        else:
            print("❌ Falha")

    except StackSpotError as e:
        print(f"\n❌ Erro: {str(e)}")
    except Exception as e:
        print(f"\n❌ Erro inesperado: {str(e)}")
        import traceback
        print("\nDetalhes do erro:")
        print(traceback.format_exc())

if __name__ == "__main__":
    main()

Configuration

The StackSpotConfig class accepts the following parameters:

Parameter Type Required Default Description
base_url str Yes - Base URL for the StackSpot API
client_id str Yes - Your StackSpot client ID
client_secret str Yes - Your StackSpot client secret
auth_url str No https://idm.stackspot.com/stackspot-freemium/oidc/oauth/token Authentication URL
max_retries int No 30 Maximum number of retries for result polling
retry_interval int No 5 Interval between retries in seconds
request_delay float No 0.0 Delay in seconds before each request
proxies dict No None Proxy configuration for HTTP/HTTPS requests

Environment Variables & .env support

You can configure the client using environment variables for improved security and flexibility. The following variables are supported:

  • STACKSPOT_BASE_URL (required)
  • STACKSPOT_CLIENT_ID (required)
  • STACKSPOT_CLIENT_SECRET (required)
  • STACKSPOT_AUTH_URL (optional)
  • STACKSPOT_MAX_RETRIES (optional)
  • STACKSPOT_RETRY_INTERVAL (optional)
  • STACKSPOT_REQUEST_DELAY (optional)
  • STACKSPOT_PROXY_HTTP (optional)
  • STACKSPOT_PROXY_HTTPS (optional)

You may use a .env file in your project root for local development:

STACKSPOT_BASE_URL=https://genai-code-buddy-api.stackspot.com
STACKSPOT_CLIENT_ID=your_client_id
STACKSPOT_CLIENT_SECRET=your_client_secret
STACKSPOT_AUTH_URL=https://idm.stackspot.com/stackspot-freemium/oidc/oauth/token
STACKSPOT_MAX_RETRIES=30
STACKSPOT_RETRY_INTERVAL=5
STACKSPOT_REQUEST_DELAY=0.0
STACKSPOT_PROXY_HTTP=http://proxy.example.com:8080
STACKSPOT_PROXY_HTTPS=https://proxy.example.com:8080

Example: Loading from environment

from stackspot_client import StackSpotConfig, StackSpotClient

# Optionally load .env file (requires python-dotenv)
from dotenv import load_dotenv
load_dotenv()

config = StackSpotConfig.from_env()
client = StackSpotClient(config)

Example: Using proxies

from stackspot_client import StackSpotConfig, StackSpotClient

# Configure proxies directly
config = StackSpotConfig(
    base_url="https://genai-code-buddy-api.stackspot.com",
    client_id="your_client_id",
    client_secret="your_client_secret",
    proxies={
        'http': 'http://proxy.example.com:8080',
        'https': 'https://proxy.example.com:8080'
    }
)

# Or configure via environment variables
# STACKSPOT_PROXY_HTTP=http://proxy.example.com:8080
# STACKSPOT_PROXY_HTTPS=https://proxy.example.com:8080
config = StackSpotConfig.from_env()

client = StackSpotClient(config)

| client_id | str | Yes | - | Your StackSpot client ID | | client_secret | str | Yes | - | Your StackSpot client secret | | auth_url | str | No | https://idm.stackspot.com/stackspot-freemium/oidc/oauth/token | Authentication URL | | max_retries | int | No | 30 | Maximum number of retries for result polling | | retry_interval | int | No | 5 | Interval between retries in seconds |

Usage Examples

Knowledge Source Upload Examples

Below are usage examples for the upload methods available in the KnowledgeSources class:

from stackspot_client import StackSpotConfig, StackSpotClient
from stackspot_client.knowledge_sources import KnowledgeSources

# It is recommended to configure your credentials using environment variables or a .env file:
# STACKSPOT_BASE_URL, STACKSPOT_CLIENT_ID, STACKSPOT_CLIENT_SECRET, STACKSPOT_AUTH_URL (optional)
config = StackSpotConfig.from_env()

# If needed, you can override any parameter manually:
# config.base_url = 'https://another-endpoint.com'

client = StackSpotClient(config)
ks = KnowledgeSources(client)
ks_slug = 'my-ks'
file_path = '/path/to/file.pdf'

# 1. Direct file upload (without Docling processing)
ks.upload_file(file_path, ks_slug)

> **Note:** Supported files for direct upload: `.json`, `.yml`, `.yaml`, `.md`, `.txt`, `.pdf`, `.zip` (the `.zip` file must contain only the supported file types listed). Maximum size: **10MB per file**. Other formats must be processed with Docling.

# 2. Upload content extracted from a URL (processed by Docling to Markdown)
ks.upload_from_url('https://example.com/article', ks_slug)

# 3. Upload a local file processed by Docling (generates Markdown before upload)
ks.upload_file_with_docling(file_path, ks_slug)

> **Note:** When using Docling (with `upload_from_url` or `upload_file_with_docling`), a wide range of file formats is supported. For the complete list, see the [Docling Supported Formats documentation](https://docling-project.github.io/docling/usage/supported_formats/).

# Delete all files from a knowledge source
success = ks.delete_all_files("my-knowledge-source")

Response Format

The get_execution_result method returns a dictionary with the following structure:

{
    'status': 'COMPLETED',  # or 'FAILED', 'RUNNING'
    'answer': '...',       # The actual response
    'progress': {          # Optional progress information
        'status': 'COMPLETE',
        'percentage': 100
    }
}

Error Types

The library provides several error types for better error handling:

  • StackSpotError: Base exception class
  • AuthenticationError: Raised when authentication fails
  • APIError: Raised when API calls fail

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

License

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

Support

For support with the StackSpot API, please refer to the official documentation.

Changelog

0.1.7

  • Added proxy support for HTTP/HTTPS requests
  • New proxies parameter in StackSpotConfig for direct proxy configuration
  • Environment variables support: STACKSPOT_PROXY_HTTP and STACKSPOT_PROXY_HTTPS
  • Proxy configuration applies to both authentication and API requests
  • Updated documentation with proxy usage examples

0.1.4

  • Environment-based configuration as default (with .env support)
  • File upload validation: allowed formats and max size (10MB)
  • Integration with Docling for file and URL uploads (Markdown conversion)
  • Added ValidationError for input validation
  • Maintainer metadata (eltonjosesouza) in setup.py
  • General code and documentation cleanup

0.1.2

  • Added support for Knowledge Sources management
  • Implemented knowledge source creation functionality
  • Added support for file uploads
  • Implemented URL content upload functionality
  • Added functionality to delete all files from a knowledge source

0.1.0

  • Initial release
  • Basic authentication and command execution
  • Result polling with retry mechanism
  • Comprehensive error 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

stackspot_client-0.1.7.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

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

stackspot_client-0.1.7-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file stackspot_client-0.1.7.tar.gz.

File metadata

  • Download URL: stackspot_client-0.1.7.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for stackspot_client-0.1.7.tar.gz
Algorithm Hash digest
SHA256 fd11cce184321fedce8aa9c9380ad4454d4c85458d56f90015211307dabce1bd
MD5 8fd71efb0ed71f1c6c88575d0eaf0f0a
BLAKE2b-256 2daa07cdf746a2c109f1f892ac036e2dd0cc2a5283705dae214c0bdf892b1e0d

See more details on using hashes here.

File details

Details for the file stackspot_client-0.1.7-py3-none-any.whl.

File metadata

File hashes

Hashes for stackspot_client-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 7a2383ad1b38547b6b5eabc2cd6f8e8bd3f4cf1d3a0623b9b9a2e99d6d6a99c6
MD5 8d3f4e89867e8c1db79441a8fd2c885b
BLAKE2b-256 bb8dd10e1b1b78be27082bf1d5a06ac8c1b885d98e29460417e541fcf7abbd1e

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