/// <summary>
        /// Send a variety for form params. Returns file count and body params
        /// </summary>
        /// <param name="SendMixedArrayInput">Object containing request parameters</param>
        /// <return>Returns the ServerResponse response from the API call</return>
        public ServerResponse SendMixedArray(SendMixedArrayInput input)
        {
            Task <ServerResponse> t = SendMixedArrayAsync(input);

            Task.WaitAll(t);
            return(t.Result);
        }
Beispiel #2
0
        public async Task TestSendMixedArray()
        {
            // Parameters for the API call
            SendMixedArrayInput input = new SendMixedArrayInput();

            input.File     = TestHelper.GetFile("https://dl.dropboxusercontent.com/u/31838656/binary.png");
            input.Integers = APIHelper.JsonDeserialize <List <int> >("[1,2,3,4,5]");
            input.Models   = APIHelper.JsonDeserialize <List <Employee> >("[{\"name\":\"Shahid Khaliq\",\"age\":5147483645,\"address\":\"H # 531, S # 20\",\"uid\":\"123321\",\"salary\":20000,\"department\":\"Software Development\",\"joiningDay\":\"Saturday\",\"workingDays\":[\"Monday\",\"Tuesday\",\"Friday\"],\"boss\":{\"name\":\"Zeeshan Ejaz\",\"age\":5147483647,\"address\":\"I-9/1\",\"uid\":\"241123\"},\"dependents\":[{\"name\":\"Future Wife\",\"age\":5147483649,\"address\":\"H # 531, S # 20\",\"uid\":\"123412\"},{\"name\":\"Future Kid\",\"age\":5147483648,\"address\":\"H # 531, S # 20\",\"uid\":\"312341\"}]}, {\"name\":\"Shahid Khaliq\",\"age\":5147483645,\"address\":\"H # 531, S # 20\",\"uid\":\"123321\",\"salary\":20000,\"department\":\"Software Development\",\"joiningDay\":\"Saturday\",\"workingDays\":[\"Monday\",\"Tuesday\",\"Friday\"],\"boss\":{\"name\":\"Zeeshan Ejaz\",\"age\":5147483647,\"address\":\"I-9/1\",\"uid\":\"241123\"},\"dependents\":[{\"name\":\"Future Wife\",\"age\":5147483649,\"address\":\"H # 531, S # 20\",\"uid\":\"123412\"},{\"name\":\"Future Kid\",\"age\":5147483648,\"address\":\"H # 531, S # 20\",\"uid\":\"312341\"}]}]");
            input.Strings  = APIHelper.JsonDeserialize <List <string> >("[\"abc\", \"def\"]");

            // Perform API call
            ServerResponse result = null;

            try
            {
                result = await controller.SendMixedArrayAsync(input);
            }
            catch (APIException) {};

            // Test response code
            Assert.AreEqual(200, httpCallBackHandler.Response.StatusCode,
                            "Status should be 200");

            // Test whether the captured response is as we expected
            Assert.IsNotNull(result, "Result should exist");

            Assert.IsTrue(TestHelper.IsJsonObjectProperSubsetOf(
                              "{\"passed\":true}",
                              TestHelper.ConvertStreamToString(httpCallBackHandler.Response.RawBody),
                              true, true, false),
                          "Response body should have matching keys");
        }
        /// <summary>
        /// Send a variety for form params. Returns file count and body params
        /// </summary>
        /// <param name="SendMixedArrayInput">Object containing request parameters</param>
        /// <return>Returns the ServerResponse response from the API call</return>
        public async Task <ServerResponse> SendMixedArrayAsync(SendMixedArrayInput input)
        {
            //validating required parameters
            if (null == input.File)
            {
                throw new ArgumentNullException("file", "The property \"File\" in the input object cannot be null.");
            }

            if (null == input.Integers)
            {
                throw new ArgumentNullException("integers", "The property \"Integers\" in the input object cannot be null.");
            }

            if (null == input.Models)
            {
                throw new ArgumentNullException("models", "The property \"Models\" in the input object cannot be null.");
            }

            if (null == input.Strings)
            {
                throw new ArgumentNullException("strings", "The property \"Strings\" in the input object cannot be null.");
            }

            //the base uri for api requestss
            string _baseUri = Configuration.BaseUri;

            //prepare query string for API call
            StringBuilder _queryBuilder = new StringBuilder(_baseUri);

            _queryBuilder.Append("/form/mixed");


            //validate and preprocess url
            string _queryUrl = APIHelper.CleanUrl(_queryBuilder);

            //append request with appropriate headers and parameters
            var _headers = new Dictionary <string, string>()
            {
                { "user-agent", "Stamplay SDK" },
                { "accept", "application/json" }
            };

            //append form/field parameters
            var _fields = new Dictionary <string, object>()
            {
                { "file", input.File }
            };

            _fields.Add(APIHelper.PrepareFormFieldsFromObject("integers", input.Integers));
            _fields.Add(APIHelper.PrepareFormFieldsFromObject("models", input.Models));
            _fields.Add(APIHelper.PrepareFormFieldsFromObject("strings", input.Strings));

            //prepare the API call request to fetch the response
            HttpRequest _request = ClientInstance.Post(_queryUrl, _headers, _fields);

            //invoke request and get response
            HttpStringResponse _response = (HttpStringResponse)await ClientInstance.ExecuteAsStringAsync(_request);

            HttpContext _context = new HttpContext(_request, _response);

            //handle errors defined at the API level
            base.ValidateResponse(_response, _context);

            try
            {
                return(APIHelper.JsonDeserialize <ServerResponse>(_response.Body));
            }
            catch (Exception _ex)
            {
                throw new APIException("Failed to parse the response: " + _ex.Message, _context);
            }
        }