Exemple #1
0
        protected ApiRequestBuildOption BuildRequestOption(ApiRequestBuildOption requestOption)
        {
            var option = requestOption?.Clone() as ApiRequestBuildOption;

            if (option == null)
            {
                option = this.DefaultRequestBuildOption?.Clone() as ApiRequestBuildOption;
            }
            else if (this.DefaultRequestBuildOption != null)
            {
                if (option.DefaultLocationType == ApiSerializeLocationType.NotSet)
                {
                    option.DefaultLocationType = this.DefaultRequestBuildOption.DefaultLocationType;
                }
                if (option.DefaultTextCaseType == ApiSerializeTextCaseType.NotSet)
                {
                    option.DefaultTextCaseType = this.DefaultRequestBuildOption.DefaultTextCaseType;
                }
                if (option.DefaultDownloadFileMode == DownloadFileMode.NotSet)
                {
                    option.DefaultDownloadFileMode = this.DefaultRequestBuildOption.DefaultDownloadFileMode;
                }
                if (option.DefaultEnumSerializeType == ApiEnumSerializeType.NotSet)
                {
                    option.DefaultEnumSerializeType = this.DefaultRequestBuildOption.DefaultEnumSerializeType;
                }
            }

            return(option);
        }
        public object Clone()
        {
            ApiRequestBuildOption option = new ApiRequestBuildOption();

            option.DefaultLocationType      = this.DefaultLocationType;
            option.DefaultTextCaseType      = this.DefaultTextCaseType;
            option.DefaultEnumSerializeType = this.DefaultEnumSerializeType;
            option.DefaultDownloadFileMode  = this.DefaultDownloadFileMode;
            option.SerializeObjectCallBack  = this.SerializeObjectCallBack;
            option.JsonSerializerSettings   = this.JsonSerializerSettings;
            return(option);
        }
Exemple #3
0
 public async Task <ApiResponse <T> > PostByModelAsync <T>(object model, string url = null, ApiRequestBuildOption requestOption = null, ApiResultBuildOption <T> resultOption = null) where T : class, new()
 {
     return(await PostByModelAsync(model, null, url, requestOption, resultOption));
 }
Exemple #4
0
        public async Task <ApiResponse <T> > PostByModelAsync <T>(object model, string controllerUrl, string url = null, ApiRequestBuildOption requestOption = null, ApiResultBuildOption <T> resultOption = null) where T : class, new()
        {
            var reqOption = this.BuildRequestOption(requestOption);

            if (reqOption.DefaultLocationType == ApiSerializeLocationType.Auto)
            {
                reqOption.DefaultLocationType = ApiSerializeLocationType.FromForm;
            }
            var resOption = this.BuildResultOption(resultOption);
            var request   = ApiRequest.FromModel(model, url, reqOption);

            request.ApiUrl = ApiRequest.CombineUrl(controllerUrl, request.ApiUrl);
            var response = await this.PostAsync <T>(request, resOption);

            response.SetModelStatus();
            return(response);
        }
Exemple #5
0
        public async Task <FileResponse> DownloadFileByModelAsync(object model, string controllerUrl, string url = null, ApiRequestBuildOption requestOption = null)
        {
            var reqOption = this.BuildRequestOption(requestOption);

            if (reqOption.DefaultLocationType == ApiSerializeLocationType.Auto)
            {
                reqOption.DefaultLocationType = ApiSerializeLocationType.FromQuery;
            }
            var request = ApiRequest.FromModel(model, url, reqOption);

            request.ApiUrl = ApiRequest.CombineUrl(controllerUrl, request.ApiUrl);
            return(await DownloadFileAsync(request));
        }
Exemple #6
0
 public async Task <FileResponse> DownloadFileByModelAsync(object model, string url = null, ApiRequestBuildOption requestOption = null)
 {
     return(await DownloadFileByModelAsync(model, null, url, requestOption));
 }
        public void ParseModel(object model, string url = null, ApiRequestBuildOption option = null)
        {
            if (model == null)
            {
                return;
            }

            if (string.IsNullOrEmpty(url) == false)
            {
                this.ApiUrl = url;
            }
            else
            {
                var nm = model.GetType().Name;

                if (nm.EndsWith("Request"))
                {
                    nm = nm.Substring(0, nm.LastIndexOf("Request"));
                }
                this.ApiUrl = nm;
            }

            if (option?.SerializeObjectCallBack != null)
            {
                this.SerializeObjectCallBack = option?.SerializeObjectCallBack;
            }

            var type = model.GetType();
            var defaultLocationType = option?.DefaultLocationType ?? ApiSerializeLocationType.Ignore;

            if (defaultLocationType == ApiSerializeLocationType.NotSet || defaultLocationType == ApiSerializeLocationType.Auto)
            {
                defaultLocationType = ApiSerializeLocationType.Ignore;
            }

            defaultLocationType = GetSerializeLocationType(type, defaultLocationType);


            var defaultTextCaseType = option?.DefaultTextCaseType ?? ApiSerializeTextCaseType.None;

            if (defaultTextCaseType == ApiSerializeTextCaseType.NotSet)
            {
                defaultTextCaseType = ApiSerializeTextCaseType.None;
            }

            defaultTextCaseType = GetSerializeTextCaseType(type, defaultTextCaseType);

            var defaultEnumSerializeType = option?.DefaultEnumSerializeType ?? ApiEnumSerializeType.Value;

            if (defaultEnumSerializeType == ApiEnumSerializeType.NotSet)
            {
                defaultEnumSerializeType = ApiEnumSerializeType.Value;
            }

            defaultTextCaseType = GetSerializeTextCaseType(type, defaultTextCaseType);

            this.DownloadFileMode = option?.DefaultDownloadFileMode ?? DownloadFileMode.Get;
            if (DownloadFileMode == DownloadFileMode.NotSet)
            {
                DownloadFileMode = DownloadFileMode.Get;
            }

            StringWriter sw = new StringWriter(this.JsonBuilder);
            var          jw = new JsonTextWriter(sw);

            jw.WriteStartObject();
            var properties = type.GetProperties();

            foreach (var p in properties)
            {
                object[] attrs;

                attrs = p.GetCustomAttributes(typeof(ApiUrlAttribute), true);
                if (attrs != null && attrs.Count() > 0)
                {
                    var apiUrl = Convert.ToString(p.GetValue(model));
                    if (ApiUrl != null)
                    {
                        this.ApiUrl = apiUrl;
                    }

                    continue;
                }

                var locationType = GetSerializeLocationType(p, defaultLocationType);
                var textCaseType = GetSerializeTextCaseType(p, defaultTextCaseType);

                if (locationType == ApiSerializeLocationType.Ignore)
                {
                    continue;
                }

                var    value    = p.GetValue(model);
                string strValue = null;
                if (p.PropertyType.IsEnum)
                {
                    var serizeType = GetEnumSerializeType(p, defaultEnumSerializeType);
                    if (serizeType == ApiEnumSerializeType.Name)
                    {
                        strValue = Convert.ToString(value);
                    }
                    else
                    {
                        strValue = EnumHelper.Serialize(value);
                    }
                }

                var name = p.Name;
                if (textCaseType == ApiSerializeTextCaseType.CamelCase)
                {
                    name = name.ToCamelCase();
                }

                if (p.PropertyType.IsAssignableFrom(typeof(ISerializable)))
                {
                    var request = new ApiRequest();

                    request.ParseModel(value, this.ApiUrl, option);

                    this.ApiUrl = request.ApiUrl;

                    foreach (var key in request.QueryStrings.Keys)
                    {
                        this.QueryStrings[key] = request.QueryStrings[key];
                    }

                    foreach (var key in request.Headers.Keys)
                    {
                        this.Headers[key] = request.Headers[key];
                    }

                    var childJson = request.JsonBuilder.ToString();
                    if (string.IsNullOrEmpty(childJson) == false)
                    {
                        jw.WritePropertyName(name);
                        jw.WriteRawValue(childJson);
                    }

                    foreach (var d in request.Objects)
                    {
                        var obj = d as ISerializable;
                        if (obj != null)
                        {
                            this.AddObject(obj);
                        }
                    }

                    foreach (var file in request.Files)
                    {
                        this.AddFile(file.Name, file.FileName, file.Bytes);
                    }
                    return;
                }

                if (locationType == ApiSerializeLocationType.FromQuery)
                {
                    this.AddQueryParameter(name, strValue ?? Convert.ToString(value));
                }
                else if (locationType == ApiSerializeLocationType.FromHeader)
                {
                    this.AddHeaderParameter(name, strValue ?? Convert.ToString(value));
                }
                else // if (locationType == ApiSerializeLocationType.FromForm)
                {
                    attrs = p.GetCustomAttributes(typeof(ApiFileAttribute), true);
                    if (attrs != null && attrs.Count() > 0)
                    {
                        var attr = attrs[0] as ApiFileAttribute;
                        name = attr.Name;
                        var fileName = attr.FileName;
                        if (string.IsNullOrEmpty(attr.FileNameProperty) == false)
                        {
                            var fileNameProperty = type.GetProperty(attr.FileNameProperty);
                            var fileNameObj      = fileNameProperty.GetValue(model);
                            if (fileNameObj != null)
                            {
                                var str = Convert.ToString(fileNameObj);
                                name     = name ?? str;
                                fileName = fileName ?? str;
                            }
                        }
                        if (p.PropertyType.IsAssignableFrom(typeof(byte[])))
                        {
                            var bytes = value as byte[];
                            this.AddFile(name, fileName, bytes);
                        }
                        else if (p.PropertyType.IsAssignableFrom(typeof(Stream)))
                        {
                            var stream = value as Stream;
                            this.AddFile(name, fileName, stream);
                        }
                        else if (p.PropertyType.IsAssignableFrom(typeof(string)) || p.PropertyType.IsAssignableFrom(typeof(String)))
                        {
                            var filePath = value as string;
                            this.AddFile(name, fileName, filePath);
                        }
                    }
                    else
                    {
                        jw.WritePropertyName(name);
                        jw.WriteValue(value);
                    }
                }
            }

            jw.WriteEndObject();
        }
        public static ApiRequest FromModel <T>(T model, string controllerUrl, string url = null, ApiRequestBuildOption option = null) where T : class
        {
            if (model == null)
            {
                return(new ApiRequest());
            }

            var request = new ApiRequest();

            request.ParseModel(model, url, option);
            return(request);
        }
 public static ApiRequest FromModel <T>(T model, string url = null, ApiRequestBuildOption option = null) where T : class
 {
     return(FromModel(model, url, null));
 }