Python源码示例:googleapiclient.discovery.build()
示例1
def make_cloud_mlengine_request_fn(credentials, model_name, version):
"""Wraps function to make CloudML Engine requests with runtime args."""
def _make_cloud_mlengine_request(examples):
"""Builds and sends requests to Cloud ML Engine."""
api = discovery.build("ml", "v1", credentials=credentials)
parent = "projects/%s/models/%s/versions/%s" % (cloud.default_project(),
model_name, version)
input_data = {
"instances": [{
"input": {
"b64": base64.b64encode(ex.SerializeToString())
}
} for ex in examples]
}
prediction = api.projects().predict(body=input_data, name=parent).execute()
return prediction["predictions"]
return _make_cloud_mlengine_request
示例2
def create_runner(known_args, pipeline_args, input_pattern, watchdog_file,
watchdog_file_update_interval_seconds):
# type: (argparse.Namespace, List[str], str, Optional[str], int) -> VepRunner
"""Returns an instance of VepRunner using the provided args.
Args:
known_args: The list of arguments defined in `variant_transform_options`.
pipeline_args: The list of remaining arguments meant to be used to
determine resources like number of workers, machine type, etc.
input_pattern: The VCF files to be annotated.
watchdog_file: The file that will be updated by the Dataflow worker every
`watchdog_file_update_interval_seconds`. Once the file is found to be
stale, the VEP process running in the VM will be killed.
watchdog_file_update_interval_seconds: The `watchdog_file` will be updated
by the Dataflow worker every `watchdog_file_update_interval_seconds`.
"""
credentials = client.GoogleCredentials.get_application_default()
pipeline_service = discovery.build(
'genomics', 'v2alpha1', credentials=credentials)
runner = VepRunner(
pipeline_service, known_args.vep_species, known_args.vep_assembly,
input_pattern, known_args.annotation_output_dir,
known_args.vep_info_field, known_args.vep_image_uri,
known_args.vep_cache_path, known_args.vep_num_fork, pipeline_args,
watchdog_file, watchdog_file_update_interval_seconds)
return runner
示例3
def get_api_client(self, service, version, scopes):
"""Gets an authenticated api connection to the provided service and version.
Args:
service: str, the name of the service to connect to.
version: str, the version of the service to connect to.
scopes: List[str], a list of the required scopes for this api call.
Returns:
An authenticated api client connection.
"""
if not self._credentials.has_scopes(scopes):
scopes.extend(self._credentials.scopes)
self._credentials = self.get_credentials(scopes)
return build(
serviceName=service, version=version,
http=google_auth_httplib2.AuthorizedHttp(credentials=self._credentials))
示例4
def _create_directory_service(self, user_email=None):
"""Build and return a Google Admin SDK Directory API service object.
Args:
user_email: String, The email address of the user that needs permission to
access the admin APIs.
Returns:
Google Admin SDK Directory API service object.
Raises:
UnauthorizedUserError: If a user email is not provided.
"""
if user_email and user_email.split('@')[1] in constants.APP_DOMAINS:
credentials = service_account.Credentials.from_service_account_file(
filename=constants.SECRETS_FILE,
scopes=constants.DIRECTORY_SCOPES,
subject=constants.ADMIN_EMAIL)
logging.info('Created delegated credentials for %s.', user_email)
else:
raise UnauthorizedUserError('User Email not provided.')
return build(
serviceName='admin',
version='directory_v1',
http=google_auth_httplib2.AuthorizedHttp(credentials=credentials))
示例5
def __init__(self, api, option="pandas"):
self.YOUTUBE_READ_WRITE_SSL_SCOPE = "https://www.googleapis.com/auth/youtube.force-ssl"
self.YOUTUBE_API_SERVICE_NAME = 'youtube'
self.YOUTUBE_API_VERSION = 'v3'
self.DEVELOPER_KEY = api["api_key"]
# This variable defines a message to display if the CLIENT_SECRETS_FILE is
# missing.
self.MISSING_CLIENT_SECRET_MESSAGE = """
WARNING: Please configure OAuth 2.0
To run you will need to populate the client_secrets.json file
with information from the APIs Console
https://console.developers.google.com"""
self.option = option
self.youtube = build(self.YOUTUBE_API_SERVICE_NAME, self.YOUTUBE_API_VERSION,
developerKey=self.DEVELOPER_KEY)
示例6
def list_tpus(project, location):
"""List existing TPU nodes in the project/location.
Args:
project: (str) GCP project id.
location: (str) GCP compute location, such as "us-central1-b".
Returns
A Python dictionary with keys 'nodes' and 'nextPageToken'.
"""
service = discovery.build('tpu', 'v1', credentials=credentials)
parent = 'projects/{}/locations/{}'.format(project, location)
request = service.projects().locations().nodes().list(parent=parent)
return request.execute()
示例7
def get_tpu(project, location, tpu_name):
"""List existing TPU nodes in the project/location.
Args:
project: (str) GCP project id.
location: (str) GCP compute location, such as "us-central1-b".
tpu_name: (str) The ID of the TPU node.
Returns
A TPU node object.
"""
service = discovery.build('tpu', 'v1', credentials=credentials)
name = 'projects/{}/locations/{}/nodes/{}'.format(
project, location, tpu_name)
request = service.projects().locations().nodes().get(name=name)
return request.execute()
示例8
def delete_tpu(project, location, tpu_name):
"""List existing TPU nodes in the project/location.
Args:
project: (str) GCP project id.
location: (str) GCP compute location, such as "us-central1-b".
tpu_name: (str) The ID of the TPU node.
Returns
A TPU node deletion operation object.
"""
service = discovery.build('tpu', 'v1', credentials=credentials)
name = 'projects/{}/locations/{}/nodes/{}'.format(
project, location, tpu_name)
request = service.projects().locations().nodes().delete(
name=name)
return request.execute()
示例9
def list_tpus(project, location):
"""List existing TPU nodes in the project/location.
Args:
project: (str) GCP project id.
location: (str) GCP compute location, such as "us-central1-b".
Returns
A Python dictionary with keys 'nodes' and 'nextPageToken'.
"""
service = discovery.build('tpu', 'v1', credentials=credentials)
parent = 'projects/{}/locations/{}'.format(project, location)
request = service.projects().locations().nodes().list(parent=parent)
return request.execute()
示例10
def get_tpu(project, location, tpu_name):
"""List existing TPU nodes in the project/location.
Args:
project: (str) GCP project id.
location: (str) GCP compute location, such as "us-central1-b".
tpu_name: (str) The ID of the TPU node.
Returns
A TPU node object.
"""
service = discovery.build('tpu', 'v1', credentials=credentials)
name = 'projects/{}/locations/{}/nodes/{}'.format(
project, location, tpu_name)
request = service.projects().locations().nodes().get(name=name)
return request.execute()
示例11
def delete_tpu(project, location, tpu_name):
"""List existing TPU nodes in the project/location.
Args:
project: (str) GCP project id.
location: (str) GCP compute location, such as "us-central1-b".
tpu_name: (str) The ID of the TPU node.
Returns
A TPU node deletion operation object.
"""
service = discovery.build('tpu', 'v1', credentials=credentials)
name = 'projects/{}/locations/{}/nodes/{}'.format(
project, location, tpu_name)
request = service.projects().locations().nodes().delete(
name=name)
return request.execute()
示例12
def list_tpus(project, location):
"""List existing TPU nodes in the project/location.
Args:
project: (str) GCP project id.
location: (str) GCP compute location, such as "us-central1-b".
Returns
A Python dictionary with keys 'nodes' and 'nextPageToken'.
"""
service = discovery.build('tpu', 'v1', credentials=credentials)
parent = 'projects/{}/locations/{}'.format(project, location)
request = service.projects().locations().nodes().list(parent=parent)
return request.execute()
示例13
def get_tpu(project, location, tpu_name):
"""List existing TPU nodes in the project/location.
Args:
project: (str) GCP project id.
location: (str) GCP compute location, such as "us-central1-b".
tpu_name: (str) The ID of the TPU node.
Returns
A TPU node object.
"""
service = discovery.build('tpu', 'v1', credentials=credentials)
name = 'projects/{}/locations/{}/nodes/{}'.format(
project, location, tpu_name)
request = service.projects().locations().nodes().get(name=name)
return request.execute()
示例14
def file_handler(update, context):
"""handles the uploaded files"""
file = context.bot.getFile(update.message.document.file_id)
file.download(update.message.document.file_name)
doc = update.message.document
service = build('drive', 'v3', credentials=getCreds(),cache_discovery=False)
filename = doc.file_name
metadata = {'name': filename}
media = MediaFileUpload(filename, chunksize=1024 * 1024, mimetype=doc.mime_type, resumable=True)
request = service.files().create(body=metadata,
media_body=media)
response = None
while response is None:
status, response = request.next_chunk()
if status:
print( "Uploaded %d%%." % int(status.progress() * 100))
context.bot.send_message(chat_id=update.effective_chat.id, text="✅ File uploaded!")
示例15
def make_cloud_mlengine_request_fn(credentials, model_name, version):
"""Wraps function to make CloudML Engine requests with runtime args."""
def _make_cloud_mlengine_request(examples):
"""Builds and sends requests to Cloud ML Engine."""
api = discovery.build("ml", "v1", credentials=credentials)
parent = "projects/%s/models/%s/versions/%s" % (cloud.default_project(),
model_name, version)
input_data = {
"instances": [{ # pylint: disable=g-complex-comprehension
"input": {
"b64": base64.b64encode(ex.SerializeToString())
}
} for ex in examples]
}
response = api.projects().predict(body=input_data, name=parent).execute()
predictions = response["predictions"]
for prediction in predictions:
prediction["outputs"] = np.array([prediction["outputs"]])
prediction["scores"] = np.array(prediction["scores"])
return predictions
return _make_cloud_mlengine_request
示例16
def main():
parser = argparse.ArgumentParser(
'build_msan_libs.py', description='MSan builder.')
parser.add_argument(
'--no-track-origins',
action='store_true',
help='Build with -fsanitize-memory-track-origins=0.')
parser.add_argument(
'command',
choices=['build_packages', 'merge'],
help='The command to run.')
args = parser.parse_args()
cloudbuild = build('cloudbuild', 'v1', cache_discovery=False)
if args.command == 'build_packages':
for package in PACKAGES:
build_body = get_build(build_steps(package, args.no_track_origins))
print(start_build(cloudbuild, build_body))
else: # merge
print(start_build(cloudbuild,
get_build(merge_steps(args.no_track_origins))))
示例17
def make_cloud_mlengine_request_fn(credentials, model_name, version):
"""Wraps function to make CloudML Engine requests with runtime args."""
def _make_cloud_mlengine_request(examples):
"""Builds and sends requests to Cloud ML Engine."""
api = discovery.build("ml", "v1", credentials=credentials)
parent = "projects/%s/models/%s/versions/%s" % (cloud.default_project(),
model_name, version)
input_data = {
"instances": [{ # pylint: disable=g-complex-comprehension
"input": {
"b64": base64.b64encode(ex.SerializeToString())
}
} for ex in examples]
}
prediction = api.projects().predict(body=input_data, name=parent).execute()
return prediction["predictions"]
return _make_cloud_mlengine_request
示例18
def get_resource_url_with_default(self, resource, url_or_name, **kwargs):
"""
Build a GCPResourceUrl from a service's name and resource url or name.
If the url_or_name is a valid GCP resource URL, then we build the
GCPResourceUrl object by parsing this URL. If the url_or_name is its
short name, then we build the GCPResourceUrl object by constructing
the resource URL with default project, region, zone values.
"""
# If url_or_name is a valid GCP resource URL, then parse it.
if self.RESOURCE_REGEX.match(url_or_name):
return self.parse_url(url_or_name)
# Otherwise, construct resource URL with default values.
if resource not in self._resources:
log.warning('Unknown resource: %s', resource)
return None
parameter_defaults = self._parameter_defaults.copy()
parameter_defaults.update(kwargs)
parsed_url = GCPResourceUrl(resource, self._connection)
for key in self._resources[resource]['parameters']:
parsed_url.parameters[key] = parameter_defaults.get(
key, url_or_name)
return parsed_url
示例19
def get_conn(self):
"""
Retrieves the connection to Cloud Firestore.
:return: Google Cloud Firestore services object.
"""
if not self._conn:
http_authorized = self._authorize()
# We cannot use an Authorized Client to retrieve discovery document due to an error in the API.
# When the authorized customer will send a request to the address below
# https://www.googleapis.com/discovery/v1/apis/firestore/v1/rest
# then it will get the message below:
# > Request contains an invalid argument.
# At the same time, the Non-Authorized Client has no problems.
non_authorized_conn = build("firestore", self.api_version, cache_discovery=False)
self._conn = build_from_document(
non_authorized_conn._rootDesc, # pylint: disable=protected-access
http=http_authorized
)
return self._conn
示例20
def get_pubsub_client():
"""Get a pubsub client from the API."""
return discovery.build('pubsub', 'v1', credentials=CREDENTIALS)
示例21
def __init__(self):
Plugin.__init__(self)
self.sqladmin = discovery.build(
'sqladmin', 'v1beta4', credentials=CREDENTIALS)
self.batch = self.sqladmin.new_batch_http_request(
callback=self.batch_callback)
示例22
def __init__(self):
Plugin.__init__(self)
self.storage = discovery.build(
'storage', 'v1', credentials=CREDENTIALS)
self.batch = self.storage.new_batch_http_request(
callback=self.batch_callback)
示例23
def __init__(self):
Plugin.__init__(self)
self.compute = discovery.build(
'compute', 'v1', credentials=CREDENTIALS)
self.batch = self.compute.new_batch_http_request(
callback=self.batch_callback)
示例24
def __init__(self):
Plugin.__init__(self)
self.bigquery = discovery.build(
'bigquery', 'v2', credentials=CREDENTIALS)
self.batch = self.bigquery.new_batch_http_request(
callback=self.batch_callback)
示例25
def __init__(self):
Plugin.__init__(self)
self.compute = discovery.build(
'compute', 'v1', credentials=CREDENTIALS)
self.batch = self.compute.new_batch_http_request(
callback=self.batch_callback)
示例26
def __init__(self):
Plugin.__init__(self)
self.bigtable = discovery.build(
'bigtableadmin', 'v2', credentials=CREDENTIALS)
self.batch = self.bigtable.new_batch_http_request(
callback=self.batch_callback)
示例27
def launch_job(job_spec):
"""Launch job on ML Engine."""
project_id = "projects/{}".format(
text_encoder.native_to_unicode(cloud.default_project()))
credentials = GoogleCredentials.get_application_default()
cloudml = discovery.build("ml", "v1", credentials=credentials,
cache_discovery=False)
request = cloudml.projects().jobs().create(body=job_spec, parent=project_id)
request.execute()
示例28
def youtube_search(options):
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
developerKey=DEVELOPER_KEY)
# Call the search.list method to retrieve results matching the specified
# query term.
search_response = youtube.search().list(
q=options.q,
part='id,snippet',
maxResults=3
).execute()
videos = []
channels = []
playlists = []
# Add each result to the appropriate list, and then display the lists of
# matching videos, channels, and playlists.
for search_result in search_response.get('items', []):
if search_result['id']['kind'] == 'youtube#video':
videos.append('%s (https://www.youtube.com/watch?v=%s)' % (search_result['snippet']['title'],
search_result['id']['videoId']))
elif search_result['id']['kind'] == 'youtube#channel':
channels.append('%s (%s)' % (search_result['snippet']['title'],
search_result['id']['channelId']))
elif search_result['id']['kind'] == 'youtube#playlist':
playlists.append('%s (%s)' % (search_result['snippet']['title'],
search_result['id']['playlistId']))
print ('Videos:\n', '\n'.join(videos), '\n')
print ('Channels:\n', '\n'.join(channels), '\n')
print ('Playlists:\n', '\n'.join(playlists), '\n')
示例29
def get_authenticated_service():
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
credentials = flow.run_console()
return build(API_SERVICE_NAME, API_VERSION, credentials = credentials)
示例30
def google(ctx: MtgContext, *, args: str) -> None:
"""Google search"""
api_key = configuration.get('cse_api_key')
cse_id = configuration.get('cse_engine_id')
if api_key is None or cse_id is None:
await ctx.send('The google command has not been configured.')
return
if len(args) == 0:
await ctx.send('{author}: No search term provided. Please type !google followed by what you would like to search.'.format(author=ctx.author.mention))
return
try:
service = build('customsearch', 'v1', developerKey=api_key)
res = service.cse().list(q=args, cx=cse_id, num=1).execute() # pylint: disable=no-member
if 'items' in res:
r = res['items'][0]
s = '{title} <{url}> {abstract}'.format(title=r['title'], url=r['link'], abstract=r['snippet'])
else:
s = '{author}: Nothing found on Google.'.format(author=ctx.author.mention)
except HttpError as e:
if e.resp['status'] == '403':
s = 'We have reached the allowed limits of Google API'
else:
raise e
await ctx.send(s)