Skip to main content
 首页 » 编程设计

python之Google API 客户端,在参数名称中调用带点的内置 API

2024年12月31日8soundcode

我在 Google App Engine 上尝试通过 Google API 客户端调用 Monitoring API 的特定方法。当我用 interval.startTime 调用 timeSeries.list 时,错误是 SyntaxError: keyword can't be an expression。当我将 interval.startTime 和 interval.EndTime 替换为 interval=intervalobj 时,错误是:

File "/base/data/home/apps/e~bwm2-bgi/scaler: 
scaling-readmon.412218217025616715/lib/googleapiclient/discovery.py",  
line 716, in method raise TypeError 
('Got an unexpected keyword argument "%s"' % name)  
TypeError: Got an unexpected keyword argument "interval" 

我以与 interval=intervalobl 相同的方式使用了 Compute API,并且它起作用了。任何提示表示赞赏。

代码:

import webapp2 
import logging 
from google.appengine.ext import vendor 
vendor.add('lib') 
from google.appengine.api import app_identity 
from googleapiclient import discovery 
from oauth2client.client import GoogleCredentials 
 
monitoring = discovery.build('monitoring','v3', credentials=GoogleCredentials.get_application_default()) 
class Scaler(webapp2.RequestHandler): 
        def post(self): 
                ''' 
                req = monitoring.projects().metricDescriptors().list(name='projects/PROJ') 
                res = req.execute() 
                logging.info(res) 
                ''' 
                intervalobj = { 
                        'startTime': '2018-08-10T11:01:23.045123456Z', 
                        'endTime': '2018-08-10T11:01:23.045123456Z' 
                } 
 
                res = monitoring.projects().timeSeries().list( 
                        name = 'projects/bwm2-bgi', 
                        filter = 'metric.type="appengine.googleapis.com/http/server/response_style_count"', 
                        interval.startTime = '2018-08-10T11:01:23.045123456Z', 
                        interval.endTime = '2018-08-28T11:01:23.045123456Z').execute() 
                logging.info(res) 
 
app = webapp2.WSGIApplication([ 
        ('/scaler', Scaler) 
], debug=True) 

请您参考如下方法:

对我来说,使用 interval_startTime 和 interval_endTime 代替了 interval.startTime 和 interval.endTime。

request = monitor.projects().timeSeries().list(name=project_name, 
                                        interval_startTime='2019-03-19T06:00:00.045123456Z', 
                                        interval_endTime='2019-03-19T07:00:00.045123456Z',  
                                        filter='metric.type="appengine.googleapis.com/http/server/response_style_count"') 

我相信在使用 monitoring_v3.MetricServiceClient TimeSeries 而非发现资源指定点时使用 interval.endTime 设置点间隔。