Skip to content

Commit 5ae0fd5

Browse files
authored
docs: consolidate (#679)
* docs: migrate custom http client content from docs to repo * docs: consolidate docs information with README.md
1 parent e07390c commit 5ae0fd5

File tree

5 files changed

+201
-15
lines changed

5 files changed

+201
-15
lines changed

CHANGES.md

-2
Original file line numberDiff line numberDiff line change
@@ -3015,6 +3015,4 @@ The newest version of the `twilio-csharp` helper library!
30153015

30163016
This version brings a host of changes to update and modernize the `twilio-csharp` helper library. It is auto-generated to produce a more consistent and correct product.
30173017

3018-
- [Migration Guide](https://www.twilio.com/docs/libraries/csharp/migrating-your-csharp-dot-net-application-twilio-sdk-4x-5x)
30193018
- [Full API Documentation](https://twilio.github.io/twilio-csharp/)
3020-
- [General Documentation](https://www.twilio.com/docs/libraries/csharp)

CONTRIBUTING.md

-5
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,6 @@ you're working on.
8484
For large fixes, please build and test the documentation before submitting the
8585
PR to be sure you haven't accidentally introduced layout or formatting issues.
8686

87-
If you want to help improve the docs at
88-
[https://www.twilio.com/docs/libraries/csharp][docs-link], please contact
89-
[help@twilio.com](mailto:help@twilio.com).
90-
9187
## <a name="submit"></a> Submission Guidelines
9288

9389
### Submitting an Issue
@@ -171,6 +167,5 @@ you are working:
171167
* All features or bug fixes **must be tested** by one or more tests.
172168
* All classes and methods **must be documented**.
173169
174-
[docs-link]: https://www.twilio.com/docs/libraries/csharp
175170
[issue-link]: https://github.com/twilio/twilio-csharp/issues/new
176171
[github]: https://github.com/twilio/twilio-csharp

README.md

+14-6
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ More documentation for this library can be found [here][libdocs].
1717

1818
### Migrate from earlier versions
1919

20-
See the migration guide [here][migrating]. Also, if you were using the `Twilio.Mvc` package, that has been replaced by the [Twilio.AspNet.Mvc][aspnet] package which is compatible with this version of the library.
20+
See the migration guide [here](./UPGRADE.md). Also, if you were using the `Twilio.Mvc` package, that has been replaced by the [Twilio.AspNet.Mvc][aspnet] package which is compatible with this version of the library.
2121

2222
### TLS 1.2 Requirements
2323

2424
New accounts and subaccounts are now required to use TLS 1.2 when accessing the REST API. ["Upgrade Required" errors](https://www.twilio.com/docs/api/errors/20426) indicate that TLS 1.0/1.1 is being used. With .NET, you can enable TLS 1.2 using this setting:
25+
2526
```csharp
2627
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
2728
```
@@ -38,17 +39,22 @@ The best and easiest way to add the Twilio libraries to your .NET project is to
3839

3940
From within Visual Studio, you can use the NuGet GUI to search for and install the Twilio NuGet package. Or, as a shortcut, simply type the following command into the Package Manager Console:
4041

41-
Install-Package Twilio
42+
```shell
43+
Install-Package Twilio
44+
```
4245

4346
### With .NET Core Command Line Tools
4447

4548
If you are building with the .NET Core command line tools, then you can run the following command from within your project directory:
4649

47-
dotnet add package Twilio
50+
```shell
51+
dotnet add package Twilio
52+
```
4853

4954
## Sample usage
5055

5156
The examples below show how to have your application initiate and outbound phone call and send an SMS message using the Twilio .NET helper library:
57+
5258
```csharp
5359
TwilioClient.Init("ACCOUNT_SID", "AUTH_TOKEN");
5460

@@ -79,6 +85,7 @@ TwilioClient.SetEdge("sydney");
7985
This will result in the `hostname` transforming from `api.twilio.com` to `api.sydney.au1.twilio.com`.
8086

8187
## Enable debug logging
88+
8289
There are two ways to enable debug logging in the default HTTP client. You can create an environment variable called `TWILIO_LOG_LEVEL` and set it to `debug` or you can set the LogLevel variable on the client as debug:
8390

8491
```csharp
@@ -131,30 +138,32 @@ Console.WriteLine(response);
131138

132139
## Use a custom HTTP Client
133140

134-
To use a custom HTTP client with this helper library, please see the [Twilio documentation](https://www.twilio.com/docs/libraries/csharp-dotnet/custom-http-clients-dot-net-framework).
141+
To use a custom HTTP client with this helper library, please see the [advanced example of how to do so](./advanced-examples/custom-http-client.md).
135142

136143
## Docker Image
137144

138145
The `Dockerfile` present in this repository and its respective `twilio/twilio-csharp` Docker image are used by Twilio for testing purposes.
139146

140147
You could use the docker image for building and running tests:
148+
141149
```bash
142150
docker build -t twiliobuild .
143151
```
144152

145153
Bash:
154+
146155
```bash
147156
docker run -it --rm -v $(pwd):/twilio twiliobuild
148157
make test
149158
```
150159

151160
Powershell:
161+
152162
```pwsh
153163
docker run -it --rm -v ${PWD}:/twilio twiliobuild
154164
make test
155165
```
156166

157-
158167
## Get support
159168

160169
If you need help installing or using the library, please check the [Twilio Support Help Center](https://support.twilio.com) first, and [file a support ticket](https://twilio.com/help/contact) if you don't find an answer to your question.
@@ -165,5 +174,4 @@ If you've instead found a bug in the library or would like new features added, g
165174
[apidocs]: https://www.twilio.com/docs/api
166175
[twiml]: https://www.twilio.com/docs/api/twiml
167176
[libdocs]: https://www.twilio.com/docs/libraries/reference/twilio-csharp/
168-
[migrating]: https://www.twilio.com/docs/libraries/csharp-dotnet/usage-guide
169177
[aspnet]: https://github.com/twilio/twilio-aspnet

UPGRADE.md

-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ _`MAJOR` version bumps will have upgrade notes posted here._
1010

1111
Behind the scenes Csharp Helper is now auto-generated via OpenAPI with this release. This enables us to rapidly add new features and enhance consistency across versions and languages.
1212

13-
To learn more about the Csharp Helper Library, check out [our docs](https://www.twilio.com/docs/libraries/csharp-dotnet).
14-
1513
[2017-11-XX] 5.8.x to 5.9.x
1614
---------------------------
1715

+187
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
# Custom HTTP Clients for the Twilio C# Helper Library with .NET Framework
2+
3+
If you are working with the Twilio C# / .NET Helper Library, and you need to be able to modify the HTTP requests that the library makes to the Twilio servers, you’re in the right place. The most common need to alter the HTTP request is to connect and authenticate with an enterprise’s proxy server. We’ll provide sample code that you can drop right into your app to handle this use case.
4+
5+
## Connect and authenticate with a proxy server
6+
7+
To connect and provide credentials to a proxy server that may be between your app and Twilio, you need a way to modify the HTTP requests that the Twilio helper library makes on your behalf to invoke the Twilio REST API.
8+
9+
On .NET 4.5.1 and above, the Twilio helper library uses the `HttpClient` class (in the `System.Net.Http` `namespace`) under the hood to make the HTTP requests. Knowing this, the following two facts should help us arrive at the solution:
10+
11+
- Connecting to a proxy server with `HttpClient` is a [solved problem](https://gist.github.com/bryanbarnard/8102915).
12+
- The Twilio Helper Library allows you to provide your own `HttpClient` for making API requests.
13+
14+
So the question becomes how do we apply this to a typical Twilio REST API example?
15+
16+
```csharp
17+
TwilioClient.Init(accountSid, authToken);
18+
19+
var message = MessageResource.Create(
20+
to: new PhoneNumber("+15558675309"),
21+
from: new PhoneNumber("+15017250604"),
22+
body: "Hello from C#");
23+
```
24+
25+
Where does a `TwilioRestClient` get created and used? Out of the box, the helper library is creating a default `TwilioRestClient` for you, using the Twilio credentials you pass to the `Init` method. However, there’s nothing stopping you from creating your own and using that.
26+
27+
Once you have your own `TwilioRestClient`, you can pass it to any Twilio REST API resource action you want. Here’s an example of sending an SMS message with a custom client:
28+
29+
```csharp
30+
using System;
31+
using Twilio.Rest.Api.V2010.Account;
32+
using Twilio.Types;
33+
34+
namespace CustomClientDotNet4x
35+
{
36+
class Program
37+
{
38+
static void Main(string[] args)
39+
{
40+
var twilioRestClient = ProxiedTwilioClientCreator.GetClient();
41+
42+
// Now that we have our custom built TwilioRestClient,
43+
// we can pass it to any REST API resource action.
44+
var message = MessageResource.Create(
45+
to: new PhoneNumber("+15017122661"),
46+
from: new PhoneNumber("+15017122661"),
47+
body: "Hey there!",
48+
// Here's where you inject the custom client
49+
client: twilioRestClient
50+
);
51+
52+
Console.WriteLine($"Message SID: {message.Sid}");
53+
}
54+
}
55+
}
56+
```
57+
58+
## Create your custom TwilioRestClient
59+
60+
When you take a closer look at the constructor for `TwilioRestClient`, you see that the `httpClient` parameter is actually of type `Twilio.Http.HttpClient` and not the `System.Net.HttpClient` we were expecting. `Twilio.Http.HttpClient` is actually an abstraction that allows plugging in any implementation of an HTTP client you want (or even creating a mocking layer for unit testing).
61+
62+
However, within the helper library, there is an implementation of `Twilio.Http.HttpClient` called SystemNetHttpClient. This class wraps the `System.Net.HttpClient` and provides it to the Twilio helper library to make the necessary HTTP requests.
63+
64+
## Call Twilio through the proxy server
65+
66+
Now that we understand how all the components fit together, we can create our own `TwilioRestClient` that can connect through a proxy server. To make this reusable, here’s a class that you can use to create this `TwilioRestClient` whenever you need one.
67+
68+
```csharp
69+
using System;
70+
using System.Configuration;
71+
using System.Net;
72+
using System.Net.Http;
73+
using System.Text;
74+
using Twilio.Clients;
75+
76+
namespace CustomClientDotNet4x
77+
{
78+
public static class ProxiedTwilioClientCreator
79+
{
80+
private static HttpClient _httpClient;
81+
82+
private static void CreateHttpClient()
83+
{
84+
var proxyUrl = ConfigurationManager.AppSettings["ProxyServerUrl"];
85+
var handler = new HttpClientHandler()
86+
{
87+
Proxy = new WebProxy(proxyUrl),
88+
UseProxy = true
89+
};
90+
91+
_httpClient = new HttpClient(handler);
92+
var byteArray = Encoding.Unicode.GetBytes(
93+
ConfigurationManager.AppSettings["ProxyUsername"]
94+
+ ":"
95+
+ ConfigurationManager.AppSettings["ProxyPassword"]
96+
);
97+
98+
_httpClient.DefaultRequestHeaders.Authorization =
99+
new System.Net.Http.Headers.AuthenticationHeaderValue(
100+
"Basic",
101+
Convert.ToBase64String(byteArray)
102+
);
103+
}
104+
105+
public static TwilioRestClient GetClient()
106+
{
107+
var accountSid = ConfigurationManager.AppSettings["TwilioAccountSid"];
108+
var authToken = ConfigurationManager.AppSettings["TwilioAuthToken"];
109+
110+
if (_httpClient == null)
111+
{
112+
// It's best* to create a single HttpClient and reuse it
113+
// * See: https://goo.gl/FShAAe
114+
CreateHttpClient();
115+
}
116+
117+
var twilioRestClient = new TwilioRestClient(
118+
accountSid,
119+
authToken,
120+
httpClient: new Twilio.Http.SystemNetHttpClient(_httpClient)
121+
);
122+
123+
return twilioRestClient;
124+
}
125+
}
126+
}
127+
```
128+
129+
Notice the use of `ConfigurationManager.AppSettings` to retrieve various configuration settings:
130+
131+
- Your Twilio Account Sid and Auth Token ([found here, in the Twilio console](https://console.twilio.con))
132+
- Your proxy server URL (including the server name or address and port number)
133+
- Your username and password for the proxy server
134+
135+
These settings can be placed in your Web.config or App.config (for a console app) like so:
136+
137+
```xml
138+
<appSettings>
139+
<!-- Find your Twilio Account Sid and Token at twilio.com/console -->
140+
<add key="TwilioAccountSid" value="ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" />
141+
<add key="TwilioAuthToken" value="your_auth_token" />
142+
143+
<!-- Replace the following with your proxy server's settings -->
144+
<add key="ProxyServerUrl" value="http://127.0.0.1:8888"/>
145+
<add key="ProxyUsername" value="your_username"/>
146+
<add key="ProxyPassword" value="your_password"/>
147+
</appSettings>
148+
```
149+
150+
Here’s a console program that sends a text message and shows how it all can work together.
151+
152+
```csharp
153+
using System;
154+
using Twilio.Rest.Api.V2010.Account;
155+
using Twilio.Types;
156+
157+
namespace CustomClientDotNet4x
158+
{
159+
class Program
160+
{
161+
static void Main(string[] args)
162+
{
163+
var twilioRestClient = ProxiedTwilioClientCreator.GetClient();
164+
165+
// Now that we have our custom built TwilioRestClient,
166+
// we can pass it to any REST API resource action.
167+
var message = MessageResource.Create(
168+
to: new PhoneNumber("+15017122661"),
169+
from: new PhoneNumber("+15017122661"),
170+
body: "Hey there!",
171+
// Here's where you inject the custom client
172+
client: twilioRestClient
173+
);
174+
175+
Console.WriteLine($"Message SID: {message.Sid}");
176+
}
177+
}
178+
}
179+
```
180+
181+
## What else can this technique be used for?
182+
183+
Now that you know how to inject your own System.Net.HttpClient into the Twilio API request pipeline, you could use this technique to add custom HTTP headers to the requests (perhaps as required by an upstream proxy server).
184+
185+
You could also implement your own Twilio.Http.HttpClient to mock the Twilio API responses so your unit and integration tests can run quickly without the need to make a connection to Twilio. In fact, there’s [already an example online](https://github.com/dprothero/twilio-mock-example) showing how to do exactly that.
186+
187+
We can’t wait to see what you build!

0 commit comments

Comments
 (0)