~ubuntu-branches/ubuntu/trusty/smuxi/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/ServiceStack/src/ServiceStack.Common/ServiceClient.Web/JsvRestClientAsync.cs

  • Committer: Package Import Robot
  • Author(s): Mirco Bauer
  • Date: 2013-05-25 22:11:31 UTC
  • mfrom: (1.2.12)
  • Revision ID: package-import@ubuntu.com-20130525221131-nd2mc0kzubuwyx20
Tags: 0.8.11-1
* [22d13d5] Imported Upstream version 0.8.11
* [6d2b95a] Refreshed patches
* [89eb66e] Added ServiceStack libraries to smuxi-engine package
* [848ab10] Enable Campfire engine
* [c6dbdc7] Always build db4o for predictable build result
* [13ec489] Exclude OS X specific libraries from dh_clideps

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System;
 
2
using System.IO;
 
3
using ServiceStack.Service;
 
4
using ServiceStack.ServiceHost;
 
5
using ServiceStack.Text;
 
6
 
 
7
namespace ServiceStack.ServiceClient.Web
 
8
{
 
9
    public class JsvRestClientAsync 
 
10
        : IRestClientAsync
 
11
    {
 
12
        public const string ContentType = "application/jsv";
 
13
 
 
14
        public JsvRestClientAsync(string baseUri)
 
15
            : this()
 
16
        {
 
17
            this.BaseUri = baseUri.WithTrailingSlash();
 
18
        }
 
19
 
 
20
        public JsvRestClientAsync()
 
21
        {
 
22
            this.client = new AsyncServiceClient {
 
23
                ContentType = ContentType,
 
24
                StreamSerializer = SerializeToStream,
 
25
                StreamDeserializer = TypeSerializer.DeserializeFromStream
 
26
            };
 
27
        }
 
28
 
 
29
        public TimeSpan? Timeout
 
30
        {
 
31
            get { return this.client.Timeout; }
 
32
            set { this.client.Timeout = value; }
 
33
        }
 
34
 
 
35
        private static void SerializeToStream(IRequestContext requestContext, object dto, Stream stream)
 
36
        {
 
37
            TypeSerializer.SerializeToStream(dto, stream);
 
38
        }
 
39
 
 
40
        private readonly AsyncServiceClient client;
 
41
 
 
42
        public string BaseUri { get; set; }
 
43
 
 
44
        public void SetCredentials(string userName, string password)
 
45
        {
 
46
            this.client.SetCredentials(userName, password);
 
47
        }
 
48
 
 
49
        private string GetUrl(string relativeOrAbsoluteUrl)
 
50
        {
 
51
            return relativeOrAbsoluteUrl.StartsWith("http:")
 
52
                || relativeOrAbsoluteUrl.StartsWith("https:")
 
53
                     ? relativeOrAbsoluteUrl
 
54
                     : this.BaseUri + relativeOrAbsoluteUrl;
 
55
        }
 
56
 
 
57
        public void GetAsync<TResponse>(string relativeOrAbsoluteUrl, Action<TResponse> onSuccess, Action<TResponse, Exception> onError)
 
58
        {
 
59
            this.client.SendAsync(HttpMethod.Get, GetUrl(relativeOrAbsoluteUrl), null, onSuccess, onError);
 
60
        }
 
61
 
 
62
        public void DeleteAsync<TResponse>(string relativeOrAbsoluteUrl, Action<TResponse> onSuccess, Action<TResponse, Exception> onError)
 
63
        {
 
64
            this.client.SendAsync(HttpMethod.Delete, GetUrl(relativeOrAbsoluteUrl), null, onSuccess, onError);
 
65
        }
 
66
 
 
67
        public void PostAsync<TResponse>(string relativeOrAbsoluteUrl, object request, Action<TResponse> onSuccess, Action<TResponse, Exception> onError)
 
68
        {
 
69
            this.client.SendAsync(HttpMethod.Post, GetUrl(relativeOrAbsoluteUrl), request, onSuccess, onError);
 
70
        }
 
71
 
 
72
        public void PutAsync<TResponse>(string relativeOrAbsoluteUrl, object request, Action<TResponse> onSuccess, Action<TResponse, Exception> onError)
 
73
        {
 
74
            this.client.SendAsync(HttpMethod.Put, GetUrl(relativeOrAbsoluteUrl), request, onSuccess, onError);
 
75
        }
 
76
 
 
77
        public void Dispose() {}
 
78
    }
 
79
}
 
 
b'\\ No newline at end of file'