Skip to content

Commit 71b285f

Browse files
codefriarclaude
andcommitted
feat(rest): add fluent interface to RestLibApiCall
- Added a builder pattern with fluent interface through static create() method - Added convenience methods for all HTTP verbs (usingGet, usingPost, etc.) - Added methods for setting path, query, body, and headers - Added comprehensive tests for the new interface - Updated documentation with examples 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 5885d9a commit 71b285f

5 files changed

Lines changed: 461 additions & 0 deletions

File tree

.idea/git_toolbox_blame.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/libraries/ApexKit_lwc_types.xml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/RestLibApiCall.md

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,173 @@ The query string to be appended to the path
8484

8585
## Methods
8686

87+
### `public static RestLibApiCall create()`
88+
89+
Creates a new instance for a fluent API call.
90+
91+
#### Returns
92+
93+
| Type | Description |
94+
| -------------- | ------------------------------------------------- |
95+
| RestLibApiCall | A new RestLibApiCall instance for fluent chaining |
96+
97+
### `public RestLibApiCall withMethod(HttpVerb method)`
98+
99+
Sets the HTTP method for this API call.
100+
101+
#### Parameters
102+
103+
| Param | Description |
104+
| -------- | -------------------------------------------------- |
105+
| `method` | HttpVerb enum value ie: GET, POST, PUT, PATCH, DEL |
106+
107+
#### Returns
108+
109+
| Type | Description |
110+
| -------------- | --------------------------------- |
111+
| RestLibApiCall | The current instance for chaining |
112+
113+
### `public RestLibApiCall usingGet()`
114+
115+
Convenience method to set HTTP method to GET.
116+
117+
#### Returns
118+
119+
| Type | Description |
120+
| -------------- | --------------------------------- |
121+
| RestLibApiCall | The current instance for chaining |
122+
123+
### `public RestLibApiCall usingPost()`
124+
125+
Convenience method to set HTTP method to POST.
126+
127+
#### Returns
128+
129+
| Type | Description |
130+
| -------------- | --------------------------------- |
131+
| RestLibApiCall | The current instance for chaining |
132+
133+
### `public RestLibApiCall usingPut()`
134+
135+
Convenience method to set HTTP method to PUT.
136+
137+
#### Returns
138+
139+
| Type | Description |
140+
| -------------- | --------------------------------- |
141+
| RestLibApiCall | The current instance for chaining |
142+
143+
### `public RestLibApiCall usingPatch()`
144+
145+
Convenience method to set HTTP method to PATCH.
146+
147+
#### Returns
148+
149+
| Type | Description |
150+
| -------------- | --------------------------------- |
151+
| RestLibApiCall | The current instance for chaining |
152+
153+
### `public RestLibApiCall usingDelete()`
154+
155+
Convenience method to set HTTP method to DELETE.
156+
157+
#### Returns
158+
159+
| Type | Description |
160+
| -------------- | --------------------------------- |
161+
| RestLibApiCall | The current instance for chaining |
162+
163+
### `public RestLibApiCall usingHead()`
164+
165+
Convenience method to set HTTP method to HEAD.
166+
167+
#### Returns
168+
169+
| Type | Description |
170+
| -------------- | --------------------------------- |
171+
| RestLibApiCall | The current instance for chaining |
172+
173+
### `public RestLibApiCall withPath(String path)`
174+
175+
Sets the path for this API call.
176+
177+
#### Parameters
178+
179+
| Param | Description |
180+
| ------ | ---------------------------------- |
181+
| `path` | String of the path to the resource |
182+
183+
#### Returns
184+
185+
| Type | Description |
186+
| -------------- | --------------------------------- |
187+
| RestLibApiCall | The current instance for chaining |
188+
189+
### `public RestLibApiCall withQuery(String query)`
190+
191+
Sets the query parameters for this API call.
192+
193+
#### Parameters
194+
195+
| Param | Description |
196+
| ------- | --------------------------------------------------------- |
197+
| `query` | String of the query parameters to be appended to the path |
198+
199+
#### Returns
200+
201+
| Type | Description |
202+
| -------------- | --------------------------------- |
203+
| RestLibApiCall | The current instance for chaining |
204+
205+
### `public RestLibApiCall withBody(String body)`
206+
207+
Sets the body for this API call.
208+
209+
#### Parameters
210+
211+
| Param | Description |
212+
| ------ | ---------------------------------------------- |
213+
| `body` | String of the body to be sent with the request |
214+
215+
#### Returns
216+
217+
| Type | Description |
218+
| -------------- | --------------------------------- |
219+
| RestLibApiCall | The current instance for chaining |
220+
221+
### `public RestLibApiCall withHeaders(Map<String, String> headers)`
222+
223+
Sets the headers for this API call.
224+
225+
#### Parameters
226+
227+
| Param | Description |
228+
| --------- | -------------------------------------------------------------- |
229+
| `headers` | Map<String, String> of the headers to be sent with the request |
230+
231+
#### Returns
232+
233+
| Type | Description |
234+
| -------------- | --------------------------------- |
235+
| RestLibApiCall | The current instance for chaining |
236+
237+
### `public RestLibApiCall withHeader(String key, String value)`
238+
239+
Adds a single header to the existing headers.
240+
241+
#### Parameters
242+
243+
| Param | Description |
244+
| ------- | ---------------- |
245+
| `key` | The header key |
246+
| `value` | The header value |
247+
248+
#### Returns
249+
250+
| Type | Description |
251+
| -------------- | --------------------------------- |
252+
| RestLibApiCall | The current instance for chaining |
253+
87254
### `private static String ensureStringEndsInSlash(String resource)`
88255

89256
`TESTVISIBLE`
@@ -102,4 +269,40 @@ Ensures that the inputted string ends in a `/` makes callouts more robust.
102269
| ------ | --------------------------------------------------------- |
103270
| String | inputted string with `/` if it didn't already end in one. |
104271

272+
## Usage Examples
273+
274+
### Basic GET Request
275+
276+
```apex
277+
RestLibApiCall apiCall = RestLibApiCall.create()
278+
.usingGet()
279+
.withPath('/accounts')
280+
.withQuery('limit=10');
281+
282+
HttpResponse response = RestClient.makeApiCall('MyNamedCredential', apiCall);
283+
```
284+
285+
### POST with Body and Custom Headers
286+
287+
```apex
288+
RestLibApiCall apiCall = RestLibApiCall.create()
289+
.usingPost()
290+
.withPath('/accounts')
291+
.withBody(JSON.serialize(newAccount))
292+
.withHeader('X-Custom-Header', 'CustomValue');
293+
294+
HttpResponse response = RestClient.makeApiCall('MyNamedCredential', apiCall);
295+
```
296+
297+
### PATCH Request
298+
299+
```apex
300+
RestLibApiCall apiCall = RestLibApiCall.create()
301+
.usingPatch()
302+
.withPath('/accounts/001xx000003DGb2AAG')
303+
.withBody(JSON.serialize(accountUpdates));
304+
305+
HttpResponse response = RestClient.makeApiCall('MyNamedCredential', apiCall);
306+
```
307+
105308
---

force-app/main/default/classes/rest lib/RestLibApiCall.cls

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,136 @@ public with sharing class RestLibApiCall {
5959
// prettier-ignore
6060
private RestLibApiCall() {} // NOPMD
6161

62+
/**
63+
* @description Creates a new instance for a fluent API call
64+
* @return A new RestLibApiCall instance for fluent chaining
65+
*/
66+
public static RestLibApiCall create() {
67+
return new RestLibApiCall();
68+
}
69+
70+
/**
71+
* @description Sets the HTTP method for this API call
72+
* @param method HttpVerb enum value ie: GET, POST, PUT, PATCH, DEL
73+
* @return The current instance for chaining
74+
*/
75+
public RestLibApiCall withMethod(HttpVerb method) {
76+
this.method = method;
77+
if (method == HttpVerb.PATCH) {
78+
// Handle PATCH as POST with custom parameter
79+
this.method = HttpVerb.POST;
80+
if (this.encodedQuery == null) {
81+
this.encodedQuery = '?_HttpMethod=PATCH';
82+
} else {
83+
this.encodedQuery += '?_HttpMethod=PATCH';
84+
}
85+
}
86+
return this;
87+
}
88+
89+
/**
90+
* @description Convenience method to set HTTP method to GET
91+
* @return The current instance for chaining
92+
*/
93+
public RestLibApiCall usingGet() {
94+
return withMethod(HttpVerb.GET);
95+
}
96+
97+
/**
98+
* @description Convenience method to set HTTP method to POST
99+
* @return The current instance for chaining
100+
*/
101+
public RestLibApiCall usingPost() {
102+
return withMethod(HttpVerb.POST);
103+
}
104+
105+
/**
106+
* @description Convenience method to set HTTP method to PUT
107+
* @return The current instance for chaining
108+
*/
109+
public RestLibApiCall usingPut() {
110+
return withMethod(HttpVerb.PUT);
111+
}
112+
113+
/**
114+
* @description Convenience method to set HTTP method to PATCH
115+
* @return The current instance for chaining
116+
*/
117+
public RestLibApiCall usingPatch() {
118+
return withMethod(HttpVerb.PATCH);
119+
}
120+
121+
/**
122+
* @description Convenience method to set HTTP method to DELETE
123+
* @return The current instance for chaining
124+
*/
125+
public RestLibApiCall usingDelete() {
126+
return withMethod(HttpVerb.DEL);
127+
}
128+
129+
/**
130+
* @description Convenience method to set HTTP method to HEAD
131+
* @return The current instance for chaining
132+
*/
133+
public RestLibApiCall usingHead() {
134+
return withMethod(HttpVerb.HEAD);
135+
}
136+
137+
/**
138+
* @description Sets the path for this API call
139+
* @param path String of the path to the resource
140+
* @return The current instance for chaining
141+
*/
142+
public RestLibApiCall withPath(String path) {
143+
this.path = ensureStringEndsInSlash(path);
144+
return this;
145+
}
146+
147+
/**
148+
* @description Sets the query parameters for this API call
149+
* @param query String of the query parameters to be appended to the path
150+
* @return The current instance for chaining
151+
*/
152+
public RestLibApiCall withQuery(String query) {
153+
this.query = query;
154+
this.encodedQuery = EncodingUtil.urlEncode(query, 'UTF-8');
155+
return this;
156+
}
157+
158+
/**
159+
* @description Sets the body for this API call
160+
* @param body String of the body to be sent with the request
161+
* @return The current instance for chaining
162+
*/
163+
public RestLibApiCall withBody(String body) {
164+
this.body = body;
165+
return this;
166+
}
167+
168+
/**
169+
* @description Sets the headers for this API call
170+
* @param headers Map<String, String> of the headers to be sent with the request
171+
* @return The current instance for chaining
172+
*/
173+
public RestLibApiCall withHeaders(Map<String, String> headers) {
174+
this.headers = headers;
175+
return this;
176+
}
177+
178+
/**
179+
* @description Adds a single header to the existing headers
180+
* @param key The header key
181+
* @param value The header value
182+
* @return The current instance for chaining
183+
*/
184+
public RestLibApiCall withHeader(String key, String value) {
185+
if (this.headers == null) {
186+
this.headers = new Map<String, String>();
187+
}
188+
this.headers.put(key, value);
189+
return this;
190+
}
191+
62192
/**
63193
* @description Basic constructor that builds an instance with the necessary information to make a callout.
64194
*

0 commit comments

Comments
 (0)