2 using System.Collections.Generic;
7 using Hammock.Authentication;
10 using Hammock.Retries;
11 using Hammock.Serialization;
14 using Hammock.Streaming;
17 using Hammock.Silverlight.Compat;
19 using System.Collections.Specialized;
27 public enum QueryHandling
30 /// Query strings present in paths are left alone.
34 /// Query string pairs present in paths are added to the parameter collection,
35 /// where they are appended back in the case of GET, HEAD, DELETE, and OPTIONS, or added to the
36 /// request body in the case of POST, or PUT.
44 public abstract class RestBase : PropertyChangedBase
46 private byte[] _postContent;
47 private TaskOptions _taskOptions;
48 private RetryPolicy _retryPolicy;
50 public WebParameterCollection GetAllHeaders()
52 var headers = new WebParameterCollection();
54 var parameters = Headers.AllKeys.Select(key => new WebPair(key, Headers[key]));
55 foreach (var parameter in parameters)
57 headers.Add(parameter.Name, parameter.Value);
63 protected virtual internal NameValueCollection Headers { get; set; }
64 protected virtual internal WebParameterCollection Parameters { get; set; }
65 protected virtual internal WebParameterCollection Cookies { get; set; }
66 protected virtual internal ICollection<HttpPostParameter> PostParameters { get; set; }
67 protected internal virtual bool TraceEnabled { get; set; }
69 protected virtual internal byte[] PostContent
78 if (value != null && (Method != WebMethod.Post && Method != WebMethod.Put))
80 Method = WebMethod.Post;
85 public virtual Func<RestRequest, RestResponseBase, Type> GetErrorResponseEntityType { get; set; }
86 public virtual string UserAgent { get; set; }
87 public virtual WebMethod? Method { get; set; }
88 public virtual IWebCredentials Credentials { get; set; }
89 public virtual Encoding Encoding { get; set; }
96 private void Initialize()
98 Headers = new NameValueCollection(0);
99 Parameters = new WebParameterCollection();
100 Cookies = new WebParameterCollection(0);
101 PostParameters = new List<HttpPostParameter>(0);
105 public virtual ServicePoint ServicePoint { get; set; }
106 public virtual bool? FollowRedirects { get; set; }
109 public virtual QueryHandling? QueryHandling { get; set; }
110 public virtual string Proxy { get; set; }
111 public virtual TimeSpan? Timeout { get; set; }
112 public virtual string VersionPath { get; set; }
113 public virtual ISerializer Serializer { get; set; }
114 public virtual IDeserializer Deserializer { get; set; }
115 public virtual ICache Cache { get; set; }
116 public virtual CacheOptions CacheOptions { get; set; }
117 public virtual RetryPolicy RetryPolicy
119 get { return _retryPolicy; }
122 if (_retryPolicy == value)
126 _retryPolicy = value;
127 RetryState = new TaskState();
131 public virtual TaskOptions TaskOptions
133 get { return _taskOptions; }
136 if (_taskOptions == value)
140 _taskOptions = value;
141 TaskState = new TaskState();
145 public virtual bool IsFirstIteration
149 if (RetryState != null)
151 return RetryState.RepeatCount == 0;
153 if (TaskState != null)
155 return TaskState.RepeatCount == 0;
161 public virtual ITaskState TaskState { get; set; }
162 public virtual ITaskState RetryState { get; set; }
163 public virtual StreamOptions StreamOptions { get; set; }
164 public virtual Func<string> CacheKeyFunction { get; set; }
165 public virtual DecompressionMethods? DecompressionMethods { get; set; }
166 public virtual IWebQueryInfo Info { get; set; }
167 public virtual string Path { get; set; }
168 public virtual object Tag { get; set; }
170 public virtual void AddHeader(string name, string value)
172 Headers.Add(name, value);
175 public virtual void AddParameter(string name, string value)
177 Parameters.Add(name, value);
180 public virtual void AddCookie(string name, string value)
182 Cookies.Add(new HttpCookieParameter(name, value));
185 public virtual void AddCookie(Uri domain, string name, string value)
187 Cookies.Add(new HttpCookieParameter(name, value) { Domain = domain });
190 public virtual void AddField(string name, string value)
192 var field = new HttpPostParameter(name, value);
193 PostParameters.Add(field);
196 public virtual void AddFile(string name, string fileName, string filePath)
198 AddFile(name, fileName, filePath, "application/octet-stream", "form-data");
201 public virtual void AddFile(string name, string fileName, string filePath, string contentType)
203 AddFile(name, fileName, filePath, contentType, "form-data");
206 public virtual void AddFile(string name, string fileName, Stream stream)
208 AddFile(name, fileName, stream, "application/octet-stream", "form-data");
211 public virtual void AddFile(string name, string fileName, Stream stream, string contentType)
213 AddFile(name, fileName, stream, contentType, "form-data");
216 public virtual void AddFile(string name, string fileName, string filePath, string contentType, string contentDisposition)
218 var parameter = HttpPostParameter.CreateFile(name, fileName, filePath, contentType, contentDisposition);
219 PostParameters.Add(parameter);
222 public virtual void AddFile(string name, string fileName, Stream stream, string contentType, string contentDisposition)
224 var parameter = HttpPostParameter.CreateFile(name, fileName, stream, contentType, contentDisposition);
225 PostParameters.Add(parameter);
228 public virtual void AddPostContent(byte[] content)
230 if (PostContent == null)
232 PostContent = content;
236 var original = PostContent.Length;
237 var current = content.Length;
239 var final = new byte[current + original];
240 Array.Copy(PostContent, 0, final, 0, original);
241 Array.Copy(content, 0, final, original, current);
248 public class RetryEventArgs : EventArgs
250 public virtual RestClient Client { get; set; }
251 public virtual RestRequest Request { get; set; }
254 public class FileProgressEventArgs : EventArgs
256 public virtual string FileName { get; set; }
257 public virtual long BytesWritten { get; set; }
258 public virtual long TotalBytes { get; set; }