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

« back to all changes in this revision

Viewing changes to lib/Twitterizer/Twitterizer2/Core/TwitterCommand.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:
42
42
#if !SILVERLIGHT
43
43
    using System.Web;
44
44
#endif
45
 
#if !LITE && !SILVERLIGHT
46
 
    using System.Web.Caching;
47
 
#endif
48
45
    using Twitterizer;
49
46
 
50
47
    /// <summary>
66
63
        /// <param name="optionalProperties">The optional properties.</param>
67
64
        protected TwitterCommand(HTTPVerb method, string endPoint, OAuthTokens tokens, OptionalProperties optionalProperties)
68
65
        {
69
 
                        this.RequestParameters = new Dictionary<string, object>();
 
66
            this.RequestParameters = new Dictionary<string, object>();
70
67
            this.Verb = method;
71
68
            this.Tokens = tokens;
72
69
            this.OptionalProperties = optionalProperties ?? new OptionalProperties();
78
75
        /// Gets or sets the optional properties.
79
76
        /// </summary>
80
77
        /// <value>The optional properties.</value>
81
 
        public OptionalProperties OptionalProperties { get; set; }
 
78
        protected OptionalProperties OptionalProperties { get; set; }
82
79
 
83
80
        /// <summary>
84
81
        /// Gets or sets the API method URI.
85
82
        /// </summary>
86
83
        /// <value>The URI for the API method.</value>
87
 
        public Uri Uri { get; set; }
 
84
        private Uri Uri { get; set; }
88
85
 
89
86
        /// <summary>
90
87
        /// Gets or sets the method.
91
88
        /// </summary>
92
89
        /// <value>The method.</value>
93
 
        public HTTPVerb Verb { get; set; }
 
90
        private HTTPVerb Verb { get; set; }
94
91
 
95
92
        /// <summary>
96
93
        /// Gets or sets the request parameters.
102
99
        /// Gets or sets the serialization delegate.
103
100
        /// </summary>
104
101
        /// <value>The serialization delegate.</value>
105
 
        public SerializationHelper<T>.DeserializationHandler DeserializationHandler { get; set; }
 
102
        protected SerializationHelper<T>.DeserializationHandler DeserializationHandler { get; set; }
106
103
 
107
104
        /// <summary>
108
105
        /// Gets the request tokens.
119
116
        /// Gets or sets a value indicating whether this <see cref="TwitterCommand&lt;T&gt;"/> is multipart.
120
117
        /// </summary>
121
118
        /// <value><c>true</c> if multipart; otherwise, <c>false</c>.</value>
122
 
        public bool Multipart { get; set; }
 
119
        protected bool Multipart { get; set; }
123
120
 
124
121
        /// <summary>
125
122
        /// Executes the command.
137
134
            // Loop through all of the custom attributes assigned to the command class
138
135
            foreach (Attribute attribute in this.GetType().GetCustomAttributes(false))
139
136
            {
140
 
                if (attribute.GetType() == typeof(AuthorizedCommandAttribute))
 
137
                if (attribute is AuthorizedCommandAttribute)
141
138
                {
142
139
                    if (this.Tokens == null)
143
140
                    {
152
149
                        throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "Token values cannot be null when executing the \"{0}\" command.", this.GetType()));
153
150
                    }
154
151
                }
155
 
                else if (attribute.GetType() == typeof(RateLimitedAttribute))
 
152
                else if (attribute is RateLimitedAttribute)
156
153
                {
157
154
                    // Get the rate limiting status
158
155
                    if (TwitterRateLimitStatus.GetStatus(this.Tokens).ResponseObject.RemainingHits == 0)
163
160
 
164
161
            }
165
162
 
166
 
#if !LITE && !SILVERLIGHT
167
 
            // Variables and objects needed for caching
168
 
            StringBuilder cacheKeyBuilder = new StringBuilder(this.Uri.AbsoluteUri);
169
 
            if (this.Tokens != null)
170
 
            {
171
 
                cacheKeyBuilder.AppendFormat("|{0}|{1}", this.Tokens.ConsumerKey, this.Tokens.ConsumerKey);
172
 
            }
173
 
 
174
 
            Cache cache = HttpRuntime.Cache;
175
 
#endif
176
 
 
177
163
            // Prepare the query parameters
178
 
                        Dictionary<string, object> queryParameters = new Dictionary<string, object>();
179
 
                        foreach (KeyValuePair<string, object> item in this.RequestParameters)
 
164
            Dictionary<string, object> queryParameters = new Dictionary<string, object>();
 
165
            foreach (KeyValuePair<string, object> item in this.RequestParameters)
180
166
            {
181
167
                queryParameters.Add(item.Key, item.Value);
182
 
#if !LITE && !SILVERLIGHT
183
 
                cacheKeyBuilder.AppendFormat("|{0}={1}", item.Key, item.Value);
184
 
#endif
185
168
            }
186
169
 
187
 
#if !LITE && !SILVERLIGHT
188
 
            // Lookup the cached item and return it
189
 
            if (this.Verb == HTTPVerb.GET && this.OptionalProperties.CacheOutput && cache[cacheKeyBuilder.ToString()] != null)
190
 
            {
191
 
                if (cache[cacheKeyBuilder.ToString()] is T)
192
 
                {
193
 
                    return new TwitterResponse<T>()
194
 
                               {
195
 
                                   ResponseObject = (T)cache[cacheKeyBuilder.ToString()],
196
 
                                   ResponseCached = true
197
 
                               };
198
 
                }
199
 
            }
200
 
            
201
 
#endif
202
170
            // Declare the variable to be returned
203
171
            twitterResponse.ResponseObject = default(T);
204
172
            twitterResponse.RequestUrl = this.Uri.AbsoluteUri;
208
176
 
209
177
            try
210
178
            {
211
 
                                WebRequestBuilder requestBuilder = new WebRequestBuilder(this.Uri, this.Verb, this.Tokens, "") { Multipart = this.Multipart };
 
179
                                WebRequestBuilder requestBuilder = new WebRequestBuilder(this.Uri, this.Verb, this.Tokens) { Multipart = this.Multipart };
212
180
 
213
181
#if !SILVERLIGHT
214
182
                if (this.OptionalProperties != null)
222
190
 
223
191
                HttpWebResponse response = requestBuilder.ExecuteRequest();
224
192
 
 
193
                if (response == null)
 
194
                {
 
195
                    twitterResponse.Result = RequestResult.Unknown;
 
196
                    return twitterResponse;
 
197
                }
 
198
 
225
199
                responseData = ConversionUtility.ReadStream(response.GetResponseStream());
226
200
                twitterResponse.Content = Encoding.UTF8.GetString(responseData, 0, responseData.Length);
227
201
 
228
202
                twitterResponse.RequestUrl = requestBuilder.RequestUri.AbsoluteUri;
229
203
 
 
204
#if !SILVERLIGHT
230
205
                // Parse the rate limiting HTTP Headers
231
206
                rateLimiting = ParseRateLimitHeaders(response.Headers);
232
207
 
233
208
                // Parse Access Level
234
209
                accessLevel = ParseAccessLevel(response.Headers);
 
210
#else
 
211
                rateLimiting = null;
 
212
                accessLevel = AccessLevel.Unknown;
 
213
 
 
214
#endif
235
215
 
236
216
                // Lookup the status code and set the status accordingly
237
217
                SetStatusCode(twitterResponse, response.StatusCode, rateLimiting);
266
246
                responseData = ConversionUtility.ReadStream(exceptionResponse.GetResponseStream());
267
247
                twitterResponse.Content = Encoding.UTF8.GetString(responseData, 0, responseData.Length);
268
248
 
 
249
#if !SILVERLIGHT
269
250
                rateLimiting = ParseRateLimitHeaders(exceptionResponse.Headers);
270
251
 
271
252
                // Parse Access Level
272
253
                accessLevel = ParseAccessLevel(exceptionResponse.Headers);
 
254
#else
 
255
                rateLimiting = null;
 
256
                accessLevel = AccessLevel.Unknown;
 
257
#endif
273
258
 
274
259
                // Try to read the error message, if there is one.
275
260
                try
276
261
                {
277
 
                    twitterResponse.ErrorMessage = SerializationHelper<TwitterErrorDetails>.Deserialize(responseData).ErrorMessage;
 
262
                    TwitterErrorDetails errorDetails = SerializationHelper<TwitterErrorDetails>.Deserialize(responseData);
 
263
                    twitterResponse.ErrorMessage = errorDetails.ErrorMessage;
278
264
                }
279
265
                catch (Exception)
280
266
                {
304
290
                twitterResponse.Result = RequestResult.Unknown;
305
291
                return twitterResponse;
306
292
            }
307
 
 
308
 
#if !LITE && !SILVERLIGHT
309
 
            this.AddResultToCache(cacheKeyBuilder, cache, twitterResponse.ResponseObject);
310
 
#endif
 
293
            catch (Newtonsoft.Json.JsonSerializationException)
 
294
            {
 
295
                twitterResponse.ErrorMessage = "Unable to parse JSON";
 
296
                twitterResponse.Result = RequestResult.Unknown;
 
297
                return twitterResponse;
 
298
            }
311
299
 
312
300
            // Pass the current oauth tokens into the new object, so method calls from there will keep the authentication.
313
301
            twitterResponse.Tokens = this.Tokens;
330
318
                    break;
331
319
 
332
320
                case HttpStatusCode.BadRequest:
333
 
                    twitterResponse.Result = rateLimiting.Remaining == 0 ? RequestResult.RateLimited : RequestResult.BadRequest;
 
321
                    twitterResponse.Result = (rateLimiting != null && rateLimiting.Remaining == 0) ? RequestResult.RateLimited : RequestResult.BadRequest;
 
322
                    break;
 
323
 
 
324
                case (HttpStatusCode)420: //Rate Limited from Search/Trends API
 
325
                    twitterResponse.Result = RequestResult.RateLimited;
334
326
                    break;
335
327
 
336
328
                case HttpStatusCode.Unauthorized:
345
337
                    twitterResponse.Result = RequestResult.ProxyAuthenticationRequired;
346
338
                    break;
347
339
 
 
340
                case HttpStatusCode.RequestTimeout:
 
341
                    twitterResponse.Result = RequestResult.TwitterIsOverloaded;
 
342
                    break;
 
343
 
 
344
                case HttpStatusCode.Forbidden:
 
345
                    twitterResponse.Result = RequestResult.Unauthorized;
 
346
                    break;
 
347
 
348
348
                default:
349
349
                    twitterResponse.Result = RequestResult.Unknown;
350
350
                    break;
387
387
                rateLimiting.ResetDate = DateTime.SpecifyKind(new DateTime(1970, 1, 1, 0, 0, 0, 0)
388
388
                    .AddSeconds(double.Parse(responseHeaders["X-RateLimit-Reset"], CultureInfo.InvariantCulture)), DateTimeKind.Utc);
389
389
            }
 
390
            else if(!string.IsNullOrEmpty(responseHeaders["Retry-After"]))
 
391
            {
 
392
                rateLimiting.ResetDate = DateTime.UtcNow.AddSeconds(Convert.ToInt32(responseHeaders["Retry-After"]));
 
393
            }
 
394
            
390
395
            return rateLimiting;
391
396
        }
392
397
 
408
413
                    case "read-write-privatemessages":
409
414
                    case "read-write-directmessages":
410
415
                        return AccessLevel.ReadWriteDirectMessage;
411
 
                    default:
412
 
                        break;
413
416
                }
414
417
                return AccessLevel.Unknown;
415
418
            }
416
 
            else
417
 
                return AccessLevel.Unavailable;
418
 
        }
419
 
 
420
 
 
421
 
#if !LITE && !SILVERLIGHT
422
 
        /// <summary>
423
 
        /// Adds the result to cache.
424
 
        /// </summary>
425
 
        /// <param name="cacheKeyBuilder">The cache key builder.</param>
426
 
        /// <param name="cache">The cache.</param>
427
 
        /// <param name="resultObject">The result object.</param>
428
 
        private void AddResultToCache(StringBuilder cacheKeyBuilder, Cache cache, T resultObject)
429
 
        {
430
 
            // If caching is enabled, add the result to the cache.
431
 
            if (this.Verb == HTTPVerb.GET && this.OptionalProperties.CacheOutput)
432
 
            {
433
 
                cache.Add(
434
 
                    cacheKeyBuilder.ToString(),
435
 
                    resultObject,
436
 
                    null,
437
 
                    DateTime.Now.Add(this.OptionalProperties.CacheTimespan),
438
 
                    Cache.NoSlidingExpiration,
439
 
                    CacheItemPriority.Normal,
440
 
                    null);
441
 
            }
442
 
        }
443
 
#endif
444
 
        }
 
419
            return AccessLevel.Unavailable;
 
420
        }
 
421
    }
445
422
}