public void CustomDateTimeSerialization()
        {
            // serialization
            var thing = new CustomThing
            {
                From  = new DateTime(2020, 04, 24, 1, 2, 3),
                To    = null,
                Now   = new DateTime?(new DateTime(2019, 12, 24, 1, 2, 3)),
                Then  = new DateTime?(),
                Start = new DateTime(2020, 05, 19, 2, 48, 55),
            };

            var ss   = new ServiceStackSerializer();
            var json = ss.Serialize(thing);

            Assert.NotNull(json);
            WriteLine(JsonFormatter.FormatJson(json));

            var obj = ss.Deserialize <CustomThing>(new RestResponse()
            {
                Content = json
            });

            Assert.NotNull(obj);
        }
예제 #2
0
        public ServiceClient(string url, string version)
        {
            restUrl    = url;
            apiVersion = version;

            _serializer = new ServiceStackSerializer();

            restClient           = new RestClient(url);
            restClient.UserAgent = "PaketGlobal";
            restClient.Timeout   = 60 * 1000;
            restClient.AddHandler("application/json", _serializer);
        }
예제 #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CommonApiClient"/> class.
        /// </summary>
        /// <param name="client">REST API client, see <see cref="RestSharp"/>.</param>
        /// <param name="credentials">Credentials.</param>
        public CommonApiClient(IRestClient client, Credentials credentials)
        {
            Credentials = credentials;
            Serializer  = new ServiceStackSerializer();
            BaseUrl     = client.BaseUrl.ToString();
            //Limiter = new RequestRateLimiter();

            // set up REST client
            Client = client;
            Client.Authenticator = new CredentialsAuthenticator(this, credentials);
            Client.Encoding      = Encoding.UTF8;
            Client.ThrowOnDeserializationError = false;
            Client.UseSerializer(() => Serializer);
        }
예제 #4
0
        static TestsFixture()
        {
            ServiceStackSerializer.Init();

            HttpClientFactory.Get = (() =>
            {
                var httpClientHandler = new HttpClientHandler();
                if (httpClientHandler.SupportsAutomaticDecompression)
                {
                    httpClientHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                }
                var httpClient = new HttpClient(httpClientHandler)
                {
                    Timeout = TimeSpan.FromMinutes(3)
                };
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/jsonp"));
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/html"));

                return(httpClient);
            });
        }
예제 #5
0
        private async Task <T> SendRequest <T>(RestRequest request, string pubkey = null, bool signData = true, RestClient customClient = null, SignHandler customSign = null, RawBytes rb = null, System.IO.Stream responseStream = null, bool preferSSL = false, bool suppressUnAuthorized = false, bool suppressNoConnection = false, bool suppressServerErrors = false, CancellationTokenSource cancellationTokenSource = null)
        {
            var resource = request.Resource;

            var client = customClient ?? restClient;

            if (signData)
            {
                SignRequest(request, pubkey, client, customSign: customSign);
            }

            try
            {
                IRestResponse <T> response;

                if (responseStream != null)
                {
                    request.ResponseWriter = (obj) =>
                    {
                        obj.CopyTo(responseStream);
                        obj.Close();
                    };

                    if (cancellationTokenSource != null)
                    {
                        response = await client.ExecuteTaskAsync <T>(request, cancellationTokenSource.Token);
                    }
                    else
                    {
                        response = await client.ExecuteTaskAsync <T>(request);
                    }

                    responseStream.Dispose();
                }
                else
                {
                    if (cancellationTokenSource != null)
                    {
                        response = await client.ExecuteTaskAsync <T>(request, cancellationTokenSource.Token);
                    }
                    else
                    {
                        response = await client.ExecuteTaskAsync <T>(request);
                    }

                    if (rb != null)
                    {
                        rb.Data = response.RawBytes;
                    }
                }

                if (response.ResponseUri != null)
                {
                    if (!response.ResponseUri.ToString().Contains("package_photo"))
                    {
                        System.Diagnostics.Debug.WriteLine("Status: {0}, Content: {1}", response.StatusCode, response.Content);
                    }
                }


                ServiceStackSerializer.HandleStatusCode(response, resource);
                return(response.Data);
            }
            catch (WebException e)
            {
                System.Diagnostics.Debug.WriteLine("SendRequest to: {0} Error: {1}", client.BaseUrl, e.ToString());
                if (!suppressNoConnection)
                {
                    try
                    {
                        App.Locator.Workspace.OnConnectionError(new ConnectionErrorEventArgs(resource));
                    }
                    catch
                    {
                        App.Locator.Workspace.OnConnectionError(new ConnectionErrorEventArgs(""));
                    }
                }
            }
            catch (ServiceException e)
            {
                System.Diagnostics.Debug.WriteLine(e);
                if (!suppressServerErrors)
                {
                    App.Locator.Workspace.OnServiceError(new ServiceErrorEventArgs(e));
                }
            } catch (UnAuthorizedException e) {
                //Profile.DeleteCredentials ();
                if (!suppressUnAuthorized)
                {
                    App.Locator.Workspace.OnAuthenticationRequired(e);
                }
            } catch (Exception e) {
                System.Diagnostics.Debug.WriteLine(e);
                //if (!suppressNoConnection)
                //_workspace.OnConnectionError(EventArgs.Empty);
            }

            return(default(T));
        }
예제 #6
0
 public ArcGISGatewayTests()
 {
     _serviceStackSerializer = new ServiceStackSerializer();
     _jsonDotNetSerializer   = new JsonDotNetSerializer();
 }
예제 #7
0
 public SecureGISGatewayTests()
 {
     _serviceStackSerializer        = new ServiceStackSerializer();
     CryptoProviderFactory.Disabled = true;
 }
        private T Deserialize <T>(string json)
        {
            var ss = new ServiceStackSerializer();

            return(ss.Deserialize <T>(json));
        }
        private string Serialize <T>(T dto)
        {
            var ss = new ServiceStackSerializer();

            return(ss.Serialize(dto));
        }