Skip to main content

Python SDK for https://fragment.dev/

Project description

fragment-python

Fragment is the Ledger API for engineers that move money. Stop wrangling payment tables, debugging balance errors, and hacking together data pipelines. Start shipping the features that make a difference.

Installation

Using pip:

pip install fragment-python

Using poetry:

poetry add fragment-python

Usage

Get started by instantiating a Client from fragment.sdk.client. You can generate credentials using the Fragment dashboard

from fragment.sdk.client import Client

graphql_client = Client(
    client_id="<client id from the dashboard>",
    client_secret="<client secret from the dashboard>",
    api_url="<api url from the dashboard>",
    auth_url="<auth url from the dashboard>",
    auth_scope="<auth scope from the dashboard>",
  )

async def print_schema():
  get_schema_result = await graphql_client.get_schema("<Your schema key here>")
  print(get_schema_result.schema_.json())

import asyncio
loop = asyncio.get_event_loop()
loop.run_until_complete(print_schema())

Read the Using custom queries section to learn how to use your own GraphQL queries with the SDK.

Using a synchronous client

If you prefer using a synchronous client instead of an async one, then:

from fragment.sync_sdk.client import Client

graphql_client = Client(
    client_id="<client id from the dashboard>",
    client_secret="<client secret from the dashboard>",
    api_url="<api url from the dashboard>",
    auth_url="<auth url from the dashboard>",
    auth_scope="<auth scope from the dashboard>",
  )

get_schema_result = graphql_client.get_schema("<Your schema key here>")
print(get_schema_result.schema_.json())

Examples

Post a Ledger Entry

To post a Ledger Entry defined in your Schema:

await graphql_client.add_ledger_entry(
  ik="some-ik",
  ledger_ik="your-ledger-ik",
  type="user_funds_account",
  posted="1968-01-01T16:45:00Z",
  parameters=dict(
    user_id="user-1",
    funding_amount="20000",
  )
)

Read a Ledger Account's Balance

To read a Ledger Account's balance:

from fragment.sdk.enums import CurrencyCode
from fragment.sdk.input_types import CurrencyMatchInput

await graphql_client.get_ledger_account_balance(
  ledger_ik="your-ledger-ik",
  path="liabilities/user:user-1/available",
  balance_currency=CurrencyMatchInput(code=CurrencyCode.USD),
)

Using custom queries

While the SDK comes with GraphQL queries out of the box, you may want to customize these queries for your product. In order to do that:

  1. Define your custom GraphQL queries in a GraphQL file. For example, in queries/custom-queries.graphql:
query getSchemaName($key: SafeString!) {
  schema(schema: { key: $key }) {
    key
    name
  }
}
  1. Run fragment-python-client-codegen to generate the GraphQL SDK client. GraphQL named queries are converted to snake_case to conform to Python's code conventions. Optionally, pass the --sync flag to generate a synchronous client instead of the default async GraphQL client.
fragment-python-client-codegen \
  --input-dir libs/fragment/queries/ \
  --target-package-name=custom_queries_package \
  --output-dir=libs/fragment
  1. Use the client from the generated package in your product! Apart from the custom query methods, this client is functionally identical to fragment.sdk.client.Client
from .libs.fragment.custom_queries_package.client import Client

graphql_client = Client(
    client_id="<client id from the dashboard>",
    client_secret="<client secret from the dashboard>",
    api_url="<api url from the dashboard>",
    auth_url="<auth url from the dashboard>",
    auth_scope="<auth scope from the dashboard>",
  )

async def print_schema_name():
  # Note that getSchemaName is converted to snake_case automatically
  response = await graphql_client.get_schema_name("<Your Schema Key>")
  print(response.schema_.key)

import asyncio
loop = asyncio.get_event_loop()
loop.run_until_complete(print_schema())

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

fragment_python-0.5.2.tar.gz (50.1 kB view details)

Uploaded Source

Built Distribution

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

fragment_python-0.5.2-py3-none-any.whl (87.0 kB view details)

Uploaded Python 3

File details

Details for the file fragment_python-0.5.2.tar.gz.

File metadata

  • Download URL: fragment_python-0.5.2.tar.gz
  • Upload date:
  • Size: 50.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.10.14 Linux/6.11.0-1018-azure

File hashes

Hashes for fragment_python-0.5.2.tar.gz
Algorithm Hash digest
SHA256 f2d91d2bb0a78f27fedc081c73a4e90ce8ab077d8f22dcb03d369a9a42e7ff7c
MD5 82f73d35e946521ba0eeea3210ef930f
BLAKE2b-256 1d03a220c152595a6201e0e7847b52ca620e436b65c295027723a43dcd9efc35

See more details on using hashes here.

File details

Details for the file fragment_python-0.5.2-py3-none-any.whl.

File metadata

  • Download URL: fragment_python-0.5.2-py3-none-any.whl
  • Upload date:
  • Size: 87.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.10.14 Linux/6.11.0-1018-azure

File hashes

Hashes for fragment_python-0.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 59d09cf0d5723d71630aec3a65599776a3ccc66f2a80a74123d96bd120e62783
MD5 91ff2cb3b7125ba50b7a814199374683
BLAKE2b-256 1c2d6371041bf6af21a19076ce7275fd9edb7b1c41895020ae5b5abf13dd51fd

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