Skip to content

Commit 4af066c

Browse files
committed
Fix intro a bit
1 parent b8e0fdf commit 4af066c

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

docs/intro.md

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ If you only have a few number of one-off requests to make to an API, you can use
2828
using RestSharp;
2929
using RestSharp.Authenticators;
3030

31-
var client = new RestClient("https://api.twitter.com/1.1") {
31+
var options = new RestClientOptions("https://api.twitter.com/1.1") {
3232
Authenticator = new HttpBasicAuthenticator("username", "password")
3333
};
34+
var client = new RestClient(options);
3435
var request = new RestRequest("statuses/home_timeline.json");
36+
// The cancellation token comes from the caller. You can still make a call without it.
3537
var response = await client.GetAsync(request, cancellationToken);
3638
```
3739

@@ -46,21 +48,34 @@ For example:
4648
using RestSharp;
4749
using RestSharp.Authenticators;
4850

49-
var client = new RestClient("https://api.twitter.com/1.1");
50-
client.Authenticator = new HttpBasicAuthenticator("username", "password");
51+
var options = new RestClientOptions("https://api.twitter.com/1.1") {
52+
Authenticator = new HttpBasicAuthenticator("username", "password")
53+
};
54+
var client = new RestClient(options);
5155

52-
var request = new RestRequest("statuses/home_timeline.json", DataFormat.Json);
56+
var request = new RestRequest("statuses/home_timeline.json");
5357

58+
// The cancellation token comes from the caller. You can still make a call without it.
5459
var timeline = await client.GetAsync<HomeTimeline>(request, cancellationToken);
5560
```
5661

57-
The most important difference, however, that async methods that are named after HTTP methods return the `Task<T>` instead of `Task<IRestResponse<T>>`. Because it means that you won't get an error response if the request fails, those methods
58-
throw an exception.
62+
Both snippets about use the `GetAsync` extension, which is a wrapper about `ExecuteGetAsync`, which, in turn, is a wrapper around `ExecuteAsync`.
63+
All `ExecuteAsync` overloads and return the `RestResponse` or `RestResponse<T>`.
5964

60-
All `ExecuteAsync` overloads, however, behave in the same way as `Execute` and return the `IRestResponse` or `IRestResponse<T>`.
65+
The most important difference is that async methods that are named after HTTP methods return the `Task<T>` instead of `Task<RestResponse<T>>`. Because it means that you won't get an error response if the request fails, those methods
66+
throw an exception. For keeping the API consistent, non-generic functions like `GetAsync` or `PostAsync` also throw an exception if the request fails, although they return the `Task<RestResponse>`.
6167

6268
Read [here](error-handling.md) about how RestSharp handles exceptions.
6369

70+
RestSharp also offers even simpler way to make JSON calls. You can use the `GetJsonAsync` and `PostJsonAsync` extension methods, which will automatically serialize the request body to JSON and deserialize the response to the specified type.
71+
72+
```csharp
73+
var client = new RestClient(options);
74+
var timeline = await client.GetJsonAsync<HomeTimeline>("statuses/home_timeline.json", cancellationToken);
75+
```
76+
77+
Read [here](usage.md#json-requests) about making JSON calls without preparing a request object.
78+
6479
### Content type
6580

6681
RestSharp supports sending XML or JSON body as part of the request. To add a body to the request, simply call `AddJsonBody` or `AddXmlBody` method of the `IRestRequest` instance.

0 commit comments

Comments
 (0)