Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions demo-app/main/default/classes/CustomerWebServiceDemo.cls
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ global with sharing class CustomerWebServiceDemo {
}

public class CustomersProcessorV1 extends libak_RestProcessor {
protected override libak_RestFramework.IRestResponse handleGet() {
protected override libak_IRestResponse handleGet() {
List<Account> accounts = [
SELECT Id, Name, Phone, BillingStreet, BillingCity, BillingState, BillingPostalCode
FROM Account
Expand All @@ -42,7 +42,7 @@ global with sharing class CustomerWebServiceDemo {
}
}

protected override libak_RestFramework.IRestResponse handlePost() {
protected override libak_IRestResponse handlePost() {
Account newAccount = (Account)JSON.deserialize(this.request.requestBody.toString(), Account.class);
insert newAccount;

Expand All @@ -51,7 +51,7 @@ global with sharing class CustomerWebServiceDemo {
}

public class CustomerProcessorV1 extends libak_RestProcessor {
protected override libak_RestFramework.IRestResponse handleGet() {
protected override libak_IRestResponse handleGet() {
List<Account> accounts = [
SELECT Id, Name, Phone, BillingStreet, BillingCity, BillingState, BillingPostalCode
FROM Account
Expand All @@ -65,7 +65,7 @@ global with sharing class CustomerWebServiceDemo {
}
}

protected override libak_RestFramework.IRestResponse handlePut() {
protected override libak_IRestResponse handlePut() {
String accountId = this.getUriParam('customer_sf_id');
List<Account> existingAccounts = [SELECT Id FROM Account WHERE Id = :accountId];

Expand All @@ -79,7 +79,7 @@ global with sharing class CustomerWebServiceDemo {
return new libak_JsonResponse(updatedAccount);
}

protected override libak_RestFramework.IRestResponse handleDelete() {
protected override libak_IRestResponse handleDelete() {
String accountId = this.getUriParam('customer_sf_id');
List<Account> existingAccounts = [SELECT Id FROM Account WHERE Id = :accountId];

Expand Down
2 changes: 1 addition & 1 deletion force-app/main/default/classes/libak_ErrorResponse.cls
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* The `libak_ErrorResponse` class represents an error REST response. It allows developers to construct
* responses with custom error messages, status codes, and details.
*/
public class libak_ErrorResponse implements libak_RestFramework.IRestResponse {
public class libak_ErrorResponse implements libak_IRestResponse {
@TestVisible
private Integer statusCode;
@TestVisible
Expand Down
4 changes: 2 additions & 2 deletions force-app/main/default/classes/libak_ErrorResponseFactory.cls
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* The `libak_ErrorResponseFactory` class is responsible for creating error responses based on exceptions.
* It maps exception types to appropriate HTTP status codes and error messages.
*/
public class libak_ErrorResponseFactory implements libak_RestFramework.IErrorResponseFactory {
public class libak_ErrorResponseFactory implements libak_IErrorResponseFactory {
private Map<String, Integer> httpStatusByErrorType = new Map<String, Integer>{
libak_RestFramework.InvalidUriException.class.getName() => libak_RestFramework.HTTP_CODE_BAD_REQUEST,
libak_RestFramework.MethodNotAllowedException.class.getName() => libak_RestFramework.HTTP_CODE_METHOD_NOT_ALLOWED
Expand All @@ -14,7 +14,7 @@ public class libak_ErrorResponseFactory implements libak_RestFramework.IErrorRes
* @param exc The exception for which to create an error response.
* @return An error response based on the exception.
*/
public libak_RestFramework.IRestResponse newErrorRestResponse(Exception exc) {
public libak_IRestResponse newErrorRestResponse(Exception exc) {
Integer statusCode = this.httpStatusByErrorType.get(exc.getTypeName());
return statusCode != null
? new libak_ErrorResponse(statusCode, exc)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* The `libak_IErrorResponseFactory` interface defines a method for creating error responses.
*/
public interface libak_IErrorResponseFactory {
libak_IRestResponse newErrorRestResponse(Exception exc);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
9 changes: 9 additions & 0 deletions force-app/main/default/classes/libak_IRestLogger.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* The `libak_IRestLogger` interface defines methods for logging REST-related information.
*/
public interface libak_IRestLogger {
void initLog(RestRequest request);
void addErrorDetails(Exception exc);
void createLog();
}

5 changes: 5 additions & 0 deletions force-app/main/default/classes/libak_IRestLogger.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
6 changes: 6 additions & 0 deletions force-app/main/default/classes/libak_IRestResponse.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* The `libak_IRestResponse` interface defines a method for sending REST responses.
*/
public interface libak_IRestResponse {
void sendResponse();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
33 changes: 5 additions & 28 deletions force-app/main/default/classes/libak_RestFramework.cls
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ public class libak_RestFramework {
* @param errorResponseFactoryType The type of the error response factory to use for creating error responses (optional, can be null).
*/
public static void handleRequest(Type routerType, Type loggerType, Type errorResponseFactoryType) {
IRestResponse response;
IErrorResponseFactory errorResponseFactory;
IRestLogger restLogger;
libak_IRestResponse response;
libak_IErrorResponseFactory errorResponseFactory;
libak_IRestLogger restLogger;
try {
errorResponseFactory = (IErrorResponseFactory)errorResponseFactoryType?.newInstance();
restLogger = (IRestLogger)loggerType?.newInstance();
errorResponseFactory = (libak_IErrorResponseFactory)errorResponseFactoryType?.newInstance();
restLogger = (libak_IRestLogger)loggerType?.newInstance();
restLogger?.initLog(RestContext.request);
libak_RestRouter router = ((libak_RestRouter)routerType.newInstance()).setRoutes();
libak_RestProcessor processor = router
Expand All @@ -70,29 +70,6 @@ public class libak_RestFramework {
}
}

/**
* The `IRestResponse` interface defines a method for sending REST responses.
*/
public interface IRestResponse {
void sendResponse();
}

/**
* The `IErrorResponseFactory` interface defines a method for creating error responses.
*/
public interface IErrorResponseFactory {
IRestResponse newErrorRestResponse(Exception exc);
}

/**
* The `IRestLogger` interface defines methods for logging REST-related information.
*/
public interface IRestLogger {
void initLog(RestRequest request);
void addErrorDetails(Exception exc);
void createLog();
}

/**
* This exception is thrown when an invalid URI is encountered. It represents a client error (HTTP 400 Bad Request).
*/
Expand Down
34 changes: 17 additions & 17 deletions force-app/main/default/classes/libak_RestProcessor.cls
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* The `libak_RestProcessor` class serves as the base class for implementing specific REST processors.
* It includes methods for handling different HTTP methods, parsing URI parameters, and managing errors.
* @return {libak_RestFramework.IRestResponse}
* @return {libak_IRestResponse}
*/
public virtual class libak_RestProcessor {
@TestVisible
Expand All @@ -11,9 +11,9 @@ public virtual class libak_RestProcessor {
@TestVisible
private Map<String, String> uriParamsMap;
@TestVisible
private libak_RestFramework.IErrorResponseFactory errorResponseFactory;
private libak_IErrorResponseFactory errorResponseFactory;
@TestVisible
private libak_RestFramework.IRestLogger restLogger;
private libak_IRestLogger restLogger;

/**
* Processes the incoming REST request by handling different HTTP methods and managing errors.
Expand All @@ -22,10 +22,10 @@ public virtual class libak_RestProcessor {
* to the appropriate handler based on the HTTP method (GET, POST, PUT, PATCH, DELETE). If an unsupported
* HTTP method is received, it throws a libak_RestFramework.MethodNotAllowedException.
*
* @return An instance of libak_RestFramework.IRestResponse representing the response to the REST request.
* @return An instance of libak_IRestResponse representing the response to the REST request.
* @throws libak_RestFramework.MethodNotAllowedException If the incoming HTTP method is not supported for this resource.
*/
public libak_RestFramework.IRestResponse process() {
public libak_IRestResponse process() {
try {
switch on(String) this.request.httpMethod {
when 'GET' {
Expand Down Expand Up @@ -85,7 +85,7 @@ public virtual class libak_RestProcessor {
* @param errorResponseFactory The error response factory to use.
* @return The current `libak_RestProcessor` instance.
*/
public libak_RestProcessor useErrorResponseFactory(libak_RestFramework.IErrorResponseFactory errorResponseFactory) {
public libak_RestProcessor useErrorResponseFactory(libak_IErrorResponseFactory errorResponseFactory) {
this.errorResponseFactory = errorResponseFactory;
return this;
}
Expand All @@ -96,7 +96,7 @@ public virtual class libak_RestProcessor {
* @param restLogger The REST logger to use.
* @return The current `libak_RestProcessor` instance.
*/
public libak_RestProcessor useRestLogger(libak_RestFramework.IRestLogger restLogger) {
public libak_RestProcessor useRestLogger(libak_IRestLogger restLogger) {
this.restLogger = restLogger;
return this;
}
Expand All @@ -110,9 +110,9 @@ public virtual class libak_RestProcessor {
* If this method is called without being overridden in a subclass, it will throw a libak_RestFramework.MethodNotAllowedException
* indicating that the GET method is not supported for this resource.
*
* @return An instance of libak_RestFramework.IRestResponse representing the response to the GET request.
* @return An instance of libak_IRestResponse representing the response to the GET request.
*/
protected virtual libak_RestFramework.IRestResponse handleGet() {
protected virtual libak_IRestResponse handleGet() {
this.throwMethodNotAllowedException();
return null;
}
Expand All @@ -124,9 +124,9 @@ public virtual class libak_RestProcessor {
* If this method is called without being overridden in a subclass, it will throw a libak_RestFramework.MethodNotAllowedException
* indicating that the POST method is not supported for this resource.
*
* @return An instance of libak_RestFramework.IRestResponse representing the response to the POST request.
* @return An instance of libak_IRestResponse representing the response to the POST request.
*/
protected virtual libak_RestFramework.IRestResponse handlePost() {
protected virtual libak_IRestResponse handlePost() {
this.throwMethodNotAllowedException();
return null;
}
Expand All @@ -138,9 +138,9 @@ public virtual class libak_RestProcessor {
* If this method is called without being overridden in a subclass, it will throw a libak_RestFramework.MethodNotAllowedException
* indicating that the PUT method is not supported for this resource.
*
* @return An instance of libak_RestFramework.IRestResponse representing the response to the PUT request.
* @return An instance of libak_IRestResponse representing the response to the PUT request.
*/
protected virtual libak_RestFramework.IRestResponse handlePut() {
protected virtual libak_IRestResponse handlePut() {
this.throwMethodNotAllowedException();
return null;
}
Expand All @@ -152,9 +152,9 @@ public virtual class libak_RestProcessor {
* If this method is called without being overridden in a subclass, it will throw a libak_RestFramework.MethodNotAllowedException
* indicating that the PATCH method is not supported for this resource.
*
* @return An instance of libak_RestFramework.IRestResponse representing the response to the PATCH request.
* @return An instance of libak_IRestResponse representing the response to the PATCH request.
*/
protected virtual libak_RestFramework.IRestResponse handlePatch() {
protected virtual libak_IRestResponse handlePatch() {
this.throwMethodNotAllowedException();
return null;
}
Expand All @@ -166,9 +166,9 @@ public virtual class libak_RestProcessor {
* If this method is called without being overridden in a subclass, it will throw a libak_RestFramework.MethodNotAllowedException
* indicating that the DELETE method is not supported for this resource.
*
* @return An instance of libak_RestFramework.IRestResponse representing the response to the DELETE request.
* @return An instance of libak_IRestResponse representing the response to the DELETE request.
*/
protected virtual libak_RestFramework.IRestResponse handleDelete() {
protected virtual libak_IRestResponse handleDelete() {
this.throwMethodNotAllowedException();
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion force-app/main/default/classes/libak_RestRouter.cls
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public abstract class libak_RestRouter {
* @return A new `libak_RestProcessor` instance configured to handle the specified REST request with custom error handling and logging.
* @throws libak_RestFramework.InvalidUriException If the requested URI does not match any defined routes.
*/
public libak_RestProcessor newRestProcessor(RestRequest request, libak_RestFramework.IErrorResponseFactory errorResponseFactory, libak_RestFramework.IRestLogger restLogger) {
public libak_RestProcessor newRestProcessor(RestRequest request, libak_IErrorResponseFactory errorResponseFactory, libak_IRestLogger restLogger) {
List<String> sortedRoutes = new List<String>(this.routeToRestProcessorType.keySet());
sortedRoutes.sort();
while (sortedRoutes.size() > 0) {
Expand Down
2 changes: 1 addition & 1 deletion force-app/main/default/classes/libak_SuccessResponse.cls
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* The `libak_SuccessResponse` class represents a successful REST response. It allows developers to construct
* responses with custom data, status codes, and headers.
*/
public virtual class libak_SuccessResponse implements libak_RestFramework.IRestResponse {
public virtual class libak_SuccessResponse implements libak_IRestResponse {
protected Map<String, String> headers = new Map<String, String>();
protected Integer statusCode;
protected Blob data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public with sharing class libak_TestErrorResponse {
@IsTest
static void testErrorResponseInstance(){
RestContext.response = new RestResponse();
libak_RestFramework.IRestResponse response = new libak_ErrorResponse(
libak_IRestResponse response = new libak_ErrorResponse(
libak_RestFramework.HTTP_CODE_BAD_REQUEST,
new libak_RestFramework.InvalidUriException('Bad Request')
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ public with sharing class libak_TestErrorResponseFactory {
@IsTest
static void testCustomException(){
RestContext.response = new RestResponse();
libak_RestFramework.IErrorResponseFactory responseFactory = new libak_ErrorResponseFactory();
libak_IErrorResponseFactory responseFactory = new libak_ErrorResponseFactory();
Exception exc = new libak_RestFramework.MethodNotAllowedException('Error message');

Test.startTest();

libak_RestFramework.IRestResponse response = responseFactory.newErrorRestResponse(exc);
libak_IRestResponse response = responseFactory.newErrorRestResponse(exc);
response.sendResponse();

Test.stopTest();
Expand Down Expand Up @@ -37,12 +37,12 @@ public with sharing class libak_TestErrorResponseFactory {
@IsTest
static void testUnexpectedException(){
RestContext.response = new RestResponse();
libak_RestFramework.IErrorResponseFactory responseFactory = new libak_ErrorResponseFactory();
libak_IErrorResponseFactory responseFactory = new libak_ErrorResponseFactory();
Exception exc = new TestUnexpectedException('Error message');

Test.startTest();

libak_RestFramework.IRestResponse response = responseFactory.newErrorRestResponse(exc);
libak_IRestResponse response = responseFactory.newErrorRestResponse(exc);
response.sendResponse();

Test.stopTest();
Expand Down
10 changes: 5 additions & 5 deletions force-app/main/default/classes/tests/libak_TestJsonResponse.cls
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public with sharing class libak_TestJsonResponse {
static void testConstructorStringData(){
RestContext.response = new RestResponse();
String stringResponseData = JSON.serialize(TEST_RESPONSE_DATA);
libak_RestFramework.IRestResponse response = new libak_JsonResponse(stringResponseData);
libak_IRestResponse response = new libak_JsonResponse(stringResponseData);

Test.startTest();

Expand Down Expand Up @@ -41,7 +41,7 @@ public with sharing class libak_TestJsonResponse {
@IsTest
static void testConstructorObjectData(){
RestContext.response = new RestResponse();
libak_RestFramework.IRestResponse response = new libak_JsonResponse(TEST_RESPONSE_DATA);
libak_IRestResponse response = new libak_JsonResponse(TEST_RESPONSE_DATA);

Test.startTest();

Expand Down Expand Up @@ -75,7 +75,7 @@ public with sharing class libak_TestJsonResponse {
static void testConstructorStatusCodeAndStringData(){
RestContext.response = new RestResponse();
String stringResponseData = JSON.serialize(TEST_RESPONSE_DATA);
libak_RestFramework.IRestResponse response = new libak_JsonResponse(libak_RestFramework.HTTP_CODE_BAD_REQUEST, stringResponseData);
libak_IRestResponse response = new libak_JsonResponse(libak_RestFramework.HTTP_CODE_BAD_REQUEST, stringResponseData);

Test.startTest();

Expand Down Expand Up @@ -107,7 +107,7 @@ public with sharing class libak_TestJsonResponse {
@IsTest
static void testConstructorStatusCodeAndObjectData(){
RestContext.response = new RestResponse();
libak_RestFramework.IRestResponse response = new libak_JsonResponse(libak_RestFramework.HTTP_CODE_BAD_REQUEST, TEST_RESPONSE_DATA);
libak_IRestResponse response = new libak_JsonResponse(libak_RestFramework.HTTP_CODE_BAD_REQUEST, TEST_RESPONSE_DATA);

Test.startTest();

Expand Down Expand Up @@ -140,7 +140,7 @@ public with sharing class libak_TestJsonResponse {
@IsTest
static void testSendResponse(){
RestContext.response = new RestResponse();
libak_RestFramework.IRestResponse response = new libak_JsonResponse(TEST_RESPONSE_DATA);
libak_IRestResponse response = new libak_JsonResponse(TEST_RESPONSE_DATA);

Test.startTest();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public with sharing class libak_TestRestProcessor {

Test.startTest();

libak_RestFramework.IRestResponse response = processor.process();
libak_IRestResponse response = processor.process();
response.sendResponse();

Test.stopTest();
Expand Down
Loading
Loading