Contoh kode untuk integrasi produk SaaS - AWS Marketplace

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Contoh kode untuk integrasi produk SaaS

Contoh kode berikut dapat membantu Anda mengintegrasikan produk perangkat lunak Anda sebagai layanan (SaaS) dengan AWS Marketplace API yang diperlukan untuk menerbitkan dan memelihara produk Anda.

ResolveCustomercontoh kode

Contoh kode berikut relevan untuk semua model harga. Contoh Python menukar x-amzn-marketplace-token token untukCustomerIdentifier,ProductCode, dan. CustomerAWSAccountId CustomerAWSAccountIdIni adalah Akun AWS Id yang terkait dengan langganan. Kode ini berjalan dalam aplikasi di situs web pendaftaran Anda, ketika Anda diarahkan ke sana dari Portal Manajemen AWS Marketplace. Pengalihan adalah permintaan POST yang menyertakan token.

Untuk informasi selengkapnyaResolveCustomer, lihat ResolveCustomerdi Referensi AWS Marketplace API Layanan Pengukuran.

# Import AWS Python SDK and urllib.parse import boto3 import urllib.parse as urlparse # Resolving Customer Registration Token formFields = urlparse.parse_qs(postBody) regToken = formFields['x-amzn-marketplace-token'][0] # If regToken present in POST request, exchange for customerID if (regToken): marketplaceClient = boto3.client('meteringmarketplace') customerData = marketplaceClient.resolve_customer(RegistrationToken=regToken) productCode = customerData['ProductCode'] customerID = customerData['CustomerIdentifier'] customerAWSAccountId = customerData['CustomerAWSAccountId'] # TODO: Store customer information # TODO: Validate no other accounts share the same customerID

Contoh tanggapan

{ 'CustomerIdentifier': 'string', 'CustomerAWSAccountId':'string', 'ProductCode': 'string' }

GetEntitlementcontoh kode

Contoh kode berikut relevan untuk produk SaaS dengan kontrak dan kontrak SaaS dengan model harga konsumsi. Contoh Python memverifikasi bahwa pelanggan memiliki hak aktif.

Untuk informasi selengkapnyaGetEntitlement, lihat GetEntitlementdi Referensi API Layanan AWS Marketplace Hak.

# Import AWS Python SDK import boto3 marketplaceClient = boto3.client('marketplace-entitlement', region_name='us-east-1') # Filter entitlements for a specific customerID # # productCode is supplied after the AWS Marketplace Ops team has published # the product to limited # # customerID is obtained from the ResolveCustomer response entitlement = marketplaceClient.get_entitlements({ 'ProductCode': 'productCode', 'Filter' : { 'CUSTOMER_IDENTIFIER': [ 'customerID', ] }, 'NextToken' : 'string', 'MaxResults': 123 }) # TODO: Verify the dimension a customer is subscribed to and the quantity, # if applicable

Contoh tanggapan

Nilai yang dikembalikan sesuai dengan dimensi yang dibuat saat Anda membuat produk di Portal Manajemen AWS Marketplace.

{ "Entitlements": [ { "CustomerIdentifier": "string", "Dimension": "string", "ExpirationDate": number, "ProductCode": "string", "Value": { "BooleanValue": boolean, "DoubleValue": number, "IntegerValue": number, "StringValue": "string" } } ], "NextToken": "string" }

BatchMeterUsagecontoh kode

Contoh kode berikut relevan untuk langganan SaaS dan kontrak dengan model harga konsumsi, tetapi tidak untuk produk kontrak SaaS tanpa konsumsi. Contoh Python mengirimkan catatan pengukuran untuk membebankan biaya AWS Marketplace kepada pelanggan Anda. pay-as-you-go

# NOTE: Your application will need to aggregate usage for the # customer for the hour and set the quantity as seen below. # AWS Marketplace can only accept records for up to an hour in the past. # # productCode is supplied after the AWS Marketplace Ops team has # published the product to limited # # customerID is obtained from the ResolveCustomer response # Import AWS Python SDK import boto3 usageRecord = [ { 'Timestamp': datetime(2015, 1, 1), 'CustomerIdentifier': 'customerID', 'Dimension': 'string', 'Quantity': 123 } ] marketplaceClient = boto3.client('meteringmarketplace') response = marketplaceClient.batch_meter_usage(usageRecord, productCode)

Untuk informasi selengkapnyaBatchMeterUsage, lihat BatchMeterUsagedi Referensi AWS Marketplace API Layanan Pengukuran.

Contoh tanggapan

{ 'Results': [ { 'UsageRecord': { 'Timestamp': datetime(2015, 1, 1), 'CustomerIdentifier': 'string', 'Dimension': 'string', 'Quantity': 123 }, 'MeteringRecordId': 'string', 'Status': 'Success' | 'CustomerNotSubscribed' | 'DuplicateRecord' }, ], 'UnprocessedRecords': [ { 'Timestamp': datetime(2015, 1, 1), 'CustomerIdentifier': 'string', 'Dimension': 'string', 'Quantity': 123 } ] }

BatchMeterUsagedengan contoh kode penandaan alokasi penggunaan (Opsional)

Contoh kode berikut relevan untuk langganan SaaS dan kontrak dengan model harga konsumsi, tetapi tidak untuk produk kontrak SaaS tanpa konsumsi. Contoh Python mengirimkan catatan pengukuran dengan tag alokasi penggunaan yang sesuai untuk membebankan biaya AWS Marketplace kepada pelanggan Anda. pay-as-you-go

# NOTE: Your application will need to aggregate usage for the # customer for the hour and set the quantity as seen below. # AWS Marketplace can only accept records for up to an hour in the past. # # productCode is supplied after the AWS Marketplace Ops team has # published the product to limited # # customerID is obtained from the ResolveCustomer response # Import AWS Python SDK import boto3 import time usageRecords = [ { "Timestamp": int(time.time()), "CustomerIdentifier": "customerID", "Dimension": "Dimension1", "Quantity":3, "UsageAllocations": [ { "AllocatedUsageQuantity": 2, "Tags": [ { "Key": "BusinessUnit", "Value": "IT" }, { "Key": "AccountId", "Value": "123456789" }, ] }, { "AllocatedUsageQuantity": 1, "Tags": [ { "Key": "BusinessUnit", "Value": "Finance" }, { "Key": "AccountId", "Value": "987654321" }, ] }, ] } ] marketplaceClient = boto3.client('meteringmarketplace') response = marketplaceClient.batch_meter_usage(UsageRecords=usageRecords, ProductCode="testProduct")

Untuk informasi selengkapnyaBatchMeterUsage, lihat BatchMeterUsagedi Referensi AWS Marketplace Metering Service API.

Contoh tanggapan

{ "Results": [ { "Timestamp": "1634691015", "CustomerIdentifier": "customerID", "Dimension": "Dimension1", "Quantity":3, "UsageAllocations": [ { "AllocatedUsageQuantity": 2, "Tags": [ { "Key": "BusinessUnit", "Value": "IT" }, { "Key": "AccountId", "Value": "123456789" }, ] }, { "AllocatedUsageQuantity": 1, "Tags": [ { "Key": "BusinessUnit", "Value": "Finance" }, { "Key": "AccountId", "Value": "987654321" }, ] }, ] }, "MeteringRecordId": "8fjef98ejf", "Status": "Success" }, ], "UnprocessedRecords": [ { "Timestamp": "1634691015", "CustomerIdentifier": "customerID", "Dimension": "Dimension1", "Quantity":3, "UsageAllocations": [] } ] }