Skip to content
This repository has been archived by the owner on Mar 8, 2018. It is now read-only.

Commit

Permalink
Implement generics support for the public-ish Execute method to contr…
Browse files Browse the repository at this point in the history
…ol JSON deserialization

This should allow the Api object to properly pass objects in
  • Loading branch information
R. Tyler Ballance committed Sep 3, 2009
1 parent 5f7363d commit 86df73b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
32 changes: 28 additions & 4 deletions Sources/RequestProxy.cs
Expand Up @@ -30,6 +30,7 @@ public class RequestProxy
public RequestProxy()
{
this.json = new JavaScriptSerializer();
this.hostName = "localhost";
this.hostPort = this.defaultPort;
}

Expand Down Expand Up @@ -72,8 +73,7 @@ public int Port
}
#endregion

#region "Public Methods"
public Dictionary<string, object> Execute(string endpoint)
internal string FetchJson(string endpoint)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
Expand Down Expand Up @@ -101,8 +101,7 @@ public int Port
}

reader = new StreamReader(response.GetResponseStream());
return this.json.DeserializeObject(reader.ReadToEnd()) as
Dictionary<string, object>;
return reader.ReadToEnd();
}
}
catch (WebException exc)
Expand All @@ -129,6 +128,31 @@ public int Port
}
return null;
}

#region "Public Methods"
public T Execute<T>(string endpoint)
{
string result = this.FetchJson(endpoint);

if (String.IsNullOrEmpty(result))
{
return default(T);
}

return this.json.Deserialize<T>(result);
}

public Dictionary<string, object> Execute(string endpoint)
{
string result = this.FetchJson(endpoint);

if (String.IsNullOrEmpty(result))
{
return null;
}

return this.json.DeserializeObject(result) as Dictionary<string, object>;
}
#endregion
}
}
10 changes: 10 additions & 0 deletions Tests/RequestProxyTest.cs
Expand Up @@ -105,5 +105,15 @@ public void ExecuteSimpleRequest()
Assert.IsTrue(response.ContainsKey("views"), "Response doesn't contain a 'views'");
Assert.IsTrue(response.ContainsKey("jobs"), "Response doesn't contain 'jobs'");
}

[Test]
public void GenericsExecuteJobTest()
{
RequestProxy req = new RequestProxy("localhost");
Assert.IsNotNull(req, "RequestProxy object is null");

Hudson.Data.Job job = req.Execute<Hudson.Data.Job>("/api/json");
Assert.IsNotNull(job);
}
}
}

0 comments on commit 86df73b

Please sign in to comment.