Пример #1
0
        public void YesNoSerializationTest()
        {
            JsonYandexApiSerializer serializer = new JsonYandexApiSerializer();

            Assert.AreEqual("\"Yes\"", serializer.Serialize(YesNo.Yes));

            Assert.AreEqual(YesNo.Yes, serializer.Deserialize<YesNo>("\"Yes\""));
            Assert.AreEqual(YesNo.Yes, serializer.Deserialize<YesNo?>("\"Yes\""));
            Assert.AreEqual(null, serializer.Deserialize<YesNo?>(string.Empty));
        }
Пример #2
0
        public void YesNoSerializationTest()
        {
            JsonYandexApiSerializer serializer = new JsonYandexApiSerializer();

            Assert.AreEqual("\"Yes\"", serializer.Serialize(YesNo.Yes));

            Assert.AreEqual(YesNo.Yes, serializer.Deserialize <YesNo>("\"Yes\""));
            Assert.AreEqual(YesNo.Yes, serializer.Deserialize <YesNo?>("\"Yes\""));
            Assert.AreEqual(null, serializer.Deserialize <YesNo?>(string.Empty));
        }
Пример #3
0
        public T Invoke <T>(string method, object param = null, bool financeTokenRequired = false)
        {
            if (string.IsNullOrEmpty(method))
            {
                throw new ArgumentNullException("method");
            }

            // Creating http-request

            var authProvider = Configuration.AuthProvider;

            var requestMessage = CreateRequestMessage(method, param, authProvider, financeTokenRequired);
            var request        = CreateRequest(requestMessage, authProvider);

            // Invoking the request to server

            string responseMessage;

            try
            {
                using (WebResponse response = request.GetResponse())
                {
                    responseMessage = GetResponseMessage(response);
                }
            }
            catch (Exception ex)
            {
                throw new YandexConnectionException("Unable to perform http-request to Yandex API endpoint.", ex);
            }

            // Retrieving response

            var responseObject = _serializer.Deserialize <JsonResponseObject <T> >(responseMessage);

            if (responseObject == null)
            {
                throw new YandexConnectionException("Not supported server response.");
            }

            if (responseObject.ErrorCode != YandexApiErrorCode.None)
            {
                throw new YandexDirectException(responseObject.ErrorCode, responseObject.ErrorMessage);
            }

            return(responseObject.Object);
        }