Ejemplo n.º 1
0
        public RestRequest Build()
        {
            if (httpMethod == null)
            {
                throw new InvalidOperationException("The HTTP method is required");
            }
            if (String.IsNullOrEmpty(resourceName))
            {
                throw new InvalidOperationException("The resource name is required");
            }

            var request = new RestRequest(httpMethod, resourceName)
            {
                Authenticate  = authenticate,
                Authenticator = requestAuthenticator,
                Returned      = returned
            };

            var simpleParams = parameters.Where(x => !x.IsBody() && !x.IsFile());

            foreach (var parameter in simpleParams)
            {
                request.Parameters.Add(parameter);
            }

            var body  = parameters.FirstOrDefault(x => x.IsBody());
            var files = parameters.Where(x => x.IsFile()).ToList();

            if (files.Count > 1)
            {
                if (body == null)
                {
                    body = new RequestBody();
                }

                if (!(body is IMultipartBody))
                {
                    throw new NotSupportedException("A body was found but is not multi-partable");
                }

                foreach (var file in files)
                {
                    var filePart = file as IBodyPart;
                    if (filePart == null)
                    {
                        throw new InvalidOperationException("The file is not body part");
                    }

                    ((IMultipartBody)body).AddPart(filePart);
                }
            }
            else if (files.Count == 1)
            {
                request.Parameters.Add(files[0]);
            }

            if (body != null)
            {
                if (files.Count == 1)
                {
                    var multipart = new RequestBody();
                    multipart.AddPart(body as IBodyPart);
                    multipart.AddPart(files[0] as IBodyPart);
                    request.Parameters.Add(multipart);
                }
                else
                {
                    request.Parameters.Add(body);
                }
            }

            return(request);
        }
Ejemplo n.º 2
0
 internal static HttpContent GetHttpContent(this IRequestParameter parameter, IRestClient client)
 {
     return(RequestBody.CreateContent(parameter, client));
 }