简体   繁体   English

谷歌云定价 API - Python

[英]Google Cloud Pricing API - Python

I am unable to run the following code.我无法运行以下代码。 Basically I want to list services and their prices...基本上我想列出服务及其价格......

import requests
import asyncio

class_initiate= billing_v1.services.cloud_catalog.CloudCatalogAsyncClient()
result = asyncio.run(class_initiate.list_services())```


RuntimeError: Task <Task pending coro=<CloudCatalogAsyncClient.list_services() running at local/lib/python3.7/site-packages/google/cloud/billing_v1/services/cloud_catalog/async_client.py:185> cb=[_run_until_complete_cb() at /usr/lib/python3.7/asyncio/base_events.py:158]> got Future <Task pending coro=<UnaryUnaryCall._invoke() running at /usr/local/lib/python3.7/dist-packages/grpc/aio/_call.py:489>> attached to a different loop

@brownkhusra You may want to create an async main function, and then pass it to the asyncio.run method. @brownkhusra 您可能想要创建一个异步主 function,然后将其传递给asyncio.run方法。 In this way, you ensured that the entire logic in your application will be run on the same event loop.通过这种方式,您确保了应用程序中的整个逻辑将在同一个事件循环上运行。

async def main():
    class_initiate= billing_v1.services.cloud_catalog.CloudCatalogAsyncClient()
    result = await class_initiate.list_services()

asyncio.run(main())

Use something like this to get prices for specific services.使用类似的东西来获取特定服务的价格。

In my case I looked the price for CPU/RAM in us-cenytral-1 .就我而言,我查看了us-cenytral-1中 CPU/RAM 的价格。

#!/usr/bin/env python3

from google.cloud import billing_v1
import asyncio

SERVICE_COMPUTE_DISPLAY_NAME = "Compute Engine"
REGION = "us-central1"
INSTANCE_DESCR = "N2 Custom Instance"


async def main():
    catalog = billing_v1.services.cloud_catalog.CloudCatalogAsyncClient()
    result = await catalog.list_services()
    compute_name = None
    for service in result.services:
        if service.display_name == SERVICE_COMPUTE_DISPLAY_NAME:
            compute_name = service.name
    result = await catalog.list_skus(parent=compute_name)
    for sku in result.skus:
        if REGION in sku.service_regions:
            if sku.description.startswith(INSTANCE_DESCR):
                for rate in sku.pricing_info[0].pricing_expression.tiered_rates:
                    if rate.unit_price.currency_code == "USD":
                        print(sku.description)
                        print(rate.unit_price.nanos / 1_000_000_000)


asyncio.run(main())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM