Particle.ParticleCloud.MakePostRequestWithAuthTestAsync C# (CSharp) Méthode

MakePostRequestWithAuthTestAsync() public méthode

Calls MakePostRequestAsync(string, KeyValuePair{string, string}[]) and if it returns a status code of Unauthorized try s to refresh the token and makes the request again
public MakePostRequestWithAuthTestAsync ( String method ) : Task
method String The method to call
Résultat Task
		public virtual async Task<RequestResponse> MakePostRequestWithAuthTestAsync(String method, params KeyValuePair<String, String>[] arguments)
		{
			var response = await MakePostRequestAsync(method, arguments);
			if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
			{
				await RefreshTokenAsync();
				response = await MakePostRequestAsync(method, arguments);
			}

			return response;
		}

Usage Example

Exemple #1
0
        /// <summary>
        /// Calls the function asynchronous.
        /// </summary>
        /// <param name="functionName">Name of the function.</param>
        /// <param name="arg">The argument.</param>
        /// <returns></returns>
        public async Task <Result <int> > CallFunctionAsync(String functionName, String arg)
        {
            if (String.IsNullOrWhiteSpace(functionName))
            {
                throw new ArgumentNullException(nameof(functionName));
            }

            try
            {
                var response = await cloud.MakePostRequestWithAuthTestAsync($"devices/{Id}/{Uri.EscapeUriString(functionName)}", new KeyValuePair <string, string>("arg", arg));

                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    var returnValue = response.Response.SelectToken("return_value");
                    return(new Result <int>(true, (int)returnValue.Value <long>()));
                }
                else
                {
                    return(response.AsResult <int>());
                }
            }
            catch (HttpRequestException re)
            {
                return(new Result <int>
                {
                    Success = false,
                    Error = re.Message,
                    Exception = re
                });
            }
        }