diff --git a/demo-app/main/default/classes/CustomerWebServiceDemo.cls b/demo-app/main/default/classes/CustomerWebServiceDemo.cls index c340729..7de60b7 100644 --- a/demo-app/main/default/classes/CustomerWebServiceDemo.cls +++ b/demo-app/main/default/classes/CustomerWebServiceDemo.cls @@ -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 accounts = [ SELECT Id, Name, Phone, BillingStreet, BillingCity, BillingState, BillingPostalCode FROM Account @@ -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; @@ -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 accounts = [ SELECT Id, Name, Phone, BillingStreet, BillingCity, BillingState, BillingPostalCode FROM Account @@ -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 existingAccounts = [SELECT Id FROM Account WHERE Id = :accountId]; @@ -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 existingAccounts = [SELECT Id FROM Account WHERE Id = :accountId]; diff --git a/force-app/main/default/classes/libak_ErrorResponse.cls b/force-app/main/default/classes/libak_ErrorResponse.cls index 2cdd757..7b24337 100644 --- a/force-app/main/default/classes/libak_ErrorResponse.cls +++ b/force-app/main/default/classes/libak_ErrorResponse.cls @@ -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 diff --git a/force-app/main/default/classes/libak_ErrorResponseFactory.cls b/force-app/main/default/classes/libak_ErrorResponseFactory.cls index 4ce5fc0..c38a557 100644 --- a/force-app/main/default/classes/libak_ErrorResponseFactory.cls +++ b/force-app/main/default/classes/libak_ErrorResponseFactory.cls @@ -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 httpStatusByErrorType = new Map{ libak_RestFramework.InvalidUriException.class.getName() => libak_RestFramework.HTTP_CODE_BAD_REQUEST, libak_RestFramework.MethodNotAllowedException.class.getName() => libak_RestFramework.HTTP_CODE_METHOD_NOT_ALLOWED @@ -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) diff --git a/force-app/main/default/classes/libak_IErrorResponseFactory.cls b/force-app/main/default/classes/libak_IErrorResponseFactory.cls new file mode 100644 index 0000000..0f86636 --- /dev/null +++ b/force-app/main/default/classes/libak_IErrorResponseFactory.cls @@ -0,0 +1,6 @@ +/** + * The `libak_IErrorResponseFactory` interface defines a method for creating error responses. + */ +public interface libak_IErrorResponseFactory { + libak_IRestResponse newErrorRestResponse(Exception exc); +} \ No newline at end of file diff --git a/force-app/main/default/classes/libak_IErrorResponseFactory.cls-meta.xml b/force-app/main/default/classes/libak_IErrorResponseFactory.cls-meta.xml new file mode 100644 index 0000000..998805a --- /dev/null +++ b/force-app/main/default/classes/libak_IErrorResponseFactory.cls-meta.xml @@ -0,0 +1,5 @@ + + + 62.0 + Active + diff --git a/force-app/main/default/classes/libak_IRestLogger.cls b/force-app/main/default/classes/libak_IRestLogger.cls new file mode 100644 index 0000000..a495807 --- /dev/null +++ b/force-app/main/default/classes/libak_IRestLogger.cls @@ -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(); +} + diff --git a/force-app/main/default/classes/libak_IRestLogger.cls-meta.xml b/force-app/main/default/classes/libak_IRestLogger.cls-meta.xml new file mode 100644 index 0000000..998805a --- /dev/null +++ b/force-app/main/default/classes/libak_IRestLogger.cls-meta.xml @@ -0,0 +1,5 @@ + + + 62.0 + Active + diff --git a/force-app/main/default/classes/libak_IRestResponse.cls b/force-app/main/default/classes/libak_IRestResponse.cls new file mode 100644 index 0000000..b47a735 --- /dev/null +++ b/force-app/main/default/classes/libak_IRestResponse.cls @@ -0,0 +1,6 @@ +/** + * The `libak_IRestResponse` interface defines a method for sending REST responses. + */ +public interface libak_IRestResponse { + void sendResponse(); +} diff --git a/force-app/main/default/classes/libak_IRestResponse.cls-meta.xml b/force-app/main/default/classes/libak_IRestResponse.cls-meta.xml new file mode 100644 index 0000000..998805a --- /dev/null +++ b/force-app/main/default/classes/libak_IRestResponse.cls-meta.xml @@ -0,0 +1,5 @@ + + + 62.0 + Active + diff --git a/force-app/main/default/classes/libak_RestFramework.cls b/force-app/main/default/classes/libak_RestFramework.cls index c938df7..9def73d 100644 --- a/force-app/main/default/classes/libak_RestFramework.cls +++ b/force-app/main/default/classes/libak_RestFramework.cls @@ -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 @@ -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). */ diff --git a/force-app/main/default/classes/libak_RestProcessor.cls b/force-app/main/default/classes/libak_RestProcessor.cls index a1113f0..dc94f4e 100644 --- a/force-app/main/default/classes/libak_RestProcessor.cls +++ b/force-app/main/default/classes/libak_RestProcessor.cls @@ -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 @@ -11,9 +11,9 @@ public virtual class libak_RestProcessor { @TestVisible private Map 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. @@ -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' { @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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; } diff --git a/force-app/main/default/classes/libak_RestRouter.cls b/force-app/main/default/classes/libak_RestRouter.cls index 92830e5..15aeffc 100644 --- a/force-app/main/default/classes/libak_RestRouter.cls +++ b/force-app/main/default/classes/libak_RestRouter.cls @@ -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 sortedRoutes = new List(this.routeToRestProcessorType.keySet()); sortedRoutes.sort(); while (sortedRoutes.size() > 0) { diff --git a/force-app/main/default/classes/libak_SuccessResponse.cls b/force-app/main/default/classes/libak_SuccessResponse.cls index 5b4b338..be8cdae 100644 --- a/force-app/main/default/classes/libak_SuccessResponse.cls +++ b/force-app/main/default/classes/libak_SuccessResponse.cls @@ -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 headers = new Map(); protected Integer statusCode; protected Blob data; diff --git a/force-app/main/default/classes/tests/libak_TestErrorResponse.cls b/force-app/main/default/classes/tests/libak_TestErrorResponse.cls index b5b1484..cd6f88b 100644 --- a/force-app/main/default/classes/tests/libak_TestErrorResponse.cls +++ b/force-app/main/default/classes/tests/libak_TestErrorResponse.cls @@ -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') ); diff --git a/force-app/main/default/classes/tests/libak_TestErrorResponseFactory.cls b/force-app/main/default/classes/tests/libak_TestErrorResponseFactory.cls index 77b0ba9..0b1f6fb 100644 --- a/force-app/main/default/classes/tests/libak_TestErrorResponseFactory.cls +++ b/force-app/main/default/classes/tests/libak_TestErrorResponseFactory.cls @@ -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(); @@ -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(); diff --git a/force-app/main/default/classes/tests/libak_TestJsonResponse.cls b/force-app/main/default/classes/tests/libak_TestJsonResponse.cls index fa8eeb4..9ad9ce8 100644 --- a/force-app/main/default/classes/tests/libak_TestJsonResponse.cls +++ b/force-app/main/default/classes/tests/libak_TestJsonResponse.cls @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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(); diff --git a/force-app/main/default/classes/tests/libak_TestRestProcessor.cls b/force-app/main/default/classes/tests/libak_TestRestProcessor.cls index 3752bba..e2443ff 100644 --- a/force-app/main/default/classes/tests/libak_TestRestProcessor.cls +++ b/force-app/main/default/classes/tests/libak_TestRestProcessor.cls @@ -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(); diff --git a/force-app/main/default/classes/tests/libak_TestRestRouter.cls b/force-app/main/default/classes/tests/libak_TestRestRouter.cls index 02fc45c..dd99998 100644 --- a/force-app/main/default/classes/tests/libak_TestRestRouter.cls +++ b/force-app/main/default/classes/tests/libak_TestRestRouter.cls @@ -80,14 +80,14 @@ public with sharing class libak_TestRestRouter { } } - private class TestRestLogger implements libak_RestFramework.IRestLogger { + private class TestRestLogger implements libak_IRestLogger { public void initLog(RestRequest request) {} public void addErrorDetails(Exception exc) {} public void createLog() {} } - private class libak_TestErrorResponseFactory implements libak_RestFramework.IErrorResponseFactory { - public libak_RestFramework.IRestResponse newErrorRestResponse(Exception exc) { + private class libak_TestErrorResponseFactory implements libak_IErrorResponseFactory { + public libak_IRestResponse newErrorRestResponse(Exception exc) { return null; } }