Package: amberflo-metering-python
amberflo-metering-pythonAmberflois the simplest way to integrate metering into your application.This is the official Python 3 client that wraps theAmberflo REST API.:heavy_check_mark: FeaturesAdd and update customersAssign and update product plans to customersList invoices of a customerGet a new customer portal session for a customerAdd and list prepaid orders to customersSend meter eventsIn asynchronous batches for high throughput (with optional flush on demand)Or synchronouslyUsing the Amberflo API or the Amberflo supplied AWS S3 bucketQuery usageFine grained logging control:rocket: Quick StartSign up for freeand get an API key.Install the SDKpip install amberflo-metering-pythonCreate a customerimportosfrommetering.customerimportCustomerApiClient,create_customer_payloadclient=CustomerApiClient(os.environ.get("API_KEY"))message=create_customer_payload(customer_id="sample-customer-123",customer_email="customer-123@sample.com",customer_name="Sample Customer",traits={"region":"us-east-1",},)customer=client.add_or_update(message)Ingest meter eventsimportosfromtimeimporttimefrommetering.ingestimportcreate_ingest_clientclient=create_ingest_client(api_key=os.environ["API_KEY"])dimensions={"region":"us-east-1"}customer_id="sample-customer-123"client.meter(meter_api_name="sample-meter",meter_value=5,meter_time_in_millis=int(time()*1000),customer_id=customer_id,dimensions=dimensions,)Query usageimportosfromtimeimporttimefrommetering.usageimport(AggregationType,Take,TimeGroupingInterval,TimeRange,UsageApiClient,create_usage_query)client=UsageApiClient(os.environ.get("API_KEY"))since_two_days_ago=TimeRange(int(time())-60*60*24*2)query=create_usage_query(meter_api_name="my_meter",aggregation=AggregationType.SUM,time_grouping_interval=TimeGroupingInterval.DAY,time_range=since_two_days_ago,group_by=["customerId"],usage_filter={"customerId":["some-customer-321","sample-customer-123"]},take=Take(limit=10,is_ascending=False),)report=client.get(query):zap: High throughput ingestionAmberflo.io libraries are built to support high throughput environments. That
means you can safely send hundreds of meter records per second. For example,
you can chose to deploy it on a web server that is serving hundreds of requests
per second.However, every call does not result in a HTTP request, but is queued in memory
instead. Messages are batched and flushed in the background, allowing for much
faster operation. The size of batch and rate of flush can be customized.Flush on demand:For example, at the end of your program, you'll want to
flush to make sure there's nothing left in the queue. Calling this method will
block the calling thread until there are no messages left in the queue. So,
you'll want to use it as part of your cleanup scripts and avoid using it as
part of the request lifecycle.Error handling:The SDK allows you to set up aon_errorcallback function
for handling errors when trying to send a batch.Here is a complete example, showing the default values of all options:defon_error_callback(error,batch):...client=create_ingest_client(api_key=API_KEY,max_queue_size=100000,# max number of items in the queue before rejecting new itemsthreads=2,# number of worker threads doing the sendingretries=2,# max number of retries after failuresbatch_size=100,# max number of meter records in a batchsend_interval_in_secs=0.5,# wait time before sending an incomplete batchsleep_interval_in_secs=0.1,# wait time after failure to send or queue emptyon_error=on_error_callback,# handle failures to send a batch)...client.meter(...)client.flush()# block and make sure all messages are sentWhat happens if there are just too many messages?If the module detects that it can't flush faster than it's receiving messages,
it'll simply stop accepting new messages. This allows your program to
continually run without ever crashing due to a backed up metering queue.Ingesting through the S3 bucketThe SDK provides ametering.ingest.IngestS3Clientso you can send your meter
records to us via the S3 bucket.Use of this feature is enabled if you install the library with thes3option:pip install amberflo-metering-python[s3]Just pass the S3 bucket credentials to the factory function:client=create_ingest_client(bucket_name=os.environ.get("BUCKET_NAME"),access_key=os.environ.get("ACCESS_KEY"),secret_key=os.environ.get("SECRET_KEY"),):book: DocumentationGeneral documentation on how to use Amberflo is available atProduct Walkthrough.The full REST API documentation is available atAPI Reference.:scroll: SamplesCode samples covering different scenarios are available in the./samplesfolder.:construction_worker: ContributingFeel free to open issues and send a pull request.Also, check outCONTRIBUTING.md.:bookmark_tabs: ReferenceAPI ClientsIngestfrommetering.ingestimport(create_ingest_payload,create_ingest_client,)Customerfrommetering.customerimport(CustomerApiClient,create_customer_payload,)Usagefrommetering.usageimport(AggregationType,Take,TimeGroupingInterval,TimeRange,UsageApiClient,create_usage_query,create_all_usage_query,)Customer Portal Sessionfrommetering.customer_portal_sessionimport(CustomerPortalSessionApiClient,create_customer_portal_session_payload,)Customer Prepaid Orderfrommetering.customer_prepaid_orderimport(BillingPeriod,BillingPeriodUnit,CustomerPrepaidOrderApiClient,create_customer_prepaid_order_payload,)Customer Product Invoicefrommetering.customer_product_invoiceimport(CustomerProductInvoiceApiClient,create_all_invoices_query,create_latest_invoice_query,create_invoice_query,)Customer Product Planfrommetering.customer_product_planimport(CustomerProductPlanApiClient,create_customer_product_plan_payload,)Exceptionsfrommetering.exceptionsimportApiErrorLoggingamberflo-metering-pythonuses the standard Python logging framework. By
default, logging is and set at theWARNINGlevel.The following loggers are used:metering.ingest.producermetering.ingest.s3_clientmetering.ingest.consumermetering.session.ingest_sessionmetering.session.api_session