private static async Task <T> GenericPostDataFromApiAsync <T>(this ISynologyConnection connection, string cgi, string api, int version, string method, FormParameter[] additionalParams = null) where T : ResultData { var uri = PostApiUrl(connection, cgi, api, version, method); var client = new RestClient(uri); var request = new RestRequest(Method.POST); request.AddParameter("_sid", connection.GetSid()); request.AddParameter("api", api); request.AddParameter("version", version); request.AddParameter("method", method); foreach (var temp in additionalParams) { if (temp is FileFormDataParameter) { request.AddFile("file", temp.Value, "application/x-bittorrent"); } else if (!temp.Empty) { request.AddParameter(temp.Name, temp.Value); } } var result = await client.ExecuteAsync(request); var data = result.Content; connection.Logger.LogDebug(data); return(JsonConvert.DeserializeObject <T>(data)); }
/// <summary> /// Get Uri for post operations /// </summary> /// <param name="connection"></param> /// <param name="cgi">CGI path handling the request</param> /// <param name="api">API name handling the request</param> /// <param name="version">Version of the api</param> /// <param name="method">Method of the API</param> /// <returns>The Uri object where the request has to be sent</returns> private static Uri PostApiUrl(this ISynologyConnection connection, string cgi, string api, int version, string method) { var url = new QueryStringManager(cgi); url.AddParameters(new[] { new QueryStringParameter("_sid", connection.GetSid()), }); var res = url.ToString(); connection.Logger.LogDebug($"Created API Url for POST: {res}"); return(new Uri(connection.Client.BaseAddress, res)); }
/// <summary> /// /// </summary> /// <param name="connection"></param> /// <param name="cgi"></param> /// <param name="api"></param> /// <param name="version"></param> /// <param name="method"></param> /// <param name="additionalParams"></param> /// <returns></returns> private static string GetApiUrl(this ISynologyConnection connection, string cgi, string api, int version, string method, QueryStringParameter[] additionalParams = null) { var url = new QueryStringManager(cgi); additionalParams = additionalParams ?? new QueryStringParameter[] { }; url.AddParameters(additionalParams.Concat(new[] { new QueryStringParameter("_sid", connection.GetSid()), new QueryStringParameter("api", api), new QueryStringParameter("version", version), new QueryStringParameter("method", method) }).Where(t => t.MinVersion <= version)); var res = url.ToString(); connection.Logger.LogDebug($"Created API Url for GET: {res}"); return(res); }