You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/usage.md
+24-33Lines changed: 24 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,21 +4,21 @@ title: Usage
4
4
5
5
## Recommended usage
6
6
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.
8
8
9
9
Essentially, RestSharp is a wrapper around `HttpClient` that allows you to do the following:
10
10
- Add default parameters of any kind (not just headers) to the client, once
11
11
- Add parameters of any kind to each request (query, URL segment, form, attachment, serialized body, header) in a straightforward way
12
12
- 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.)
14
14
- Handle the remote endpoint response
15
15
- Deserialize the response from JSON or XML if necessary
16
16
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.
18
18
19
19
### Authenticator
20
20
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.
22
22
23
23
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:
24
24
@@ -31,9 +31,9 @@ record TokenResponse {
31
31
}
32
32
```
33
33
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.
35
35
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:
@@ -54,9 +54,9 @@ public class TwitterAuthenticator : AuthenticatorBase {
54
54
}
55
55
```
56
56
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.
58
58
59
-
Now, we need to include the `GetToken` function to the class:
59
+
Now, we need to implement the `GetToken` function in the class:
60
60
61
61
```csharp
62
62
asyncTask<string>GetToken() {
@@ -72,7 +72,7 @@ async Task<string> GetToken() {
72
72
}
73
73
```
74
74
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.
76
76
77
77
Here we add a POST parameter `grant_type` with `client_credentials` as its value. At the moment, it's the only supported value.
78
78
@@ -97,8 +97,8 @@ public record TwitterUser(string Id, string Name, string Username);
97
97
When that is done, we can implement the interface and add all the necessary code blocks to get a working API client.
98
98
99
99
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
102
102
- The `TwitterAuthenticator` that we created previously as the client authenticator
103
103
- The actual function to get the user
104
104
@@ -131,9 +131,9 @@ public class TwitterClient : ITwitterClient, IDisposable {
131
131
}
132
132
```
133
133
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
137
137
138
138
You can find the full example code in [this gist](https://gist.github.com/alexeyzimarev/62d77bb25d7aa5bb4b9685461f8aabdd).
139
139
@@ -168,15 +168,15 @@ After you've created a `RestRequest`, you can add parameters to it. Below, you c
168
168
169
169
### Http Header
170
170
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.
172
172
173
173
::: 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.
175
175
:::
176
176
177
177
### Get or Post
178
178
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`.
180
180
181
181
On a POST or PUT Requests, it depends on whether you have files attached to a Request.
182
182
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`.
You can specify a custom body content type if necessary. The `contentType` argument is available in all the overloads that add a request body.
221
221
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
-
constjson="{ data: { foo: \"bar\" } }";
228
-
request.AddStringBody(json, ContentType.Json);
229
-
```
230
-
231
222
#### AddJsonBody
232
223
233
224
When you call `AddJsonBody`, it does the following for you:
@@ -237,7 +228,7 @@ When you call `AddJsonBody`, it does the following for you:
237
228
- Sets the internal data type of the request body to `DataType.Json`
238
229
239
230
::: 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.
241
232
:::
242
233
243
234
Here is the example:
@@ -256,12 +247,12 @@ When you call `AddXmlBody`, it does the following for you:
256
247
- Sets the internal data type of the request body to `DataType.Xml`
257
248
258
249
::: 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!
260
251
:::
261
252
262
253
### Query String
263
254
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.
265
256
266
257
Example:
267
258
@@ -280,11 +271,11 @@ You can also specify the query string parameter type explicitly:
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.
284
275
285
276
## Making a call
286
277
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.
288
279
289
280
`RestClient` has a single function for this:
290
281
@@ -331,13 +322,13 @@ Those extensions will throw an exception if the server returns an error, as ther
331
322
332
323
### JSON requests
333
324
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:
0 commit comments