/// <summary> /// Execute the command on the device. /// </summary> /// <exception cref="ArgumentNullException">Config is null. -or- ParameterSet is null.</exception> /// <exception cref="ArgumentException">Config is invalid. -or- ParameterSet is invalid.</exception> /// <exception cref="NetOpnApiHttpException">An HTTP error is raised or an invalid HTTP status code is returned.</exception> /// <exception cref="NetOpnApiNotAuthorizedException">The configured API key does not have sufficient access.</exception> /// <exception cref="NetOpnApiNotImplementedException">The command is not implemented on the device.</exception> /// <exception cref="NetOpnApiInvalidResponseException">The command returns an invalid response.</exception> public static void Execute <TParameterSet>(this ICommandWithParameterSet <TParameterSet> self) where TParameterSet : IParameterSet => ExecuteCommand( self, self.CreateRequest, self.SetEmptyResponse, self.SetResponse );
private static HttpRequestMessage CreateRequest <T>(this ICommandWithParameterSet <T> self, IDeviceConfig cfg) where T : IParameterSet => self.CommonCreateRequest( self.CreateRequestUrl(), cfg, self.UsePost ? HttpMethod.Post : HttpMethod.Get, self.UsePost ? self.CreatePostContent() : null );
private static Uri CreateRequestUrl <T>(this ICommandWithParameterSet <T> self) where T : IParameterSet { var sb = self.CreateRequestUrlCommon(); self.AppendCommandParametersToUrl(sb); return(new Uri(sb.ToString())); }
/// <summary> /// Try to executed the command. /// </summary> /// <param name="self"></param> /// <param name="errorCode">Returns the error code if command execution fails.</param> /// <returns></returns> public static bool TryExecute <TParameterSet>(this ICommandWithParameterSet <TParameterSet> self, out ErrorCode errorCode) where TParameterSet : IParameterSet { errorCode = ErrorCode.NoError; try { self.Execute(); return(true); } catch (NetOpnApiException e) { errorCode = e.Code; return(false); } }
private static StringContent CreatePostContent <T>(this ICommandWithParameterSet <T> self) where T : IParameterSet { var obj = self?.ParameterSet?.GetRequestPayload(); var type = self?.ParameterSet?.GetRequestPayloadDataType(); if (obj == null) { return(null); } var body = JsonSerializer.Serialize(obj, type); return(new StringContent(body) { Headers = { ContentType = new MediaTypeHeaderValue("application/json") } }); }
private static void AppendCommandParametersToUrl <T>(this ICommandWithParameterSet <T> self, StringBuilder builder) where T : IParameterSet { if (self is null) { return; } if (self.ParameterSet is null) { return; } var parameters = self.ParameterSet.GetUrlParameters(); if (parameters != null && parameters.Count > 0) { foreach (var parameter in parameters) { builder.Append('/').Append(HttpUtility.UrlEncode(parameter)); } } var query = self.ParameterSet.GetQueryParameters(); if (query != null && query.Count > 0) { var first = true; foreach (var param in query) { builder.Append(first ? '?' : '&'); first = false; builder.Append(HttpUtility.UrlEncode(param.Key)).Append('=').Append(HttpUtility.UrlEncode(param.Value)); } } }