public static void APIRequest <K>(Action <K> callback, Tuple <string, string>[] getParameters, Tuple <string, string>[] postParameters) where K : Response { RequestPath path = RequestPath.GetRequestPath <K>(); //TODO: Custom paths? Appropriate subdomain paths? Not sure. //Maybe store custom path in the requestpath.path itself? string parameters = getParameters .Select(new Func <Tuple <string, string>, string>(a => string.Format("{0}={1}", Uri.UnescapeDataString(a.Item1), Uri.UnescapeDataString(a.Item2)))) .Aggregate((a, b) => { if (string.IsNullOrEmpty(a)) { return(b); } else { return(string.Format("{0}&{1}", a, b)); } }); Uri requestUri = new Uri(string.Format("{0}?{1}", Path.Combine(APIBaseLocation, path.Path), parameters)); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUri); //This will handle all of the processing. RequestState <K> state = new RequestState <K>(request, postParameters, callback); state.Begin(); }
public Task <K> APIRequestAsync <K>(Tuple <string, string>[] getParameters, Tuple <string, string>[] postParameters) where K : Response { RequestPath path = RequestPath.GetRequestPath <K>(); //TODO: Custom paths? Appropriate subdomain paths? Not sure. //Maybe store custom path in the requestpath.path itself? Uri requestUri = GetSlackUri(Path.Combine(APIBaseLocation, path.Path), getParameters); HttpWebRequest request = CreateWebRequest(requestUri); //This will handle all of the processing. var state = new RequestStateForTask <K>(request, postParameters); return(state.Execute()); }
protected void APIRequest <K>(Action <K> callback, Tuple <string, string>[] getParameters, Tuple <string, string>[] postParameters) where K : Response { RequestPath path = RequestPath.GetRequestPath <K>(); //TODO: Custom paths? Appropriate subdomain paths? Not sure. //Maybe store custom path in the requestpath.path itself? Uri requestUri = GetSlackUri(Path.Combine(APIBaseLocation, path.Path), getParameters); HttpWebRequest request = CreateWebRequest(requestUri); //This will handle all of the processing. RequestState <K> state = new RequestState <K>(request, postParameters, callback); state.Begin(); }
public static RequestPath GetRequestPath <K>() { Type t = typeof(K); if (paths.ContainsKey(t)) { return(paths[t]); } TypeInfo info = t.GetTypeInfo(); RequestPath path = info.GetCustomAttribute <RequestPath>(); if (path == null) { throw new InvalidOperationException(string.Format("No valid request path for {0}", t.Name)); } paths.Add(t, path); return(path); }