|
1 | | -# csharp-http-client |
2 | | -HTTP REST client, simplified for C Sharp |
| 1 | +[](https://travis-ci.org/sendgrid/python-http-client) |
| 2 | + |
| 3 | +**Quickly and easily access any REST or REST-like API.** |
| 4 | + |
| 5 | +Here is a quick example: |
| 6 | + |
| 7 | +`GET /your/api/{param}/call` |
| 8 | + |
| 9 | +```csharp |
| 10 | +using SendGrid.CSharp.HTTP.Client; |
| 11 | +globalRequestHeaders.Add("Authorization", "Bearer XXXXXXX"); |
| 12 | +dynamic client = new Client(host: baseUrl, requestHeaders: globalRequestHeaders); |
| 13 | +client.your.api._(param).call.get() |
| 14 | +Console.WriteLine(response.StatusCode); |
| 15 | +Console.WriteLine(response.ResponseBody.ReadAsStringAsync().Result); |
| 16 | +Console.WriteLine(response.ResponseHeaders.ToString()); |
| 17 | +``` |
| 18 | + |
| 19 | +`POST /your/api/{param}/call` with headers, query parameters and a request body with versioning. |
| 20 | + |
| 21 | +```csharp |
| 22 | +using SendGrid.CSharp.HTTP.Client; |
| 23 | +globalRequestHeaders.Add("Authorization", "Bearer XXXXXXX"); |
| 24 | +dynamic client = new Client(host: baseUrl, requestHeaders: globalRequestHeaders); |
| 25 | +string queryParams = "{'Hello': 0, 'World': 1}"; |
| 26 | +string requestHeaders = "{'X-Test', 'test'}"; |
| 27 | +string requestBody = "{'some': 1, 'awesome': 2, 'data': 3}"; |
| 28 | +var response = client.your.api._(param).call.post(requestBody: requestBody, |
| 29 | + queryParams: queryParams, |
| 30 | + requestHeaders: requestHeaders) |
| 31 | +Console.WriteLine(response.StatusCode); |
| 32 | +Console.WriteLine(response.ResponseBody.ReadAsStringAsync().Result); |
| 33 | +Console.WriteLine(response.ResponseHeaders.ToString()); |
| 34 | +``` |
| 35 | + |
| 36 | +# Installation |
| 37 | + |
| 38 | +To use CSharp.HTTP.Client in your C# project, you can either <a href="https://github.com/sendgrid/csharp-http-client.git">download the SendGrid C# .NET libraries directly from our Github repository</a> or, if you have the NuGet package manager installed, you can grab them automatically. |
| 39 | + |
| 40 | +``` |
| 41 | +PM> Install-Package SendGrid.CSharp.Http.Client |
| 42 | +``` |
| 43 | + |
| 44 | +Once you have the library properly referenced in your project, you can include calls to them in your code. |
| 45 | +For a sample implementation, check the [Example](https://github.com/sendgrid/csharp-http-client/tree/master/Example) folder. |
| 46 | + |
| 47 | +Add the following namespace to use the library: |
| 48 | +```csharp |
| 49 | +using SendGrid.CSharp.HTTP.Client; |
| 50 | +``` |
| 51 | + |
| 52 | +## Usage ## |
| 53 | + |
| 54 | +Following is an example using SendGrid. You can get your free account [here](https://sendgrid.com/free?source=csharp-http-client). |
| 55 | + |
| 56 | +First, update your Environment Variable with your [SENDGRID_APIKEY](https://app.sendgrid.com/settings/api_keys). |
| 57 | + |
| 58 | +Following is an abridged example, here is the [full working code](https://github.com/sendgrid/csharp-http-client/blob/master/Example/Example.cs). |
| 59 | + |
| 60 | +```csharp |
| 61 | +using System; |
| 62 | +using System.Collections.Generic; |
| 63 | +using SendGrid.CSharp.HTTP.Client; |
| 64 | +using System.Web.Script.Serialization; |
| 65 | + |
| 66 | +class Example |
| 67 | +{ |
| 68 | + static void Main(string[] args) |
| 69 | + { |
| 70 | + String host = "https://api.sendgrid.com"; |
| 71 | + Dictionary<String, String> requestHeaders = new Dictionary<String, String>(); |
| 72 | + string apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User); |
| 73 | + requestHeaders.Add("Authorization", "Bearer " + apiKey); |
| 74 | + requestHeaders.Add("Content-Type", "application/json"); |
| 75 | + |
| 76 | + String version = "v3"; |
| 77 | + dynamic client = new Client(host, requestHeaders, version); |
| 78 | + |
| 79 | + // GET Collection |
| 80 | + string query_params = @"{ |
| 81 | + 'limit': 100 |
| 82 | + }"; |
| 83 | + dynamic response = client.version("v3").api_keys.get(query_params: query_params); |
| 84 | + var dssResponseBody = response.DeserializeResponseBody(response.ResponseBody); |
| 85 | + foreach ( var value in dssResponseBody["result"]) |
| 86 | + { |
| 87 | + Console.WriteLine("name: {0}, api_key_id: {1}",value["name"], value["api_key_id"]); |
| 88 | + } |
| 89 | + |
| 90 | + var dssResponseHeaders = response.DeserializeResponseHeaders(response.ResponseHeaders); |
| 91 | + foreach (var pair in dssResponseHeaders) |
| 92 | + { |
| 93 | + Console.WriteLine("{0}: {1}", pair.Key, pair.Value); |
| 94 | + } |
| 95 | + |
| 96 | + Console.WriteLine("\n\nPress any key to continue to POST."); |
| 97 | + Console.ReadLine(); |
| 98 | + |
| 99 | + // POST |
| 100 | + string request_body = @"{ |
| 101 | + 'name': 'My API Key 5', |
| 102 | + 'scopes': [ |
| 103 | + 'mail.send', |
| 104 | + 'alerts.create', |
| 105 | + 'alerts.read' |
| 106 | + ] |
| 107 | + }"; |
| 108 | + response = client.api_keys.post(request_body: request_body); |
| 109 | + Console.WriteLine(response.StatusCode); |
| 110 | + Console.WriteLine(response.ResponseBody.ReadAsStringAsync().Result); |
| 111 | + Console.WriteLine(response.ResponseHeaders.ToString()); |
| 112 | + JavaScriptSerializer jss = new JavaScriptSerializer(); |
| 113 | + var ds_response = jss.Deserialize<Dictionary<string, dynamic>>(response.ResponseBody.ReadAsStringAsync().Result); |
| 114 | + string api_key_id = ds_response["api_key_id"]; |
| 115 | + |
| 116 | + Console.WriteLine("\n\nPress any key to continue to GET single."); |
| 117 | + Console.ReadLine(); |
| 118 | + |
| 119 | + // GET Single |
| 120 | + response = client.api_keys._(api_key_id).get(); |
| 121 | + Console.WriteLine(response.StatusCode); |
| 122 | + Console.WriteLine(response.ResponseBody.ReadAsStringAsync().Result); |
| 123 | + Console.WriteLine(response.ResponseHeaders.ToString()); |
| 124 | + |
| 125 | + Console.WriteLine("\n\nPress any key to continue to PATCH."); |
| 126 | + Console.ReadLine(); |
| 127 | + |
| 128 | + // PATCH |
| 129 | + request_body = @"{ |
| 130 | + 'name': 'A New Hope' |
| 131 | + }"; |
| 132 | + response = client.api_keys._(api_key_id).patch(request_body: request_body); |
| 133 | + Console.WriteLine(response.StatusCode); |
| 134 | + Console.WriteLine(response.ResponseBody.ReadAsStringAsync().Result); |
| 135 | + Console.WriteLine(response.ResponseHeaders.ToString()); |
| 136 | + |
| 137 | + Console.WriteLine("\n\nPress any key to continue to PUT."); |
| 138 | + Console.ReadLine(); |
| 139 | + |
| 140 | + // PUT |
| 141 | + request_body = @"{ |
| 142 | + 'name': 'A New Hope', |
| 143 | + 'scopes': [ |
| 144 | + 'user.profile.read', |
| 145 | + 'user.profile.update' |
| 146 | + ] |
| 147 | + }"; |
| 148 | + response = client.api_keys._(api_key_id).put(request_body: request_body); |
| 149 | + Console.WriteLine(response.StatusCode); |
| 150 | + Console.WriteLine(response.ResponseBody.ReadAsStringAsync().Result); |
| 151 | + Console.WriteLine(response.ResponseHeaders.ToString()); |
| 152 | + |
| 153 | + Console.WriteLine("\n\nPress any key to continue to DELETE."); |
| 154 | + Console.ReadLine(); |
| 155 | + |
| 156 | + // DELETE |
| 157 | + response = client.api_keys._(api_key_id).delete(); |
| 158 | + Console.WriteLine(response.StatusCode); |
| 159 | + Console.WriteLine(response.ResponseHeaders.ToString()); |
| 160 | + |
| 161 | + Console.WriteLine("\n\nPress any key to exit."); |
| 162 | + Console.ReadLine(); |
| 163 | + } |
| 164 | +} |
| 165 | +``` |
| 166 | + |
| 167 | +# Announcements |
| 168 | + |
| 169 | +[2016.XX.XX] - We hit version 1! |
| 170 | + |
| 171 | +# Roadmap |
| 172 | + |
| 173 | +[Milestones](https://github.com/sendgrid/csharp-http-client/milestones) |
| 174 | + |
| 175 | +# How to Contribute |
| 176 | + |
| 177 | +We encourage contribution to our libraries, please see our [CONTRIBUTING](https://github.com/sendgrid/csharp-http-client/blob/master/CONTRIBUTING.md) guide for details. |
| 178 | + |
| 179 | +* [Feature Request](https://github.com/sendgrid/csharp-http-client/blob/master/CONTRIBUTING.md#feature_request) |
| 180 | +* [Bug Reports](https://github.com/sendgrid/csharp-http-client/blob/master/CONTRIBUTING.md#submit_a_bug_report) |
| 181 | +* [Improvements to the Codebase](https://github.com/sendgrid/csharp-http-client/blob/master/CONTRIBUTING.md#improvements_to_the_codebase) |
| 182 | + |
| 183 | +# Thanks |
| 184 | + |
| 185 | +We were inspired by the work done on [birdy](https://github.com/inueni/birdy) and [universalclient](https://github.com/dgreisen/universalclient). |
| 186 | + |
| 187 | +# About |
| 188 | + |
| 189 | +![SendGrid Logo] |
| 190 | +(https://assets3.sendgrid.com/mkt/assets/logos_brands/small/sglogo_2015_blue-9c87423c2ff2ff393ebce1ab3bd018a4.png) |
| 191 | + |
| 192 | +csharp-http-client is guided and supported by the SendGrid [Developer Experience Team](mailto:dx@sendgrid.com). |
| 193 | + |
| 194 | +csharp-http-client is maintained and funded by SendGrid, Inc. The names and logos for python-http-client are trademarks of SendGrid, Inc. |
0 commit comments