https://github.com/paypal/paypalhttp_dotnet
https://github.com/paypal/paypalhttp_dotnet
Last synced: 12 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/paypal/paypalhttp_dotnet
- Owner: paypal
- License: mit
- Created: 2019-10-03T23:20:56.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-11-23T19:54:21.000Z (over 2 years ago)
- Last Synced: 2024-04-13T19:27:26.283Z (over 2 years ago)
- Language: C#
- Size: 59.6 KB
- Stars: 5
- Watchers: 9
- Forks: 11
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# Deprecation Notice:
This SDK is deprecated; you can continue to use it, but no new features or support requests will be accepted. An integration with [the new Server SDK](https://github.com/paypal/PayPal-Server-SDKs) is recommended. Review the [docs](https://developer.paypal.com/serversdk/http/getting-started/how-to-get-started/) for details.
## PayPal HttpClient
PayPalHttp is a generic HTTP Client used with [generated server SDKs](https://github.braintreeps.com/dx/sdkgen).
In it's simplest form, an [`HttpClient`](./PayPalHttp-Dotnet/HttpClient.cs) exposes an `Execute` method which takes an [HTTP request](./PayPalHttp-Dotnet/HttpRequest.cs), executes it against the domain described in an [Environment](./PayPalHttp-Dotnet/Environment.cs), and returns an [HTTP response](./PayPalHttp-Dotnet/HttpResponse.cs).
### Environment
An [`Environment`](./PayPalHttp-Dotnet/Environment.cs) 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`.
```C#
var env = new Environment('https://example.com')
```
### Requests
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](http://github.braintreeps.com/dx/sdkgen) project. Instructions for using generated HTTP request subclasses is provided in that project.
### Responses
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.
```C#
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();
```
### Injectors
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.
```C#
class LogInjector : IInjector
{
public void Inject(HttpRequest request)
{
// Do some logging here
}
}
var logInjector = new LogInjector();
client.AddInjector(logInjector);
...
```
### Error Handling
`HttpClient#Execute` may throw an `HttpException` if something went wrong during the course of execution. If the server returned a non-200 response, [HttpException](./PayPalHttp-Dotnet/HttpException.cs) will be thrown, that will contain a status code and headers you can use for debugging.
```C#
try
{
client.Execute(request);
}
catch (HttpException ex)
{
var statusCode = ex.StatusCode;
var headers = ex.Headers;
var message = ex.response();
}
```
## License
PayPalHttp-Dotnet is open source and available under the MIT license. See the [LICENSE](./LICENSE) file for more information.
## Contributing
Pull requests and issues are welcome. Please see [CONTRIBUTING.md](./CONTRIBUTING.md) for more details.