Skip to content

Commit c8f4d64

Browse files
committed
Add a demo app to provide easy way to test the libak salesforce rest api framework.
1 parent 79d2062 commit c8f4d64

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
@RestResource(UrlMapping='/*/customers/*')
2+
global with sharing class CustomerWebServiceDemo {
3+
@HttpGet
4+
global static void doGet() {
5+
libak_RestFramework.handleRequest(CustomerRestRouter.class);
6+
}
7+
@HttpPost
8+
global static void doPost() {
9+
libak_RestFramework.handleRequest(CustomerRestRouter.class);
10+
}
11+
@HttpPut
12+
global static void doPut() {
13+
libak_RestFramework.handleRequest(CustomerRestRouter.class);
14+
}
15+
@HttpDelete
16+
global static void doDelete() {
17+
libak_RestFramework.handleRequest(CustomerRestRouter.class);
18+
}
19+
20+
public class CustomerRestRouter extends libak_RestRouter {
21+
override public libak_RestRouter setRoutes() {
22+
this.routeToRestProcessorType = new Map<String, Type>{
23+
'/v1/customers' => CustomersProcessorV1.class,
24+
'/v1/customers/:customer_sf_id' => CustomerProcessorV1.class
25+
};
26+
return this;
27+
}
28+
}
29+
30+
public class CustomersProcessorV1 extends libak_RestProcessor {
31+
protected override libak_RestFramework.IRestResponse handleGet() {
32+
List<Account> accounts = [
33+
SELECT Id, Name, Phone, BillingStreet, BillingCity, BillingState, BillingPostalCode
34+
FROM Account
35+
LIMIT 100
36+
];
37+
38+
if (accounts.isEmpty()) {
39+
return new libak_ErrorResponse(404, 'Accounts are not found', '');
40+
} else {
41+
return new libak_JsonResponse(accounts);
42+
}
43+
}
44+
45+
protected override libak_RestFramework.IRestResponse handlePost() {
46+
Account newAccount = (Account)JSON.deserialize(this.request.requestBody.toString(), Account.class);
47+
insert newAccount;
48+
49+
return new libak_JsonResponse(newAccount);
50+
}
51+
}
52+
53+
public class CustomerProcessorV1 extends libak_RestProcessor {
54+
protected override libak_RestFramework.IRestResponse handleGet() {
55+
List<Account> accounts = [
56+
SELECT Id, Name, Phone, BillingStreet, BillingCity, BillingState, BillingPostalCode
57+
FROM Account
58+
WHERE Id = :this.getUriParam('customer_sf_id')
59+
];
60+
61+
if (accounts.isEmpty()) {
62+
return new libak_ErrorResponse(404, 'Accounts are not found', '');
63+
} else {
64+
return new libak_JsonResponse(accounts.get(0));
65+
}
66+
}
67+
68+
protected override libak_RestFramework.IRestResponse handlePut() {
69+
String accountId = this.getUriParam('customer_sf_id');
70+
List<Account> existingAccounts = [SELECT Id FROM Account WHERE Id = :accountId];
71+
72+
if (existingAccounts.isEmpty()) {
73+
return new libak_ErrorResponse(404, 'Account are not found', '');
74+
}
75+
76+
Account updatedAccount = (Account)JSON.deserialize(this.request.requestBody.toString(), Account.class);
77+
updatedAccount.Id = accountId;
78+
update updatedAccount;
79+
return new libak_JsonResponse(updatedAccount);
80+
}
81+
82+
protected override libak_RestFramework.IRestResponse handleDelete() {
83+
String accountId = this.getUriParam('customer_sf_id');
84+
List<Account> existingAccounts = [SELECT Id FROM Account WHERE Id = :accountId];
85+
86+
if (existingAccounts.isEmpty()) {
87+
return new libak_ErrorResponse(404, 'Account are not found', '');
88+
}
89+
90+
delete existingAccounts.get(0);
91+
return new libak_SuccessResponse('Account deleted successfully');
92+
}
93+
}
94+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>62.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ConnectedApp xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<contactEmail>[email protected]</contactEmail>
4+
<label>libak salesforce rest framework</label>
5+
<oauthConfig>
6+
<callbackUrl>https://login.salesforce.com</callbackUrl>
7+
<isAdminApproved>false</isAdminApproved>
8+
<isClientCredentialEnabled>false</isClientCredentialEnabled>
9+
<isCodeCredentialEnabled>false</isCodeCredentialEnabled>
10+
<isCodeCredentialPostOnly>false</isCodeCredentialPostOnly>
11+
<isConsumerSecretOptional>false</isConsumerSecretOptional>
12+
<isIntrospectAllTokens>false</isIntrospectAllTokens>
13+
<isNamedUserJwtEnabled>false</isNamedUserJwtEnabled>
14+
<isPkceRequired>true</isPkceRequired>
15+
<isSecretRequiredForRefreshToken>true</isSecretRequiredForRefreshToken>
16+
<scopes>Full</scopes>
17+
</oauthConfig>
18+
<oauthPolicy>
19+
<ipRelaxation>ENFORCE</ipRelaxation>
20+
<refreshTokenPolicy>infinite</refreshTokenPolicy>
21+
</oauthPolicy>
22+
</ConnectedApp>

0 commit comments

Comments
 (0)