-
Notifications
You must be signed in to change notification settings - Fork 7
/
APIResponse.cs
38 lines (33 loc) · 1.02 KB
/
APIResponse.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Xml.Linq;
namespace HastyAPI {
public class APIResponse {
public WebHeaderCollection Headers { get; private set; }
public int StatusCode { get; private set; }
public string ContentType { get; private set; }
public string Text { get; private set; }
public CookieCollection Cookies { get; private set; }
public APIResponse(WebHeaderCollection headers, int statusCode, string contentType, string text, CookieCollection cookies) {
Headers = headers;
StatusCode = statusCode;
Text = text;
Cookies = cookies;
}
public APIResponse EnsureStatus(int status) {
if(this.StatusCode != status) {
throw new Exception("Status code was " + StatusCode + ", expected " + status + "." + (Text != null ? " Server responded:\n" + Text : null));
}
return this;
}
public dynamic AsJSON() {
return DynaJSON.Parse(Text);
}
public dynamic AsXML() {
return DynaXML.Parse(Text);
}
}
}