Beispiel #1
0
        public DefaultApiWrapper(IApiAuthModel apiAuthContext, string baseparturl = null)
        {
            this.BasePartUrl    = baseparturl;
            this.apiAuthContext = apiAuthContext;

            this.ModelConfig = Nglib.APP.CODE.AttributesTools.FindObjectAttribute <ModelConfigAttribute>(typeof(TApiModel));

            if (string.IsNullOrWhiteSpace(this.BasePartUrl) && this.ModelConfig != null)
            {
                this.BasePartUrl = this.ModelConfig.ApiPartUrl;
            }
        }
Beispiel #2
0
        public static HttpRequestMessage PrepareRequestFromModel(object model, HttpMethod method)
        {
            if (model == null)
            {
                return(null);
            }
            DATA.BASICS.ModelConfigAttribute modelAttribute = Nglib.APP.CODE.AttributesTools.FindObjectAttribute <DATA.BASICS.ModelConfigAttribute>(model);
            if (modelAttribute == null)
            {
                throw new Exception("Invalid Model");
            }

            Type   modeltype = model.GetType();
            string QueryUrl  = modelAttribute.ApiPartUrl;

            Dictionary <string, Tuple <HttpRequestValueAttribute, object> > properties =
                Nglib.APP.CODE.AttributesTools.FindPropertiesValueAttribute <HTTPCLIENT.HttpRequestValueAttribute>(model);

            properties.Where(p => p.Value.Item1.Type == HttpRequestParameterType.Path).ToList().ForEach(p => { QueryUrl.Replace("{" + p.Value.Item1.RealName + "}", TransformValue(p.Value.Item1, p.Value.Item2) as string); });

            properties.Where(p => p.Value.Item1.Type == HttpRequestParameterType.Query).ToList().ForEach(p => {
                object val = TransformValue(p.Value.Item1, p.Value.Item2);
                if (val != null)
                {
                    if (!QueryUrl.Contains("?"))
                    {
                        QueryUrl += "?";
                    }
                    else
                    {
                        QueryUrl += "&";
                    }
                    QueryUrl += $"{p.Value.Item1.RealName}={val}";
                }
            });

            HttpRequestMessage req = new HttpRequestMessage(method, QueryUrl);

            return(req);
        }