forked from FarmMaps/FarmMapsApiClient
		
	changed some code around
This commit is contained in:
		@@ -1,5 +1,4 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections;
 | 
			
		||||
using System.IO;
 | 
			
		||||
using System.Net.Http;
 | 
			
		||||
using System.Net.Mime;
 | 
			
		||||
@@ -69,6 +68,8 @@ namespace FarmmapsApi.Services
 | 
			
		||||
            contentStream.ThrowIfNull(nameof(contentStream));
 | 
			
		||||
            
 | 
			
		||||
            _streamLength = ContentStream.CanSeek ? ContentStream.Length : UnknownSize;
 | 
			
		||||
 | 
			
		||||
            body.ChunkSize = ChunkSize;
 | 
			
		||||
            Body = body;
 | 
			
		||||
            Path = "api/v1/file";
 | 
			
		||||
            HttpClient = httpClient;
 | 
			
		||||
@@ -93,16 +94,12 @@ namespace FarmmapsApi.Services
 | 
			
		||||
        /// <summary>Creates a request to initialize a request.</summary>
 | 
			
		||||
        private HttpRequestMessage CreateInitializeRequest()
 | 
			
		||||
        {
 | 
			
		||||
            var baseAddres = HttpClient.BaseAddress;
 | 
			
		||||
            var uri = new Uri($"{baseAddres.Scheme}://{baseAddres.Host}:{baseAddres.Port}");
 | 
			
		||||
            var builder = new RequestBuilder()
 | 
			
		||||
            {
 | 
			
		||||
                BaseUri = uri,
 | 
			
		||||
                BaseUri = HttpClient.BaseAddress,
 | 
			
		||||
                Path = Path,
 | 
			
		||||
                Method = HttpMethod,
 | 
			
		||||
            };
 | 
			
		||||
            
 | 
			
		||||
            SetAllPropertyValues(builder);
 | 
			
		||||
 | 
			
		||||
            HttpRequestMessage request = builder.CreateRequest();
 | 
			
		||||
            if (ContentType != null)
 | 
			
		||||
@@ -121,40 +118,5 @@ namespace FarmmapsApi.Services
 | 
			
		||||
 | 
			
		||||
            return request;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Reflectively enumerate the properties of this object looking for all properties containing the 
 | 
			
		||||
        /// RequestParameterAttribute and copy their values into the request builder.
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        private void SetAllPropertyValues(RequestBuilder requestBuilder)
 | 
			
		||||
        {
 | 
			
		||||
            Type myType = this.GetType();
 | 
			
		||||
            var properties = myType.GetProperties();
 | 
			
		||||
 | 
			
		||||
            foreach (var property in properties)
 | 
			
		||||
            {
 | 
			
		||||
                var attribute = property.GetCustomAttribute<RequestParameterAttribute>();
 | 
			
		||||
                if (attribute != null)
 | 
			
		||||
                {
 | 
			
		||||
                    string name = attribute.Name ?? property.Name.ToLower();
 | 
			
		||||
                    object value = property.GetValue(this, null);
 | 
			
		||||
                    if (value != null)
 | 
			
		||||
                    {
 | 
			
		||||
                        if (!(value is string) && value is IEnumerable valueAsEnumerable)
 | 
			
		||||
                        {
 | 
			
		||||
                            foreach (var elem in valueAsEnumerable)
 | 
			
		||||
                            {
 | 
			
		||||
                                requestBuilder.AddParameter(attribute.Type, name, Utilities.ConvertToString(elem));
 | 
			
		||||
                            }
 | 
			
		||||
                        }
 | 
			
		||||
                        else
 | 
			
		||||
                        {
 | 
			
		||||
                            // Otherwise just convert it to a string.
 | 
			
		||||
                            requestBuilder.AddParameter(attribute.Type, name, Utilities.ConvertToString(value));
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user