-
Notifications
You must be signed in to change notification settings - Fork 7
/
Extensions.cs
57 lines (50 loc) · 1.69 KB
/
Extensions.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
namespace HastyAPI {
public static class Extensions {
public static APIResponse ToAPIResponse(this WebResponse wr) {
var hwr = wr as HttpWebResponse;
if(hwr == null) throw new Exception("Not an HttpWebResponse");
var sr = new StreamReader(hwr.GetResponseStream());
var responseText = sr.ReadToEnd();
sr.Close();
return new APIResponse(hwr.Headers, (int)hwr.StatusCode,
hwr.ContentType, responseText, hwr.Cookies);
}
public static HttpWebRequest WithCredentials(this HttpWebRequest request, NetworkCredential credentials) {
if(credentials != null) {
request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(credentials.UserName + ":" + credentials.Password));
}
return request;
}
public static HttpWebRequest WithUserAgent(this HttpWebRequest request, string agent) {
request.UserAgent = agent;
return request;
}
public static HttpWebRequest WithHeaders(this HttpWebRequest request, object headers) {
if(headers != null) {
foreach(var pair in headers.AsDictionary()) {
request.Headers.Add(pair.Key, pair.Value);
}
}
return request;
}
public static IDictionary<string, string> AsDictionary(this object obj) {
IDictionary<string, string> vardic;
if(obj is IDictionary<string, string>) {
vardic = (IDictionary<string, string>)obj;
} else {
vardic = new Dictionary<string, string>();
foreach(var prop in obj.GetType().GetProperties()) {
var value = prop.GetValue(obj, null);
vardic.Add(prop.Name, value?.ToString());
}
}
return vardic;
}
}
}