Skip to content

API Client

The EpiSuiteAPIClient class provides the core interface for interacting with the EPISuite API.

Overview

pyepisuite.api_client.EpiSuiteAPIClient

Source code in src/pyepisuite/api_client.py
class EpiSuiteAPIClient:
    def __init__(self, base_url='https://episuite.dev/EpiWebSuite/api', api_key=None):
        self.base_url = base_url
        self.api_key = api_key

    def search(self, query_term, time_out=10):
        """
        Search the EPISuite API with a query term (SMILES, CAS, or chemical name).

        Parameters:
            query_term (str): The term to search for.
            time_out (int): The time out for the request.

        Returns:
            List[Chemical]: A list of Chemical instances.
        """
        url = f'{self.base_url}/search'
        params = {'query': query_term}
        headers = {}

        if self.api_key:
            headers['Authorization'] = f'Bearer {self.api_key}'

        response = requests.get(url, params=params, headers=headers, timeout=time_out)
        response.raise_for_status()
        data = response.json()

        # Convert each dictionary in the response to a Chemical instance
        ids = [Identifiers(**item) for item in data]
        return ids

    def submit(self, cas="", smiles=""):
        """
        Submit a CAS number or SMILES string to the EPISuite API.

        Parameters:
            cas (str): The CAS number of the chemical.
            smiles (str): The SMILES string of the chemical.

        Returns:
            dict: The JSON response from the API.

        Raises:
            ValueError: If neither 'cas' nor 'smiles' is provided.
        """
        if not cas and not smiles:
            raise ValueError("Either 'cas' or 'smiles' must be provided.")

        url = f'{self.base_url}/submit'
        params = {}
        if cas:
            params['cas'] = cas
        else:
            params['smiles'] = smiles

        headers = {}
        if self.api_key:
            headers['Authorization'] = f'Bearer {self.api_key}'

        response = requests.get(url, params=params, headers=headers)
        response.raise_for_status()
        data = response.json()
        return data

search(query_term, time_out=10)

Search the EPISuite API with a query term (SMILES, CAS, or chemical name).

Parameters:

Name Type Description Default
query_term str

The term to search for.

required
time_out int

The time out for the request.

10

Returns:

Type Description

List[Chemical]: A list of Chemical instances.

Source code in src/pyepisuite/api_client.py
def search(self, query_term, time_out=10):
    """
    Search the EPISuite API with a query term (SMILES, CAS, or chemical name).

    Parameters:
        query_term (str): The term to search for.
        time_out (int): The time out for the request.

    Returns:
        List[Chemical]: A list of Chemical instances.
    """
    url = f'{self.base_url}/search'
    params = {'query': query_term}
    headers = {}

    if self.api_key:
        headers['Authorization'] = f'Bearer {self.api_key}'

    response = requests.get(url, params=params, headers=headers, timeout=time_out)
    response.raise_for_status()
    data = response.json()

    # Convert each dictionary in the response to a Chemical instance
    ids = [Identifiers(**item) for item in data]
    return ids

submit(cas='', smiles='')

Submit a CAS number or SMILES string to the EPISuite API.

Parameters:

Name Type Description Default
cas str

The CAS number of the chemical.

''
smiles str

The SMILES string of the chemical.

''

Returns:

Name Type Description
dict

The JSON response from the API.

Raises:

Type Description
ValueError

If neither 'cas' nor 'smiles' is provided.

Source code in src/pyepisuite/api_client.py
def submit(self, cas="", smiles=""):
    """
    Submit a CAS number or SMILES string to the EPISuite API.

    Parameters:
        cas (str): The CAS number of the chemical.
        smiles (str): The SMILES string of the chemical.

    Returns:
        dict: The JSON response from the API.

    Raises:
        ValueError: If neither 'cas' nor 'smiles' is provided.
    """
    if not cas and not smiles:
        raise ValueError("Either 'cas' or 'smiles' must be provided.")

    url = f'{self.base_url}/submit'
    params = {}
    if cas:
        params['cas'] = cas
    else:
        params['smiles'] = smiles

    headers = {}
    if self.api_key:
        headers['Authorization'] = f'Bearer {self.api_key}'

    response = requests.get(url, params=params, headers=headers)
    response.raise_for_status()
    data = response.json()
    return data

Usage Examples

Basic Client Setup

from pyepisuite import EpiSuiteAPIClient

# Create client with default settings
client = EpiSuiteAPIClient()

# Or with custom base URL
client = EpiSuiteAPIClient(base_url='https://episuite.dev/EpiWebSuite/api')

# With API key (if required)
client = EpiSuiteAPIClient(api_key='your-api-key')

Searching for Chemicals

# Search by CAS number
results = client.search('50-00-0')

# Search by SMILES
results = client.search('C=O')

# Search by chemical name
results = client.search('formaldehyde')

Submitting for Predictions

# Submit using CAS number
predictions = client.submit(cas='50-00-0')

# Submit using SMILES
predictions = client.submit(smiles='C=O')

# The response includes both EPI Suite and EcoSAR results
print(predictions.keys())  # ['chemicalProperties', 'logKow', ..., 'ecosar']

Error Handling

The API client includes robust error handling:

import requests

try:
    results = client.search('invalid-identifier')
except requests.exceptions.RequestException as e:
    print(f"API request failed: {e}")
except ValueError as e:
    print(f"Invalid parameters: {e}")

Configuration Options

Timeout Settings

# Custom timeout for slow networks
results = client.search('50-00-0', time_out=30)

API Key Authentication

If the API requires authentication:

client = EpiSuiteAPIClient(api_key='your-api-key-here')

For higher-level operations, see the utility functions: