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

MakePutRequestWithAuthTestAsync() public méthode

Calls MakePutRequestAsync(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 MakePutRequestWithAuthTestAsync ( String method ) : Task
method String The method to call
Résultat Task
		public virtual async Task<RequestResponse> MakePutRequestWithAuthTestAsync(String method, params KeyValuePair<String, String>[] arguments)
		{
			var response = await MakePutRequestAsync(method, arguments);
			if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
			{
				await RefreshTokenAsync();
				response = await MakePutRequestAsync(method, arguments);
			}

			return response;
		}

Usage Example

Exemple #1
0
        /// <summary>
        /// Renames the Device.
        /// </summary>
        /// <param name="newName">The new name.</param>
        /// <returns></returns>
        public async Task <Result> RenameAsync(String newName)
        {
            if (String.IsNullOrWhiteSpace(newName))
            {
                throw new ArgumentNullException(nameof(newName));
            }

            try
            {
                var result = await cloud.MakePutRequestWithAuthTestAsync($"devices/{Id}", new KeyValuePair <string, string>("name", newName));

                if (result.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    var r = result.AsResult();
                    if (String.IsNullOrWhiteSpace(r.Error))
                    {
                        r.Success = true;
                        ParticleCloud.SyncContext.InvokeIfRequired(() =>
                        {
                            Name = newName;
                        });
                    }

                    return(r);
                }
                else
                {
                    return(result.AsResult());
                }
            }
            catch (HttpRequestException re)
            {
                return(new Result
                {
                    Success = false,
                    Error = re.Message,
                    Exception = re
                });
            }
        }