Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Accounts

Imports

Python modules

from typing import Union

Third-party modules

from sgqlc.operation import Operation

newrelic_sb_sdk module

from newrelic_sb_sdk.client import NewRelicClient
from newrelic_sb_sdk.graphql.input_objects import TimeWindowInput
from newrelic_sb_sdk.graphql.objects import Account
from newrelic_sb_sdk.utils.response import raise_response_errors

Functions

get_all_accounts

Fetches a list of all accounts available to the respective API key.

def get_all_accounts(
    *,
    client: NewRelicClient,
    include_event_types: bool = False,
    time_window: TimeWindowInput | None = None,
) -> list[Account]:
    """Retrieve all accessible New Relic accounts.

    Queries the New Relic GraphQL API to fetch accounts accessible by the
    provided client.

    Args:
        client: The New Relic API client instance for authentication.
        include_event_types: Fetches reporting event types if True. Defaults to False.
        time_window: Filters event types by a specific window. Defaults to None.

    Returns:
        A list of Account objects.
    """

    operation = Operation(client.schema.query_type)

    operation.actor.accounts.id()
    operation.actor.accounts.name()

    if include_event_types:
        if time_window:
            operation.actor.accounts.reporting_event_types(time_window=time_window)
        else:
            operation.actor.accounts.reporting_event_types()

    response = client.execute(operation)

    raise_response_errors(response=response)

    data = operation + response.json()
    data = data.actor.accounts

    return data

get_account

Retrieves specific details for a single New Relic account by its corresponding ID.

def get_account(*, client: NewRelicClient, account_id: int) -> Account:
    """Retrieve a specific New Relic account by its unique identifier.

    Args:
        client: The New Relic API client instance for authentication.
        account_id: The integer ID referencing the specific New Relic account.

    Returns:
        The target Account object fetched from the API.
    """

    operation = Operation(client.schema.query_type)

    account = operation.actor.account(id=account_id)

    account.id()
    account.name()

    response = client.execute(operation)

    raise_response_errors(response=response)

    data = operation + response.json()
    data = data.actor.account

    return data