Exemplo n.º 1
0
        private static HttpContent GetContent(WebApiRestRequest request)
        {
            if (request.File != null)
            {
                var formData = new MultipartFormDataContent();
                foreach (var parameter in request.Parameters)
                {
                    var content = new StringContent(parameter.Value.ToString());
                    formData.Add(content, $"\"{parameter.Key}\"");
                }
                var byteContent = new ByteArrayContent(request.File);
                formData.Add(byteContent, "\"file\"", $"\"{request.FileName}\"");
                return(formData);
            }

            if (request.Body == null)
            {
                return(null);
            }

            var body        = TrelloConfiguration.Serializer.Serialize(request.Body);
            var jsonContent = new StringContent(body);

            jsonContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            return(jsonContent);
        }
Exemplo n.º 2
0
 private static string GetFullResource(WebApiRestRequest request)
 {
     if (request.File != null)
     {
         return($"{_trelloApiBaseUrl}/{request.Resource}");
     }
     return($"{_trelloApiBaseUrl}/{request.Resource}?{string.Join("&", request.Parameters.Select(kvp => $"{kvp.Key}={kvp.Value}").ToList())}");
 }
Exemplo n.º 3
0
 private HttpContent GetContent(WebApiRestRequest request)
 {
     if (request.File != null)
     {
         var formData = new MultipartFormDataContent();
         foreach (var parameter in request.Parameters)
         {
             var content = new StringContent(parameter.Value.ToString());
             formData.Add(content, $"\"{parameter.Key}\"");
         }
         var byteContent = new ByteArrayContent(request.File);
         formData.Add(byteContent, "\"file\"", $"\"{request.FileName}\"");
         return(formData);
     }
     return(new ObjectContent(GetRequestType(request), request.Body, _formatter));
 }
        /// <summary>
        /// Creates a general request using a collection of objects and an additional parameter to
        /// generate the resource string and an object to supply additional parameters.
        /// </summary>
        /// <param name="endpoint">The method endpoint the request calls.</param>
        /// <param name="parameters">A list of paramaters to include in the request.</param>
        /// <returns>An IRestRequest instance which can be sent to an IRestClient.</returns>
        public IRestRequest Create(string endpoint, IDictionary <string, object> parameters = null)
        {
            var request = new WebApiRestRequest {
                Resource = endpoint
            };

            if (parameters != null)
            {
                foreach (var parameter in parameters)
                {
                    if (parameter.Key == RestFile.ParameterKey)
                    {
                        var rf = (RestFile)parameter.Value;
                        request.AddFile(parameter.Key, rf.ContentBytes, rf.FileName);
                    }
                    else
                    {
                        request.AddParameter(parameter.Key, parameter.Value);
                    }
                }
            }

            return(request);
        }
Exemplo n.º 5
0
 private static Type GetRequestType(WebApiRestRequest request)
 {
     return(request.Body?.GetType() ?? typeof(object));
 }