-
Notifications
You must be signed in to change notification settings - Fork 129
/
Utilities.cs
137 lines (119 loc) · 4.58 KB
/
Utilities.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//-----------------------------------------------------------------------
// <copyright file="Utilities.cs">
// Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
// <license>
// Microsoft Public License (Ms-PL http://opensource.org/licenses/ms-pl.html).
// Contributors may add their own copyright notice above.
// </license>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using LinqToTwitter.Net;
namespace LinqToTwitter
{
public static class Utilities
{
///// <summary>
///// Encapsulates GetResponse so tests don't invoke the request
///// </summary>
///// <param name="req">Request to Twitter</param>
///// <returns>Response to Twitter</returns>
//public static HttpWebResponse AsyncGetResponse(HttpWebRequest req)
//{
// Exception asyncException = null;
// var resetEvent = new ManualResetEvent(/*initialStateSignaled:*/ false);
// HttpWebResponse res = null;
// req.BeginGetResponse(
// new AsyncCallback(
// ar =>
// {
// try
// {
// res = req.EndGetResponse(ar) as HttpWebResponse;
// }
// catch (Exception ex)
// {
// asyncException = ex;
// }
// finally
// {
// resetEvent.Set();
// }
// }), null);
// resetEvent.WaitOne();
// if (asyncException != null)
// {
// throw asyncException;
// }
// return res;
//}
///// <summary>
///// Reads the web response stream into a string.
///// </summary>
///// <param name="resp">The response to read</param>
///// <returns>a string containing the entire web response body</returns>
//public static string ReadReponse(this WebResponse resp)
//{
// if (resp == null)
// return null;
// using (var respStream = resp.GetResponseStream())
// using (var respReader = new StreamReader(respStream))
// {
// var responseBody = respReader.ReadToEnd();
// return responseBody;
// }
//}
/// <summary>
/// Assembles a series of key=value pairs as a URI-escaped query-string.
/// </summary>
/// <param name="parameters">The parameters to include.</param>
/// <returns>A query-string-like value such as a=b&c=d. Does not include a leading question mark (?).</returns>
public static string BuildQueryString(IEnumerable<QueryParameter> parameters)
{
if (parameters == null)
throw new ArgumentNullException("parameters");
StringBuilder builder = new StringBuilder();
foreach (var pair in parameters.Where(p => !string.IsNullOrWhiteSpace(p.Value)))
{
builder.Append(Url.PercentEncode(pair.Name));
builder.Append('=');
builder.Append(Url.PercentEncode(pair.Value));
builder.Append('&');
}
if (builder.Length > 1)
builder.Length--; // truncate trailing &
return builder.ToString();
}
//#if !NETFX_CORE
// /// <summary>
// /// Reads a file into a byte array
// /// </summary>
// /// <param name="filePath">Full path of file to read.</param>
// /// <returns>Byte array with file contents.</returns>
// public static byte[] GetFileBytes(string filePath)
// {
// byte[] fileBytes = null;
// using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
// using (var memStr = new MemoryStream())
// {
// byte[] buffer = new byte[4096];
// memStr.Position = 0;
// int bytesRead = 0;
// while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
// {
// memStr.Write(buffer, 0, bytesRead);
// }
// memStr.Position = 0;
// fileBytes = memStr.GetBuffer();
// }
// return fileBytes;
// }
//#endif
}
}