Created
June 11, 2018 13:31
-
-
Save jstefanelli/178f7bce376cd13c0612e87b251b6012 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Runtime.Serialization; | |
| using System.Net; | |
| using System.Threading.Tasks; | |
| using System.IO; | |
| using System.Runtime.Serialization.Json; | |
| namespace libdiscordovr.Discord | |
| { | |
| public class RequestRunner | |
| { | |
| public static readonly string DiscordAPIEndpoint = "https://discordapp.com/api/"; | |
| public async static Task<T> RequestResponse<T>(string remoteResource, Dictionary<string, string> data, Dictionary<string, string> headers = null, string method = "POST") | |
| { | |
| HttpWebRequest r = WebRequest.CreateHttp(DiscordAPIEndpoint + remoteResource); | |
| StringBuilder requestBuilder = new StringBuilder(); | |
| bool first = true; | |
| if (data != null) | |
| { | |
| foreach (KeyValuePair<string, string> p in data) | |
| { | |
| if (!first) | |
| requestBuilder.Append("&"); | |
| requestBuilder.Append(p.Key); | |
| requestBuilder.Append("="); | |
| requestBuilder.Append(p.Value); | |
| first = false; | |
| } | |
| } | |
| if (headers != null) | |
| { | |
| foreach (KeyValuePair<string, string> h in headers) | |
| { | |
| r.Headers.Add(h.Key, h.Value); | |
| } | |
| } | |
| byte[] requestData = Encoding.UTF8.GetBytes(requestBuilder.ToString()); | |
| r.ContentType = "application/x-www-form-urlencoded"; | |
| r.ContentLength = requestData.Length; | |
| r.Method = method; | |
| r.UserAgent = "DiscordOVR { github.com/jstefanelli dev.0.1 } "; | |
| if (!method.ToLower().Equals("get")) | |
| { | |
| Stream requestStream = await r.GetRequestStreamAsync(); | |
| await requestStream.WriteAsync(requestData, 0, requestData.Length); | |
| } | |
| HttpWebResponse response = (HttpWebResponse)await r.GetResponseAsync(); | |
| if (response.StatusCode != HttpStatusCode.OK && response.StatusCode != HttpStatusCode.NoContent) | |
| { | |
| throw new Exception("Discord Request failed"); | |
| } | |
| T t = default(T); | |
| if (response.StatusCode != HttpStatusCode.NoContent) | |
| { | |
| DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); | |
| t = (T)ser.ReadObject(response.GetResponseStream()); | |
| } | |
| response.Close(); | |
| return t; | |
| } | |
| public static T RequestResponseSync<T>(string remoteResource, Dictionary<string, string> data, Dictionary<string, string> headers = null, string method = "POST") | |
| { | |
| HttpWebRequest r = WebRequest.CreateHttp(DiscordAPIEndpoint + remoteResource); | |
| StringBuilder requestBuilder = new StringBuilder(); | |
| bool first = true; | |
| if (data != null) | |
| { | |
| foreach (KeyValuePair<string, string> p in data) | |
| { | |
| if (!first) | |
| requestBuilder.Append("&"); | |
| requestBuilder.Append(p.Key); | |
| requestBuilder.Append("="); | |
| requestBuilder.Append(p.Value); | |
| first = false; | |
| } | |
| } | |
| if (headers != null) | |
| { | |
| foreach (KeyValuePair<string, string> h in headers) | |
| { | |
| r.Headers.Add(h.Key, h.Value); | |
| } | |
| } | |
| byte[] requestData = Encoding.UTF8.GetBytes(requestBuilder.ToString()); | |
| r.ContentType = "application/x-www-form-urlencoded"; | |
| r.ContentLength = requestData.Length; | |
| r.Method = method; | |
| Stream requestStream = r.GetRequestStream(); | |
| requestStream.Write(requestData, 0, requestData.Length); | |
| HttpWebResponse response = (HttpWebResponse)r.GetResponse(); | |
| if (response.StatusCode != HttpStatusCode.OK && response.StatusCode != HttpStatusCode.NoContent) | |
| { | |
| throw new Exception("Discord Request failed"); | |
| } | |
| T t = default(T); | |
| if (response.StatusCode != HttpStatusCode.NoContent) | |
| { | |
| DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); | |
| t = (T)ser.ReadObject(response.GetResponseStream()); | |
| } | |
| response.Close(); | |
| return t; | |
| } | |
| } | |
| [DataContract] | |
| public abstract class JSONSerializable | |
| { | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment