/// <summary> /// Adds a snippet to a Square Online site or updates the existing snippet on the site. . /// The snippet code is appended to the end of the `head` element on every page of the site, except checkout pages. A snippet application can add one snippet to a given site. . /// You can call [ListSites]($e/Sites/ListSites) to get the IDs of the sites that belong to a seller.. /// __Note:__ Square Online APIs are publicly available as part of an early access program. For more information, see [Early access program for Square Online APIs](https://developer.squareup.com/docs/online-api#early-access-program-for-square-online-apis).. /// </summary> /// <param name="siteId">Required parameter: The ID of the site where you want to add or update the snippet..</param> /// <param name="body">Required parameter: An object containing the fields to POST for the request. See the corresponding object definition for field details..</param> /// <param name="cancellationToken"> cancellationToken. </param> /// <returns>Returns the Models.UpsertSnippetResponse response from the API call.</returns> public async Task <Models.UpsertSnippetResponse> UpsertSnippetAsync( string siteId, Models.UpsertSnippetRequest body, CancellationToken cancellationToken = default) { // the base uri for api requests. string baseUri = this.Config.GetBaseUri(); // prepare query string for API call. StringBuilder queryBuilder = new StringBuilder(baseUri); queryBuilder.Append("/v2/sites/{site_id}/snippet"); // process optional template parameters. ApiHelper.AppendUrlWithTemplateParameters(queryBuilder, new Dictionary <string, object>() { { "site_id", siteId }, }); // append request with appropriate headers and parameters var headers = new Dictionary <string, string>() { { "user-agent", this.UserAgent }, { "accept", "application/json" }, { "content-type", "application/json; charset=utf-8" }, { "Square-Version", this.Config.SquareVersion }, }; // append body params. var bodyText = ApiHelper.JsonSerialize(body); // prepare the API call request to fetch the response. HttpRequest httpRequest = this.GetClientInstance().PostBody(queryBuilder.ToString(), headers, bodyText); if (this.HttpCallBack != null) { this.HttpCallBack.OnBeforeHttpRequestEventHandler(this.GetClientInstance(), httpRequest); } httpRequest = await this.AuthManagers["global"].ApplyAsync(httpRequest).ConfigureAwait(false); // invoke request and get response. HttpStringResponse response = await this.GetClientInstance().ExecuteAsStringAsync(httpRequest, cancellationToken).ConfigureAwait(false); HttpContext context = new HttpContext(httpRequest, response); if (this.HttpCallBack != null) { this.HttpCallBack.OnAfterHttpResponseEventHandler(this.GetClientInstance(), response); } // handle errors defined at the API level. this.ValidateResponse(response, context); var responseModel = ApiHelper.JsonDeserialize <Models.UpsertSnippetResponse>(response.Body); responseModel.Context = context; return(responseModel); }
/// <summary> /// Adds a snippet to a Square Online site or updates the existing snippet on the site. . /// The snippet code is appended to the end of the `head` element on every page of the site, except checkout pages. A snippet application can add one snippet to a given site. . /// You can call [ListSites]($e/Sites/ListSites) to get the IDs of the sites that belong to a seller.. /// __Note:__ Square Online APIs are publicly available as part of an early access program. For more information, see [Early access program for Square Online APIs](https://developer.squareup.com/docs/online-api#early-access-program-for-square-online-apis).. /// </summary> /// <param name="siteId">Required parameter: The ID of the site where you want to add or update the snippet..</param> /// <param name="body">Required parameter: An object containing the fields to POST for the request. See the corresponding object definition for field details..</param> /// <returns>Returns the Models.UpsertSnippetResponse response from the API call.</returns> public Models.UpsertSnippetResponse UpsertSnippet( string siteId, Models.UpsertSnippetRequest body) { Task <Models.UpsertSnippetResponse> t = this.UpsertSnippetAsync(siteId, body); ApiHelper.RunTaskSynchronously(t); return(t.Result); }