Improved regex and threading performance and current .NET frameworks over deprecated/forked PayPalHttp library.
PayPalHttp is a generic HTTP Client used with generated server SDKs.
In it's simplest form, an HttpClient
exposes an Execute
method which takes an HTTP request, executes it against the domain described in an Environment, and returns an HTTP response.
An Environment
describes a domain that hosts a REST API, against which an HttpClient
will make requests. Environment
is a simple interface that wraps one method, BaseUrl
.
var env = new Environment('https://example.com')
HTTP requests contain all the information needed to make an HTTP request against the REST API. Specifically, one request describes a path, a method, any path/query/form parameters, headers, attached files for upload, and body data.
These objects are constructed in code generated by the sdkgen project. Instructions for using generated HTTP request subclasses is provided in that project.
HTTP responses contain information returned by a server in response to a request as described above. They are simple objects which contain a status code, headers, and any data returned by the server.
var client = new HttpClient(env);
var request = new HttpRequest("/", HttpMethod.Get);
request.Body = "some data";
var response = await client.Execute(request);
var statusCode = response.StatusCode;
var headers = response.Headers;
var data = response.Result<String>();
Injectors are blocks that can be used for executing arbitrary pre-flight logic, such as modifying a request or logging data. Injectors are attached to an HttpClient
using the AddInjector
method.
The HttpClient
executes its injectors in a first-in, first-out order, before each request.
class LogInjector : IInjector
{
public void Inject(HttpRequest request)
{
// Do some logging here
}
}
var logInjector = new LogInjector();
client.AddInjector(logInjector);
...
HttpClient#Execute
may throw an HttpException
if something went wrong during the course of execution. If the server returned a non-200 response, HttpException will be thrown, that will contain a status code and headers you can use for debugging.
try
{
client.Execute(request);
}
catch (HttpException ex)
{
var statusCode = ex.StatusCode;
var headers = ex.Headers;
var message = ex.response<String>();
}
PayPalHttp-Dotnet is open source and available under the MIT license. See the LICENSE file for more information.
Pull requests and issues are welcome. Please see CONTRIBUTING.md for more details.