Skip to content

Commit d4428c1

Browse files
authored
refactor!: refactored rest.li client interfaces (#13)
- updated rest.li client to use resourcePath with path key placeholders, which is easier to use for sub-resources - updated batchFinder method to explicitly require a finderCriteria argument
1 parent 3c14403 commit d4428c1

13 files changed

+484
-321
lines changed

.release-it.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"after:bump": "npx auto-changelog -p"
1111
},
1212
"npm": {
13-
"publish": true
13+
"publish": false
1414
},
1515
"plugins": {
1616
"@release-it/conventional-changelog": {

README.md

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ const { RestliClient } = require('linkedin-api-client');
8585
const restliClient = new RestliClient();
8686

8787
restliClient.get({
88-
resource: '/me',
88+
resourcePath: '/me',
8989
accessToken: <THREE_LEGGED_ACCESS_TOKEN>
9090
}).then(response => {
9191
const profile = response.data;
@@ -104,7 +104,7 @@ const { RestliClient } = require('linkedin-api-client');
104104
const restliClient = new RestliClient();
105105

106106
restliClient.finder({
107-
resource: '/adAccounts',
107+
resourcePath: '/adAccounts',
108108
finderName: 'search',
109109
queryParams: {
110110
search: {
@@ -161,7 +161,8 @@ All methods of the API client require passing in a request options object, all o
161161

162162
| Parameter | Type | Required? | Description |
163163
|---|---|---|---|
164-
| `BaseRequestOptions.resource` | String | Yes | The API resource name, which should begin with a forward slash (e.g. "/adAccounts") |
164+
| `BaseRequestOptions.resourcePath` | String | Yes | <p>The resource path after the base URL, beginning with a forward slash. If the path contains keys, add curly-brace placeholders for the keys and specify the path key-value map in the `pathKeys` argument.</p><p>Examples:</p><ul><li>`resourcePath: "/me"`</li><li>`resourcePath: "/adAccounts/{id}"`</li><li>`resourcePath: "/socialActions/{actionUrn}/comments/{commentId}"`</li><li>`resourcePath: "/campaignConversions/{key}`</li></ul> |
165+
| `BaseRequestOptions.pathKeys` | Object | No | <p>If there are path keys that are part of the `resourcePath` argument, the key placeholders must be specified in the provided `pathKeys` map. The path key values can be strings, numbers, or objects, and these will be properly encoded.</p><p>Examples:</p><ul><li>`pathKeys: {"id": 123"}`</li><li>`pathKeys: {"actionUrn":"urn:li:share:123","commentId":987`}</li><li>`pathKeys: {"key": {"campaign": "urn:li:sponsoredCampaign:123", "conversion": "urn:lla:llaPartnerConversion:456"}}`</li></ul> |
165166
| `BaseRequestOptions.queryParams` | Object | No | A map of query parameters. The parameter keys and values will be correctly encoded by this method, so these should not be encoded. |
166167
| `BaseRequestOptions.accessToken` | String | Yes | The access token that should provide the application access to the specified API |
167168
| `BaseRequestOptions.versionString` | String | No | An optional version string of the format "YYYYMM" or "YYYYMM.RR". If specified, the version header will be passed and the request will use the versioned APIs base URL |
@@ -180,7 +181,6 @@ Makes a Rest.li GET request to fetch the specified entity on a resource. This me
180181
| Parameter | Type | Required? | Description |
181182
|---|---|---|---|
182183
| `params` | Object extends [BaseRequestOptions](#base-request-options) | Yes | Standard request options |
183-
| `params.id` | String \|\| Number \|\| Object | No | The id or key of the entity to fetch. For simple resources, this is not specified. |
184184

185185
**Resolved Response Object:**
186186

@@ -192,8 +192,10 @@ Makes a Rest.li GET request to fetch the specified entity on a resource. This me
192192
**Example:**
193193
```js
194194
restliClient.get({
195-
resource: '/adAccounts',
196-
id: 123,
195+
resourcePath: '/adAccounts/{id}',
196+
pathKeys: {
197+
id: 123
198+
},
197199
queryParams: {
198200
fields: 'id,name'
199201
},
@@ -227,7 +229,7 @@ Makes a Rest.li BATCH_GET request to fetch multiple entities on a resource. This
227229
**Example:**
228230
```js
229231
restliClient.batchGet({
230-
resource: '/adCampaignGroups',
232+
resourcePath: '/adCampaignGroups',
231233
id: [123, 456, 789],
232234
accessToken: MY_ACCESS_TOKEN,
233235
versionString: '202210'
@@ -257,7 +259,7 @@ Makes a Rest.li GET_ALL request to fetch all entities on a resource.
257259
**Example:**
258260
```js
259261
restliClient.getAll({
260-
resource: '/fieldsOfStudy',
262+
resourcePath: '/fieldsOfStudy',
261263
queryParams: {
262264
start: 0,
263265
count: 15
@@ -292,7 +294,7 @@ Makes a Rest.li FINDER request to find entities by some specified criteria.
292294
**Example:**
293295
```js
294296
restliClient.finder({
295-
resource: '/adAccounts',
297+
resourcePath: '/adAccounts',
296298
finderName: 'search'
297299
queryParams: {
298300
search: {
@@ -322,7 +324,8 @@ Makes a Rest.li BATCH_FINDER request to find entities by multiple sets of criter
322324
| Parameter | Type | Required? | Description |
323325
|---|---|---|---|
324326
| `params` | Object extends [BaseRequestOptions](#base-request-options) | Yes | Standard request options |
325-
| `params.batchFinderName` | String | Yes | The Rest.li batch finder name. This will be included in the request query parameters. |
327+
| `params.finderName` | String | Yes | The Rest.li batch finder name. This will be included in the request query parameters. |
328+
| `params.finderCriteria` | Object | Yes | An object with `name` and `value` properties, representing the required batch finder criteria information. `name` should be the batch finder criteria parameter name, and `value` should be a list of finder param objects. The batch finder results are correspondingly ordered according to this list. The batch finder criteria will be encoded and added to the request query parameters. |
326329

327330
**Resolved Response Object:**
328331

@@ -340,9 +343,10 @@ Makes a Rest.li BATCH_FINDER request to find entities by multiple sets of criter
340343
```js
341344
restliClient.batchFinder({
342345
resource: '/organizationAuthorizations',
343-
batchFinderName: 'authorizationActionsAndImpersonator'
344-
queryParams: {
345-
authorizationActions: [
346+
finderName: 'authorizationActionsAndImpersonator'
347+
finderCriteria: {
348+
name: 'authorizationActions',
349+
value: [
346350
{
347351
'OrganizationRoleAuthorizationAction': {
348352
actionType: 'ADMINISTRATOR_READ'
@@ -383,7 +387,7 @@ Makes a Rest.li CREATE request to create a new entity on the resource.
383387
**Example:**
384388
```js
385389
restliClient.create({
386-
resource: '/adAccountsV2',
390+
resourcePath: '/adAccountsV2',
387391
entity: {
388392
name: 'Test Ad Account',
389393
type: 'BUSINESS',
@@ -419,7 +423,7 @@ Makes a Rest.li BATCH_CREATE request to create multiple entities in a single cal
419423
**Example:**
420424
```js
421425
restliClient.batchCreate({
422-
resource: '/adCampaignGroups',
426+
resourcePath: '/adCampaignGroups',
423427
entities: [
424428
{
425429
account: 'urn:li:sponsoredAccount:111',
@@ -448,7 +452,6 @@ Makes a Rest.li UPDATE request to update an entity (overwriting the entire entit
448452
| Parameter | Type | Required? | Description |
449453
|---|---|---|---|
450454
| `params` | Object extends [BaseRequestOptions](#base-request-options) | Yes | Standard request options |
451-
| `params.id` | String \|\| Number \|\| Object | Yes | The id or key of the entity to update. For simple resources, this is not specified. |
452455
| `params.entity` | Object | Yes | The value of the entity with updated values. |
453456

454457
**Resolved Response Object:**
@@ -460,10 +463,12 @@ Makes a Rest.li UPDATE request to update an entity (overwriting the entire entit
460463
**Example:**
461464
```js
462465
restliClient.update({
463-
resource: '/adAccountUsers',
464-
id: {
465-
account: 'urn:li:sponsoredAccount:123',
466-
user: 'urn:li:person:foobar'
466+
resourcePath: '/adAccountUsers/{key}',
467+
pathKeys: {
468+
key: {
469+
account: 'urn:li:sponsoredAccount:123',
470+
user: 'urn:li:person:foobar'
471+
}
467472
},
468473
entity: {
469474
account: 'urn:li:sponsoredAccount:123',
@@ -498,7 +503,7 @@ Makes a Rest.li BATCH_UPDATE request to update multiple entities in a single cal
498503
**Example:**
499504
```js
500505
restliClient.batchUpdate({
501-
resource: '/campaignConversions',
506+
resourcePath: '/campaignConversions',
502507
ids: [
503508
{ campaign: 'urn:li:sponsoredCampaign:123', conversion: 'urn:lla:llaPartnerConversion:456' },
504509
{ campaign: 'urn:li:sponsoredCampaign:123', conversion: 'urn:lla:llaPartnerConversion:789' }
@@ -524,7 +529,6 @@ When an entity has nested fields that can be modified, passing in the original a
524529
| Parameter | Type | Required? | Description |
525530
|---|---|---|---|
526531
| `params` | Object extends [BaseRequestOptions](#base-request-options) | Yes | Standard request options |
527-
| `params.id` | String \|\| Number \|\| Object | No | The id or key of the entity to update. For simple resources, this is not specified. |
528532
| `params.patchSetObject` | Object | No | The value of the entity with only the modified fields present. If specified, this will be directly sent as the patch object. |
529533
| `params.originalEntity` | Object | No | The value of the original entity. If specified and `patchSetObject` is not provided, this will be used in conjunction with `modifiedEntity` to compute the patch object. |
530534
| `params.modifiedEntity` | Object | No | The value of the modified entity. If specified and `patchSetObject` is not provided, this will be used in conjunction with `originalEntity` to compute the patch object. |
@@ -538,8 +542,10 @@ When an entity has nested fields that can be modified, passing in the original a
538542
**Example:**
539543
```js
540544
restliClient.partialUpdate({
541-
resource: '/adAccounts',
542-
id: 123,
545+
resourcePath: '/adAccounts/{id}',
546+
pathKeys: {
547+
id: 123
548+
},
543549
patchSetObject: {
544550
name: 'TestAdAccountModified',
545551
reference: 'urn:li:organization:456'
@@ -576,7 +582,7 @@ Makes a Rest.li BATCH_PARTIAL_UPDATE request to partially update multiple entite
576582
**Example:**
577583
```js
578584
restliClient.batchPartialUpdate({
579-
resource: '/adCampaignGroups',
585+
resourcePath: '/adCampaignGroups',
580586
id: [123, 456],
581587
patchSetObjects: [
582588
{ status: 'ACTIVE' },
@@ -603,7 +609,6 @@ Makes a Rest.li DELETE request to delete an entity.
603609
| Parameter | Type | Required? | Description |
604610
|---|---|---|---|
605611
| `params` | Object extends [BaseRequestOptions](#base-request-options) | Yes | Standard request options |
606-
| `params.id` | String \|\| Number \|\| Object | No | The id or key of the entity to delete. For simple resources, this is not specified. |
607612

608613
**Resolved Response Object:**
609614

@@ -614,8 +619,10 @@ Makes a Rest.li DELETE request to delete an entity.
614619
**Example:**
615620
```js
616621
restliClient.delete({
617-
resource: '/adAccounts',
618-
id: 123,
622+
resourcePath: '/adAccounts/{id}',
623+
pathKeys: {
624+
id: 123
625+
},
619626
versionString: '202210',
620627
accessToken: MY_ACCESS_TOKEN
621628
}).then(response => {
@@ -645,7 +652,7 @@ Makes a Rest.li BATCH_DELETE request to delete multiple entities at once.
645652
**Example:**
646653
```js
647654
restliClient.batchDelete({
648-
resource: '/adAccounts',
655+
resourcePath: '/adAccounts',
649656
ids: [123, 456],
650657
versionString: '202210',
651658
accessToken: MY_ACCESS_TOKEN
@@ -676,7 +683,7 @@ Makes a Rest.li ACTION request to perform an action on a specified resource.
676683
**Example:**
677684
```js
678685
restliClient.action({
679-
resource: '/testResource',
686+
resourcePath: '/testResource',
680687
actionName: 'doSomething',
681688
accessToken: MY_ACCESS_TOKEN
682689
}).then(response => {

examples/batch-get-campaign-groups-query-tunneling.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,24 @@ async function main(): Promise<void> {
2525
* Randomly generate campaign group ids and send in a BATCH_GET request (we expect all 404 responses).
2626
* The client should automatically do query tunneling and perform a POST request.
2727
*/
28-
const campaignGroupIds = Array.from({ length: NUMBER_OF_ENTITIES }).map(_ => Math.floor(Math.random() * 99999999999999));
28+
const campaignGroupIds = Array.from({ length: NUMBER_OF_ENTITIES }).map((_) =>
29+
Math.floor(Math.random() * 99999999999999)
30+
);
2931
const batchGetResponse = await restliClient.batchGet({
30-
resource: AD_CAMPAIGN_GROUPS_RESOURCE,
32+
resourcePath: AD_CAMPAIGN_GROUPS_RESOURCE,
3133
ids: campaignGroupIds,
3234
accessToken,
3335
versionString: MDP_VERSION
3436
});
35-
console.log(`Successfully made a BATCH_GET request on /adCampaignGroups. HTTP Method: ${batchGetResponse.config.method}]`);
37+
console.log(
38+
`Successfully made a BATCH_GET request on /adCampaignGroups. HTTP Method: ${batchGetResponse.config.method}]`
39+
);
3640
}
3741

38-
main().then(() => {
39-
console.log('Completed');
40-
}).catch((error) => {
41-
console.log(`Error encountered: ${error.message}`);
42-
});
42+
main()
43+
.then(() => {
44+
console.log('Completed');
45+
})
46+
.catch((error) => {
47+
console.log(`Error encountered: ${error.message}`);
48+
});

examples/crud-ad-accounts.ts

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ dotenv.config();
1111

1212
const MDP_VERSION = '202212';
1313
const AD_ACCOUNTS_RESOURCE = '/adAccounts';
14+
const AD_ACCOUNTS_ENTITY_RESOURCE = '/adAccounts/{id}';
1415

1516
async function main(): Promise<void> {
1617
const restliClient = new RestliClient();
@@ -21,7 +22,7 @@ async function main(): Promise<void> {
2122
* Create a test ad account
2223
*/
2324
const createResponse = await restliClient.create({
24-
resource: AD_ACCOUNTS_RESOURCE,
25+
resourcePath: AD_ACCOUNTS_RESOURCE,
2526
entity: {
2627
name: 'Test Ad Account',
2728
reference: 'urn:li:organization:123',
@@ -33,38 +34,42 @@ async function main(): Promise<void> {
3334
versionString: MDP_VERSION
3435
});
3536
const adAccountId = createResponse.createdEntityId as string;
36-
console.log(`Successfully created ad account: ${adAccountId}`);
37+
console.log(`Successfully created ad account: ${adAccountId}\n`);
3738

3839
/**
3940
* Get the created ad account
4041
*/
4142
const getResponse = await restliClient.get({
42-
resource: AD_ACCOUNTS_RESOURCE,
43-
id: adAccountId,
43+
resourcePath: AD_ACCOUNTS_ENTITY_RESOURCE,
44+
pathKeys: {
45+
id: adAccountId
46+
},
4447
accessToken,
4548
versionString: MDP_VERSION
4649
});
47-
console.log(`Successfully fetched ad acccount: ${JSON.stringify(getResponse.data, null, 2)}`);
50+
console.log(`Successfully fetched ad acccount: ${JSON.stringify(getResponse.data, null, 2)}\n`);
4851

4952
/**
5053
* Partial update on ad account
5154
*/
5255
await restliClient.partialUpdate({
53-
resource: AD_ACCOUNTS_RESOURCE,
54-
id: adAccountId,
56+
resourcePath: AD_ACCOUNTS_ENTITY_RESOURCE,
57+
pathKeys: {
58+
id: adAccountId
59+
},
5560
patchSetObject: {
5661
name: 'Modified Test Ad Account'
5762
},
5863
accessToken,
5964
versionString: MDP_VERSION
6065
});
61-
console.log('Successfully did partial update of ad account');
66+
console.log('Successfully did partial update of ad account\n');
6267

6368
/**
6469
* Find all ad accounts according to a specified search criteria
6570
*/
6671
const finderResponse = await restliClient.finder({
67-
resource: AD_ACCOUNTS_RESOURCE,
72+
resourcePath: AD_ACCOUNTS_RESOURCE,
6873
finderName: 'search',
6974
queryParams: {
7075
search: {
@@ -80,22 +85,28 @@ async function main(): Promise<void> {
8085
accessToken,
8186
versionString: MDP_VERSION
8287
});
83-
console.log(`Successfully searched ad accounts: ${JSON.stringify(finderResponse.data.elements, null, 2)}`);
88+
console.log(
89+
`Successfully searched ad accounts: ${JSON.stringify(finderResponse.data.elements, null, 2)}\n`
90+
);
8491

8592
/**
8693
* Delete ad account
8794
*/
8895
await restliClient.delete({
89-
resource: AD_ACCOUNTS_RESOURCE,
90-
id: adAccountId,
96+
resourcePath: AD_ACCOUNTS_ENTITY_RESOURCE,
97+
pathKeys: {
98+
id: adAccountId
99+
},
91100
accessToken,
92101
versionString: MDP_VERSION
93102
});
94-
console.log('Successfully deleted ad account');
103+
console.log('Successfully deleted ad account\n');
95104
}
96105

97-
main().then(() => {
98-
console.log('Completed');
99-
}).catch((error) => {
100-
console.log(`Error encountered: ${error.message}`);
101-
});
106+
main()
107+
.then(() => {
108+
console.log('Completed');
109+
})
110+
.catch((error) => {
111+
console.log(`Error encountered: ${error.message}`);
112+
});

0 commit comments

Comments
 (0)