前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >gapi.client.request Methods and Classes

gapi.client.request Methods and Classes

作者头像
拿我格子衫来
发布2022-01-24 11:28:19
5080
发布2022-01-24 11:28:19
举报
文章被收录于专栏:TopFETopFE

Methods and Classes

This page documents all the methods and classes defined in the JavaScript client library.

Loading the client library

gapi.load(libraries, callbackOrConfig)

Asynchronously loads the gapi libraries requested. Use this method to load the gapi.client library.

Arguments:

  • A callback function that is called when the libraries have finished loading.
  • An object encapsulating the various configuration parameters for this method. Only callback is required. Name Type Description callback function The function called when the libraries have finished loading. onerror function The function called if the libraries failed to load. timeout number The number of milliseconds to wait before calling the ontimeout function, if the libraries still haven't loaded. ontimeout function The function called if the libraries loading has taken more time than specified by the timeout parameter.

Example:

代码语言:javascript
复制
gapi.load('client', {
callback: function() {
// Handle gapi.client initialization.
initGapiClient();
},
onerror: function() {
// Handle loading error.
alert('gapi.client failed to load!');
},
timeout: 5000, // 5 seconds.
ontimeout: function() {
// Handle timeout.
alert('gapi.client could not load in a timely manner!');
}
});

Client setup

gapi.client.init(args)

Initializes the JavaScript client with API key, OAuth client ID, scope, and API discovery document(s). If OAuth client ID and scope are provided, this function will load the gapi.auth2 module to perform OAuth. The gapi.client.init function can be run multiple times, such as to set up more APIs, to change API key, or initialize OAuth lazily. Note that the scope and clientId parameters cannot be provided multiple times, since the gapi.auth2 module can only be initialized once.

Arguments:

Returns:

Type

Description

goog.Thenable

The return value is a Promise-like goog.Thenable object that resolves when all initializations, including setting the API key, loading discovery documents, and initializing auth, are done.

gapi.client.load(urlOrObject)

Loads the client library interface to a particular API with discovery document URL or JSON object. Returns a Promise-like goog.Thenable object that resolves when the API interface is loaded. The loaded API interface will be in the form gapi.client.api.collection.method. For example, the Moderator API would create methods like gapi.client.moderator.series.list.

Arguments:

Name

Type

Description

urlOrObject

string | object

The Discovery Document URL or parsed Discovery Document JSON object (Example).

Returns:

Type

Description

goog.Thenable

The return value is a Promise-like goog.Thenable object that resolves when the API interface is loaded.

gapi.client.load(name, version, callback)

Deprecated. Please load APIs with discovery documents. Loads the client library interface to a particular API. If a callback is not provided, a goog.Thenable is returned. The loaded API interface will be in the form gapi.client.api.collection.method. For example, the Moderator API would create methods like gapi.client.moderator.series.list.

Arguments:

Name

Type

Description

name

string

The name of the API to load.

version

string

The version of the API to load.

callback

function

(optional) the function that is called once the API interface is loaded. If not provided, a goog.Thenable is returned.

gapi.client.setApiKey(apiKey)

Sets the API key for the application, which can be found in the Developer Console. Some APIs require this to be set in order to work.

Arguments:

Name

Type

Description

apiKey

string

The API key to set.

gapi.client.setToken(tokenObject)

Sets the authentication token to use in requests. This should be used if the token was obtained without using the gapi.auth2 authentication library (for instance, when using Firebase to authenticate users).

Arguments:

API requests

gapi.client.request(args)

Creates a HTTP request for making RESTful requests.

Arguments:

Returns:

Type

Description

gapi.client.Request | undefined

The returned gapi.client.Request object implements goog.Thenable and can be used like a Promise that fulfills with the response object or rejects with a reason object.

gapi.client.Request

An object encapsulating an HTTP request. This object is not instantiated directly, rather it is returned by gapi.client.request. There are two ways to execute a request. We recommend that you treat the object as a promise and use the then method, but you can also use the execute method and pass in a callback.

gapi.client.Request.then(onFulfilled, onRejected, context)

For more information about using promises, see Using Promises.

gapi.client.Request.execute(callback)

Executes the request and runs the supplied callback on response.

Arguments:

Name

Type

Description

callback(jsonResp,rawResp)

function

The callback function which executes when the request succeeds or fails. jsonResp contains the response parsed as JSON. If the response is not JSON, this field will be false. rawResp is the HTTP response. It is JSON, and can be parsed to an object which includes body, headers, status, and statusText fields.

Batch API requests

gapi.client.newBatch()

Creates a batch object for batching individual requests.

Returns:

Type

Description

gapi.client.Batch | undefined

The returned gapi.client.Batch implements goog.Thenable interface and can be used like a Promise that fulfills with a batch response object and rejects with a reason object.

gapi.client.Batch

Represents an HTTP Batch operation. Individual HTTP requests are added with the add method and the batch can be executed using then or execute. We recommend that you treat the batch object as a promise and use then. This class defines the following methods:

gapi.client.Batch.add(request,opt_params)

Adds a gapi.client.Request to the batch.

Arguments:

gapi.client.Batch.then(onFulfilled, onRejected, context)

For more information about using promises, see Using Promises.

gapi.client.Batch.execute(callback)

Executes all requests in the batch. The supplied callback is executed on success or failure.

Name

Type

Description

callback(responseMap, rawBatchResponse)

function

The callback to execute when the batch returns. responseMap is an ID-response map of each requests response. rawBatchResponse is the same response, but as an unparsed JSON-string.

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Methods and Classes
    • Loading the client library
      • gapi.load(libraries, callbackOrConfig)
    • Client setup
      • gapi.client.init(args)
      • gapi.client.load(urlOrObject)
      • gapi.client.load(name, version, callback)
      • gapi.client.setApiKey(apiKey)
      • gapi.client.setToken(tokenObject)
      • gapi.client.request(args)
      • gapi.client.Request
    • Batch API requests
      • gapi.client.newBatch()
      • gapi.client.Batch
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档