Skip to content

Commit 67dfdae

Browse files
Flexible Response object return value
1 parent 9d21445 commit 67dfdae

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

CSharpHTTPClient/Client.cs

+22
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,35 @@ public class Response
1717
public HttpStatusCode StatusCode;
1818
public HttpContent ResponseBody;
1919
public HttpResponseHeaders ResponseHeaders;
20+
public Dictionary<string, dynamic> DSResponseBody;
21+
public Dictionary<string, string> DSResponseHeaders;
2022

2123
public Response(HttpStatusCode statusCode, HttpContent responseBody, HttpResponseHeaders responseHeaders)
2224
{
2325
StatusCode = statusCode;
2426
ResponseBody = responseBody;
2527
ResponseHeaders = responseHeaders;
28+
DSResponseBody = DeserializeResponseBody(responseBody);
29+
DSResponseHeaders = DeserializeResponseHeaders(responseHeaders);
2630
}
31+
32+
public Dictionary<string, dynamic> DeserializeResponseBody(HttpContent content)
33+
{
34+
JavaScriptSerializer jss = new JavaScriptSerializer();
35+
var ds_content = jss.Deserialize<Dictionary<string, dynamic>>(content.ReadAsStringAsync().Result);
36+
return ds_content;
37+
}
38+
39+
public Dictionary<string, string> DeserializeResponseHeaders(HttpResponseHeaders content)
40+
{
41+
var ds_content = new Dictionary<string, string>();
42+
foreach (var pair in content )
43+
{
44+
ds_content.Add(pair.Key, pair.Value.First());
45+
}
46+
return ds_content;
47+
}
48+
2749
}
2850

2951
public class Client : DynamicObject

Example/Example.cs

+13-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,19 @@ static void Main(string[] args)
2323
'limit': 100
2424
}";
2525
dynamic response = client.version("v3").api_keys.get(query_params: query_params);
26-
Console.WriteLine(response.StatusCode);
27-
Console.WriteLine(response.ResponseBody.ReadAsStringAsync().Result);
28-
Console.WriteLine(response.ResponseHeaders.ToString());
26+
//Console.WriteLine(response.StatusCode);
27+
//Console.WriteLine(response.ResponseBody.ReadAsStringAsync().Result);
28+
//Console.WriteLine(response.ResponseHeaders.ToString());
29+
30+
foreach ( var value in response.DSResponseBody["result"])
31+
{
32+
Console.WriteLine("name: {0}, api_key_id: {1}",value["name"], value["api_key_id"]);
33+
}
34+
foreach (var pair in response.DSResponseHeaders)
35+
{
36+
Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
37+
}
38+
2939

3040
Console.WriteLine("\n\nPress any key to continue.");
3141
Console.ReadLine();

0 commit comments

Comments
 (0)