Skip to content

Commit ec230be

Browse files
authored
Update usage.md (#1718)
Fix grammar, typos, and readability of the usage documentation
1 parent 887c566 commit ec230be

File tree

1 file changed

+24
-33
lines changed

1 file changed

+24
-33
lines changed

docs/usage.md

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ title: Usage
44

55
## Recommended usage
66

7-
RestSharp works best as the foundation for a proxy class for your API. Each API would most probably require different settings for `RestClient`, so a dedicated API class (and its interface) gives you a nice isolation between different `RestClient` instances, and make them testable.
7+
RestSharp works best as the foundation for a proxy class for your API. Each API would most probably require different settings for `RestClient`. Hence, a dedicated API class (and its interface) gives you sound isolation between different `RestClient` instances and make them testable.
88

99
Essentially, RestSharp is a wrapper around `HttpClient` that allows you to do the following:
1010
- Add default parameters of any kind (not just headers) to the client, once
1111
- Add parameters of any kind to each request (query, URL segment, form, attachment, serialized body, header) in a straightforward way
1212
- Serialize the payload to JSON or XML if necessary
13-
- Set the correct content headers (content type, disposition, length, etc)
13+
- Set the correct content headers (content type, disposition, length, etc.)
1414
- Handle the remote endpoint response
1515
- Deserialize the response from JSON or XML if necessary
1616

17-
As an example, let's look at a simple Twitter API v2 client, which uses OAuth2 machine-to-machine authentication. For it to work, you would need to have access to Twitter Developers portal, an a project, and an approved application inside the project with OAuth2 enabled.
17+
For example, let's look at a simple Twitter API v2 client, which uses OAuth2 machine-to-machine authentication. For it to work, you would need to have access to the Twitter Developers portal, a project, and an approved application inside the project with OAuth2 enabled.
1818

1919
### Authenticator
2020

21-
Before we can make any call to the API itself, we need to get a bearer token. Twitter exposes an endpoint `https://api.twitter.com/oauth2/token`. As it follows the OAuth2 conventions, the code can be used to create an authenticator for some other vendors.
21+
Before we can call the API itself, we need to get a bearer token. Twitter exposes an endpoint `https://api.twitter.com/oauth2/token`. As it follows the OAuth2 conventions, the code can be used to create an authenticator for some other vendors.
2222

2323
First, we need a model for deserializing the token endpoint response. OAuth2 uses snake case for property naming, so we need to decorate model properties with `JsonPropertyName` attribute:
2424

@@ -31,9 +31,9 @@ record TokenResponse {
3131
}
3232
```
3333

34-
Next, we create the authenticator itself. It needs the API key and API key secret for calling the token endpoint using basic HTTP authentication. In addition, we can extend the list of parameters with the base URL, so it can be converted to a more generic OAuth2 authenticator.
34+
Next, we create the authenticator itself. It needs the API key and API key secret to call the token endpoint using basic HTTP authentication. In addition, we can extend the list of parameters with the base URL to convert it to a more generic OAuth2 authenticator.
3535

36-
The easiest way to create an authenticator is to inherit is from the `AuthanticatorBase` base class:
36+
The easiest way to create an authenticator is to inherit from the `AuthanticatorBase` base class:
3737

3838
```csharp
3939
public class TwitterAuthenticator : AuthenticatorBase {
@@ -54,9 +54,9 @@ public class TwitterAuthenticator : AuthenticatorBase {
5454
}
5555
```
5656

57-
During the first call made by the client using the authenticator, it will find out that the `Token` property is empty. It will then call the `GetToken` function to get the token once, and then will reuse the token going forwards.
57+
During the first call made by the client using the authenticator, it will find out that the `Token` property is empty. It will then call the `GetToken` function to get the token once and reuse the token going forward.
5858

59-
Now, we need to include the `GetToken` function to the class:
59+
Now, we need to implement the `GetToken` function in the class:
6060

6161
```csharp
6262
async Task<string> GetToken() {
@@ -72,7 +72,7 @@ async Task<string> GetToken() {
7272
}
7373
```
7474

75-
As we need to make a call to the token endpoint, we need our own, short-lived instance of `RestClient`. Unlike the actual Twitter client, it will use the `HttpBasicAuthenticator` to send API key and secret as username and password. The client is then gets disposed as we only use it once.
75+
As we need to make a call to the token endpoint, we need our own short-lived instance of `RestClient`. Unlike the actual Twitter client, it will use the `HttpBasicAuthenticator` to send the API key and secret as the username and password. The client then gets disposed as we only use it once.
7676

7777
Here we add a POST parameter `grant_type` with `client_credentials` as its value. At the moment, it's the only supported value.
7878

@@ -97,8 +97,8 @@ public record TwitterUser(string Id, string Name, string Username);
9797
When that is done, we can implement the interface and add all the necessary code blocks to get a working API client.
9898

9999
The client class needs the following:
100-
- A constructor, which accepts API credentials to be passed to the authenticator
101-
- A wrapped `RestClient` instance with Twitter API base URI pre-configured
100+
- A constructor, which accepts API credentials to pass to the authenticator
101+
- A wrapped `RestClient` instance with the Twitter API base URI pre-configured
102102
- The `TwitterAuthenticator` that we created previously as the client authenticator
103103
- The actual function to get the user
104104

@@ -131,9 +131,9 @@ public class TwitterClient : ITwitterClient, IDisposable {
131131
}
132132
```
133133

134-
Couple of things that don't fall to the "basics" list.
135-
- The API client class needs to be disposable, so it can dispose the wrapped `HttpClient` instance
136-
- Twitter API returns wrapped models. In this case we use the `TwitterSingleObject` wrapper, in other methods you'd need a similar object with `T[] Data` to accept collections
134+
The code above includes a couple of things that go beyond the "basics", and so we won't cover them here:
135+
- The API client class needs to be disposable, so that it can dispose of the wrapped `HttpClient` instance
136+
- Twitter API returns wrapped models. In this case, we use the `TwitterSingleObject` wrapper. In other methods, you'd need a similar object with `T[] Data` to accept collections
137137

138138
You can find the full example code in [this gist](https://gist.github.com/alexeyzimarev/62d77bb25d7aa5bb4b9685461f8aabdd).
139139

@@ -168,15 +168,15 @@ After you've created a `RestRequest`, you can add parameters to it. Below, you c
168168

169169
### Http Header
170170

171-
Adds the parameter as an HTTP header that is sent along with the request. The header name is the name of the parameter and the header value is the value.
171+
Adds the parameter as an HTTP header that is sent along with the request. The header name is the parameter's name and the header value is the value.
172172

173173
::: warning Content-Type
174-
RestSharp will use the correct content type by default. Avoid adding the `Content-Type` header manually to your requests, unless you are absolutely sure it is required. You can add a custom content type to the [body parameter](#request-body) itself.
174+
RestSharp will use the correct content type by default. Avoid adding the `Content-Type` header manually to your requests unless you are absolutely sure it is required. You can add a custom content type to the [body parameter](#request-body) itself.
175175
:::
176176

177177
### Get or Post
178178

179-
This behaves differently based on the method. If you execute a GET call, RestSharp will append the parameters to the Url in the form `url?name1=value1&name2=value2`.
179+
`GetOrPost` behaves differently based on the method. If you execute a GET call, RestSharp will append the parameters to the Url in the form `url?name1=value1&name2=value2`.
180180

181181
On a POST or PUT Requests, it depends on whether you have files attached to a Request.
182182
If not, the Parameters will be sent as the body of the request in the form `name1=value1&name2=value2`. Also, the request will be sent as `application/x-www-form-urlencoded`.
@@ -219,15 +219,6 @@ request.AddStringBody(json, ContentType.Json);
219219

220220
You can specify a custom body content type if necessary. The `contentType` argument is available in all the overloads that add a request body.
221221

222-
#### AddStringBody
223-
224-
If you have a pre-serialized payload like a JSON string, you can use `AddStringBody` to add it as a body parameter. You need to specify the content type, so the remote endpoint knows what to do with the request body. For example:
225-
226-
```csharp
227-
const json = "{ data: { foo: \"bar\" } }";
228-
request.AddStringBody(json, ContentType.Json);
229-
```
230-
231222
#### AddJsonBody
232223

233224
When you call `AddJsonBody`, it does the following for you:
@@ -237,7 +228,7 @@ When you call `AddJsonBody`, it does the following for you:
237228
- Sets the internal data type of the request body to `DataType.Json`
238229

239230
::: warning
240-
Do not send JSON string or some sort of `JObject` instance to `AddJsonBody`, it won't work! Use `AddStringBody` instead.
231+
Do not send JSON string or some sort of `JObject` instance to `AddJsonBody`; it won't work! Use `AddStringBody` instead.
241232
:::
242233

243234
Here is the example:
@@ -256,12 +247,12 @@ When you call `AddXmlBody`, it does the following for you:
256247
- Sets the internal data type of the request body to `DataType.Xml`
257248

258249
::: warning
259-
Do not send XML string to `AddXmlBody`, it won't work!
250+
Do not send XML string to `AddXmlBody`; it won't work!
260251
:::
261252

262253
### Query String
263254

264-
This works like `GetOrPost`, except that it always appends the parameters to the url in the form `url?name1=value1&name2=value2`, regardless of the request method.
255+
`QueryString` works like `GetOrPost`, except that it always appends the parameters to the url in the form `url?name1=value1&name2=value2`, regardless of the request method.
265256

266257
Example:
267258

@@ -280,11 +271,11 @@ You can also specify the query string parameter type explicitly:
280271
request.AddParameter("foo", "bar", ParameterType.QueryString);
281272
```
282273

283-
In some cases you might need to prevent RestSharp from encoding the query string parameter. To do so, use the `QueryStringWithoutEncode` parameter type.
274+
In some cases, you might need to prevent RestSharp from encoding the query string parameter. To do so, use the `QueryStringWithoutEncode` parameter type.
284275

285276
## Making a call
286277

287-
When you have a `RestRequest` instance with all the parameters added to it, you are ready to make a request.
278+
Once you've added all the parameters to your `RestRequest`, you are ready to make a request.
288279

289280
`RestClient` has a single function for this:
290281

@@ -331,13 +322,13 @@ Those extensions will throw an exception if the server returns an error, as ther
331322

332323
### JSON requests
333324

334-
For making a simple `GET` call and get a deserialized JSON response with a pre-formed resource string, use this:
325+
To make a simple `GET` call and get a deserialized JSON response with a pre-formed resource string, use this:
335326

336327
```csharp
337328
var response = await client.GetJsonAsync<TResponse>("endpoint?foo=bar", cancellationToken);
338329
```
339330

340-
You can also use a more advance extension that uses an object to compose the resource string:
331+
You can also use a more advanced extension that uses an object to compose the resource string:
341332

342333
```csharp
343334
var client = new RestClient("https://example.org");

0 commit comments

Comments
 (0)