Last active
August 29, 2015 14:01
-
-
Save glennblock/8611a5d0f316ed221262 to your computer and use it in GitHub Desktop.
Helper script for talking to the issue tracker using the scriptcs REPL
This file contains 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
/* | |
To use, drop this into the current folder. Then open the scriptcs REPL with the helper functions laoded. | |
run "scriptcs http.csx -r" | |
Start the issue tracker API self-host cloned from https://github.com/webapibook/issuetracker, by building and running the self-host project. | |
To pull down the collection and dump to the REPL. | |
var coll = GetCJ("http://localhost:8080/issue"); | |
coll | |
To grab an issue a specific issue: | |
var issue = GetJson("http://localhost:8080/issue/1"); | |
Write(issue); | |
To transition an issue from open to closed | |
Post("http://localhost:8080/issueprocessor/1?action=close"); | |
*/ | |
using System.Net.Http; | |
using System.Net.Http.Formatting; | |
using System.Net.Http.Headers; | |
using Newtonsoft.Json.Linq; | |
using WebApiContrib.Formatting.CollectionJson.Client; | |
using WebApiContrib.CollectionJson; | |
using System.Linq; | |
public HttpResponseMessage Get(string uri) { | |
var client = new HttpClient(); | |
return client.GetAsync(uri).Result; | |
} | |
public HttpResponseMessage Get(Uri uri) { | |
var client = new HttpClient(); | |
return client.GetAsync(uri).Result; | |
} | |
public Collection GetCJ(string uri) { | |
var client = new HttpClient(); | |
var request = new HttpRequestMessage(); | |
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.collection+json")); | |
var resp = client.GetAsync(uri).Result; | |
return ToCollection(resp); | |
}a | |
public JObject GetJson(string uri) { | |
var resp = Get(uri); | |
return resp.Content.ReadAsAsync<JObject>().Result; | |
} | |
public void Write(Object message) { | |
Console.WriteLine(message); | |
} | |
public HttpResponseMessage Post(string uri) { | |
var client = new HttpClient(); | |
var resp = client.PostAsync(uri,null).Result; | |
return resp; | |
} | |
public Collection ToCollection(HttpResponseMessage resp) | |
{ | |
var readDoc = resp.Content.ReadAsAsync<ReadDocument>(new [] {new CollectionJsonFormatter()}).Result; | |
return readDoc.Collection; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment