Exemple #1
0
        public void TestContentTypeOverride()
        {
            RequestMap        requestMap  = new RequestMap("{\n\"id\":\"1\"\n}");
            RequestMap        responseMap = new RequestMap("{\"Account\":{\"Status\":\"true\",\"Listed\":\"true\",\"ReasonCode\":\"S\",\"Reason\":\"STOLEN\"}}");
            TestApiController controller  = new TestApiController();

            controller.SetRestClient(mockClient(HttpStatusCode.OK, responseMap));

            // new Tuple<string, string, List<string>, List<string>>("/test1", null, headerList, queryList);
            var config   = new OperationConfig("/test1", "create", headerList, queryList);
            var metadata = new OperationMetadata("0.0.1", "http://locahost:8081", null, true, "text/json");

            RestyRequest request = controller.GetRequest(config, metadata, new TestBaseObject(requestMap));

            Assert.AreEqual("http://locahost:8081/test1", request.AbsoluteUrl.ToString());
            Assert.AreEqual(true, request.HasBody);
            Assert.AreEqual("POST", request.Method.ToString());
            Assert.AreEqual(true, request.Parameters.Exists(i => i.Name.Equals("Content-Type")));
            Assert.AreEqual(true, request.Parameters.Exists(i => i.Name.Equals("Content-Type")));

            Assert.AreEqual("text/json; charset=utf-8", request.Parameters.Find(i => i.Name.Equals("Accept")).Value.ToString());
            Assert.AreEqual("text/json; charset=utf-8", request.Parameters.Find(i => i.Name.Equals("Content-Type")).Value.ToString());



            String authentication = request.Parameters.Find(i => i.Name.Equals("Authorization")).Value.ToString();

            Assert.AreEqual(true, authentication.Contains("oauth_body_hash"));
        }
Exemple #2
0
        private RestyRequest GetRequest(OperationConfig config, OperationMetadata metadata, RequestMap requestMap)
        {
            RestyRequest restyRequest = null;
            IDictionary <string, object> dictionary  = requestMap.Clone();
            IDictionary <string, object> dictionary2 = Util.SubMap(dictionary, config.HeaderParams);
            Uri uRL     = this.GetURL(config, metadata, dictionary);
            Uri baseUrl = new Uri(string.Concat(new object[]
            {
                uRL.Scheme,
                "://",
                uRL.Host,
                ":",
                uRL.Port
            }));
            CryptographyInterceptor cryptographyInterceptor = _apiConfig?.GetCryptographyInterceptor(uRL.AbsolutePath) ?? ApiConfig.GetCryptographyInterceptor(uRL.AbsolutePath);
            string action = config.Action;

            if (!(action == "create"))
            {
                if (!(action == "delete"))
                {
                    if (!(action == "update"))
                    {
                        if (action == "read" || action == "list" || action == "query")
                        {
                            restyRequest = new RestyRequest(uRL, Method.GET);
                        }
                    }
                    else
                    {
                        restyRequest = new RestyRequest(uRL, Method.PUT);
                        if (cryptographyInterceptor != null)
                        {
                            dictionary = cryptographyInterceptor.Encrypt(dictionary);
                        }
                        if (config.RequestType == DataType.Json)
                        {
                            restyRequest.AddJsonBody(dictionary);
                        }
                        else if (config.RequestType == DataType.Xml)
                        {
                            restyRequest.AddXmlBody(dictionary);
                        }
                    }
                }
                else
                {
                    restyRequest = new RestyRequest(uRL, Method.DELETE);
                }
            }
            else
            {
                restyRequest = new RestyRequest(uRL, Method.POST);
                if (cryptographyInterceptor != null)
                {
                    dictionary = cryptographyInterceptor.Encrypt(dictionary);
                }
                if (config.RequestType == DataType.Json)
                {
                    restyRequest.AddJsonBody(dictionary);
                }
                else if (config.RequestType == DataType.Xml)
                {
                    restyRequest.AddXmlBody(dictionary);
                }
            }
            if (config.RepsonseType == DataType.Json)
            {
                restyRequest.AddHeader("Accept", "application/json");
            }
            else if (config.RepsonseType == DataType.Xml)
            {
                restyRequest.AddHeader("Accept", "application/xml");
            }
            if (config.RequestType == DataType.Json)
            {
                restyRequest.AddHeader("Content-Type", "application/json");
            }
            else if (config.RequestType == DataType.Xml)
            {
                restyRequest.AddHeader("Content-Type", "application/xml");
            }

            restyRequest.AddHeader("User-Agent", "CSharp-SDK-Unofficial/" + this.apiVersion);
            foreach (KeyValuePair <string, object> current in dictionary2)
            {
                restyRequest.AddHeader(current.Key, current.Value.ToString());
            }
            (_apiConfig?.GetAuthentication() ?? ApiConfig.GetAuthentication()).SignRequest(uRL, restyRequest);
            restyRequest.AbsoluteUrl = uRL;
            restyRequest.BaseUrl     = baseUrl;
            restyRequest.interceptor = cryptographyInterceptor;
            return(restyRequest);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="action"></param>
        /// <param name="config"></param>
        /// <param name="inputMap"></param>
        /// <returns></returns>
        protected RestyRequest GetRequest(OperationConfig config, OperationMetadata metadata, RequestMap requestMap)
        {
            RestyRequest request = null;


            // separate the parameterMap and headerMap from requestMap
            IDictionary <String, Object> paramterMap = requestMap.Clone();
            IDictionary <String, Object> headerMap   = Util.SubMap(paramterMap, config.HeaderParams);

            Uri    url           = GetURL(config, metadata, paramterMap);
            String baseUriString = url.Scheme + "://" + url.Host + ":" + url.Port;
            Uri    baseUrl       = new Uri(baseUriString);

            string contentType = "application/json; charset=utf-8";

            if (metadata.ContentTypeOverride != null)
            {
                contentType = metadata.ContentTypeOverride + "; charset=utf-8";
            }

            CryptographyInterceptor interceptor = ApiConfig.GetCryptographyInterceptor(url.AbsolutePath);



            switch (config.Action)
            {
            case "create":
                request = new RestyRequest(url, Method.POST);

                //arizzini: adding cryptography interceptor for POST
                if (interceptor != null)
                {
                    paramterMap = interceptor.Encrypt(paramterMap);
                }
                request.AddJsonBody(paramterMap);
                break;

            case "delete":
                request = new RestyRequest(url, Method.DELETE);
                break;

            case "update":
                request = new RestyRequest(url, Method.PUT);

                //arizzini: adding cryptography interceptor for PUT
                if (interceptor != null)
                {
                    paramterMap = interceptor.Encrypt(paramterMap);
                }
                request.AddJsonBody(paramterMap);
                break;

            case "read":
            case "list":
            case "query":
                request = new RestyRequest(url, Method.GET);
                break;
            }

            request.AddHeader("Accept", contentType);
            if (request.HasBody)
            {
                request.AddHeader("Content-Type", contentType);
            }
            request.AddHeader("User-Agent", Constants.getCoreVersion() + "/" + metadata.Version);

            //arizzini: adding the header paramter support.
            foreach (KeyValuePair <string, object> entry in headerMap)
            {
                request.AddHeader(entry.Key, entry.Value.ToString());
            }

            ApiConfig.GetAuthentication().SignRequest(url, request);


            request.AbsoluteUrl = url;
            request.BaseUrl     = baseUrl;
            request.interceptor = interceptor;


            return(request);
        }