コード例 #1
0
        /// <summary>
        /// Performs a post request to the Synology API
        /// </summary>
        /// <typeparam name="T">Type of result</typeparam>
        /// <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>
        /// <param name="additionalParams">Parameters of the request</param>
        /// <returns>Result of the request and the specific API/Method response</returns>
        internal ResultData <T> PostDataFromApi <T>(string cgi, string api, int version, string method, FormParameter[] additionalParams = null)
        {
            var uri = GetPostApiUrl(cgi, api, version, method);

            var allParameters = new[] {
                //new FormParameter("_sid", Sid),
                new FormParameter("api", api),
                new FormParameter("version", version),
                new FormParameter("method", method)
            }.Concat(additionalParams ?? new FormParameter[] { }).ToArray();

            using (var content = new FormParameterManager(allParameters))
            {
                var formContentTask = content.ToByteArrayAsync();
                formContentTask.Wait();

                var formContent = formContentTask.Result;

                _client.Headers[HttpRequestHeader.ContentType] = content.MultipartContent.Headers.ContentType.ToString().Replace("\"", string.Empty);

                var result = _client.UploadData(uri, formContent);

                _client.Headers[HttpRequestHeader.ContentType] = null;

                return(JsonConvert.DeserializeObject <ResultData <T> >(Encoding.Default.GetString(result)));
            }
        }
コード例 #2
0
        private async Task <T> GenericPostDataFromApiAsync <T>(string cgi, string api, int version, string method, FormParameter[] additionalParams = null) where T : ResultData
        {
            var uri = PostApiUrl(cgi, api, version, method);

            var allParameters = new[] {
                new FormParameter("api", api),
                new FormParameter("version", version),
                new FormParameter("method", method)
            }.Concat(additionalParams ?? new FormParameter[] { }).Where(t => t.MinVersion <= version).ToArray();

            using (var content = new FormParameterManager(allParameters))
            {
                var formContent = await content.ToByteArrayAsync();

                using (var requestContent = new ByteArrayContent(formContent))
                {
                    requestContent.Headers.ContentType = new MediaTypeHeaderValue(content.MultipartContent.Headers.ContentType.ToString().Replace("\"", string.Empty));

                    var result = await _client.PostAsync(uri, requestContent);

                    var data = await result.Content.ReadAsByteArrayAsync();

                    return(JsonConvert.DeserializeObject <T>(Encoding.UTF8.GetString(data)));
                }
            }
        }