Ejemplo n.º 1
0
        private string BuildURI()
        {
            var serviceName = type.GetCustomAttribute <Service>()?.path ?? type.Name;
            var apiName     = method.GetCustomAttribute <Resource>()?.name ?? method.Name;
            var suffix      = "";

            apiName = BindColonValues(apiName);
            apiName =
                string.Format(
                    apiName,
                    parameterData
                    .Where(m => m.type == ParameterType.Binding)
                    .Select(m => Uri.EscapeUriString(m.value.ToString()))
                    .ToArray());

            this.ExecuteResourceNameProcessors(ref apiName);

            var suffixParams =
                parameterData.Where(m => m.type == ParameterType.Suffix);

            foreach (var param in suffixParams)
            {
                suffix += "/" + param.value.ToString();
            }
            if (apiName.Length == 0)
            {
                suffix = suffix.TrimStart('/');
            }

            var requestUriParams =
                parameterData.Where(m => m.type == ParameterType.RequestUri);

            if (requestUriParams.Count() != 0)
            {
                suffix = "?" + RequestUriBuilder.Build(
                    requestUriParams.ToDictionary(x => x.name, y => y.value));
            }

            return($"{host}/{serviceName}/{apiName}{suffix}");
        }
Ejemplo n.º 2
0
        public HttpResponse GetResponse()
        {
            if (Config.logOutput)
            {
                Dump();
            }

            var httpMethodString = "GET";

            byte[] payload = null;

            switch (httpMethod)
            {
            case HttpMethod.Get: httpMethodString = "GET"; break;

            case HttpMethod.Post: httpMethodString = "POST"; break;

            case HttpMethod.Put: httpMethodString = "PUT"; break;

            case HttpMethod.Delete: httpMethodString = "DELETE"; break;
            }

            if (httpMethod.IsPayloadAllowed())
            {
                ParameterType paramType      = ParameterType.Ignore;
                string        requestPayload = "";
                var           parameters     = new Dictionary <string, object>();

                foreach (var param in parameterData)
                {
                    if (param.type == ParameterType.PostJson)
                    {
                        if (paramType == ParameterType.RequestUri)
                        {
                            throw new ArgumentException();
                        }
                        paramType = ParameterType.PostJson;

                        parameters[param.name] = param.value;
                    }
                    else if (param.type == ParameterType.RequestUri)
                    {
                        if (paramType == ParameterType.PostJson)
                        {
                            throw new ArgumentException();
                        }
                        paramType = ParameterType.RequestUri;

                        parameters[param.name] = param.value;
                    }
                }

                if (paramType == ParameterType.PostJson)
                {
                    requestPayload = JsonConvert.SerializeObject(parameters);

                    if (headers.ContainsKey(HeaderKey.ContentType) == false)
                    {
                        headers[HeaderKey.ContentType] = "application/json";
                    }
                }
                else if (paramType == ParameterType.RequestUri)
                {
                    requestPayload = RequestUriBuilder.Build(parameters);

                    if (headers.ContainsKey(HeaderKey.ContentType) == false)
                    {
                        headers[HeaderKey.ContentType] = "application/x-www-form-urlencoded";
                    }
                }

                if (requestPayload.Length > 0)
                {
                    payload = Encoding.UTF8.GetBytes(requestPayload);
                }
            }

            return(PerformRequest(uri, httpMethodString, headers, payload));
        }