Skip to content

Commit ebaba73

Browse files
committed
Add the prefix "libak" to classes.
1 parent ca4fa07 commit ebaba73

28 files changed

+258
-258
lines changed

force-app/main/default/classes/ErrorResponseFactory.cls

Lines changed: 0 additions & 28 deletions
This file was deleted.

force-app/main/default/classes/JsonResponse.cls

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,47 @@
11
/**
2-
* The `ErrorResponse` class represents an error REST response. It allows developers to construct
2+
* The `libak_ErrorResponse` class represents an error REST response. It allows developers to construct
33
* responses with custom error messages, status codes, and details.
44
*/
5-
public class ErrorResponse implements RestFramework.IRestResponse {
5+
public class libak_ErrorResponse implements libak_RestFramework.IRestResponse {
66
@TestVisible
77
private Integer statusCode;
88
@TestVisible
99
private String summary;
1010
private String details;
1111

1212
/**
13-
* Constructs a new `ErrorResponse` with the specified status code, summary, and details.
13+
* Constructs a new `libak_ErrorResponse` with the specified status code, summary, and details.
1414
*
1515
* @param statusCode The HTTP status code for the error response.
1616
* @param summary A summary message describing the error.
1717
* @param details Additional details about the error.
1818
*/
19-
public ErrorResponse(Integer statusCode, String summary, String details) {
19+
public libak_ErrorResponse(Integer statusCode, String summary, String details) {
2020
this.statusCode = statusCode;
2121
this.summary = summary;
2222
this.details = details;
2323
}
2424

2525
/**
26-
* Constructs a new `ErrorResponse` with the specified status code and summary,
26+
* Constructs a new `libak_ErrorResponse` with the specified status code and summary,
2727
* based on an exception.
2828
*
2929
* @param status The HTTP status code for the error response.
3030
* @param summary A summary message describing the error.
3131
* @param exc The exception from which to derive error details.
3232
*/
33-
public ErrorResponse(Integer status, String summary, Exception exc) {
33+
public libak_ErrorResponse(Integer status, String summary, Exception exc) {
3434
this(status, summary, exc?.getMessage());
3535
}
3636

3737
/**
38-
* Constructs a new `ErrorResponse` based on an exception. It sets the status code,
38+
* Constructs a new `libak_ErrorResponse` based on an exception. It sets the status code,
3939
* summary, and details based on the exception's information.
4040
*
4141
* @param status The HTTP status code for the error response.
4242
* @param exc The exception from which to derive error details.
4343
*/
44-
public ErrorResponse(Integer status, Exception exc) {
44+
public libak_ErrorResponse(Integer status, Exception exc) {
4545
this(status, exc?.getMessage(), exc);
4646
}
4747

@@ -53,6 +53,6 @@ public class ErrorResponse implements RestFramework.IRestResponse {
5353
public void sendResponse() {
5454
RestContext.response.statusCode = this.statusCode;
5555
RestContext.response.responseBody = BLOB.valueOf(JSON.serialize(this));
56-
RestContext.response.addHeader(RestFramework.HEADER_NAME_CONTENT_TYPE, RestFramework.CONTENT_TYPE_APPLICATION_JSON);
56+
RestContext.response.addHeader(libak_RestFramework.HEADER_NAME_CONTENT_TYPE, libak_RestFramework.CONTENT_TYPE_APPLICATION_JSON);
5757
}
5858
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* The `libak_ErrorResponseFactory` class is responsible for creating error responses based on exceptions.
3+
* It maps exception types to appropriate HTTP status codes and error messages.
4+
*/
5+
public class libak_ErrorResponseFactory implements libak_RestFramework.IErrorResponseFactory {
6+
private Map<String, Integer> httpStatusByErrorType = new Map<String, Integer>{
7+
libak_RestFramework.InvalidUriException.class.getName() => libak_RestFramework.HTTP_CODE_BAD_REQUEST,
8+
libak_RestFramework.NotFoundException.class.getName() => libak_RestFramework.HTTP_CODE_NOT_FOUND,
9+
libak_RestFramework.MethodNotAllowedException.class.getName() => libak_RestFramework.HTTP_CODE_METHOD_NOT_ALLOWED
10+
};
11+
12+
/**
13+
* Creates a new error response for the given exception.
14+
*
15+
* @param exc The exception for which to create an error response.
16+
* @return An error response based on the exception.
17+
*/
18+
public libak_RestFramework.IRestResponse newErrorRestResponse(Exception exc) {
19+
Integer statusCode = this.httpStatusByErrorType.get(exc.getTypeName());
20+
return statusCode != null
21+
? new libak_ErrorResponse(statusCode, exc)
22+
: new libak_ErrorResponse(
23+
libak_RestFramework.HTTP_CODE_INTERNAL_SERVER_ERROR,
24+
libak_RestFramework.ERROR_MESSAGE_INTERNAL_SERVER_ERROR,
25+
exc
26+
);
27+
}
28+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* The `libak_JsonResponse` class represents a successful REST response with JSON data. It extends the `libak_SuccessResponse`
3+
* class and provides convenience methods for handling JSON data.
4+
*/
5+
public class libak_JsonResponse extends libak_SuccessResponse {
6+
7+
/**
8+
* Constructs a new `libak_JsonResponse` with the provided JSON data as a string.
9+
*
10+
* @param data The JSON data to include in the response.
11+
*/
12+
public libak_JsonResponse(String data) {
13+
super(data);
14+
this.addHeader(libak_RestFramework.HEADER_NAME_CONTENT_TYPE, libak_RestFramework.CONTENT_TYPE_APPLICATION_JSON);
15+
}
16+
17+
/**
18+
* Constructs a new `libak_JsonResponse` with the provided JSON data as an object, serialized to a string.
19+
*
20+
* @param data The JSON data to include in the response.
21+
*/
22+
public libak_JsonResponse(Object data) {
23+
super(Blob.valueOf(JSON.serialize(data)));
24+
this.addHeader(libak_RestFramework.HEADER_NAME_CONTENT_TYPE, libak_RestFramework.CONTENT_TYPE_APPLICATION_JSON);
25+
}
26+
27+
/**
28+
* Constructs a new `libak_JsonResponse` with the specified status code and JSON data as a string.
29+
*
30+
* @param statusCode The HTTP status code for the response.
31+
* @param data The JSON data to include in the response.
32+
*/
33+
public libak_JsonResponse(Integer statusCode, String data) {
34+
super(statusCode, data);
35+
this.addHeader(libak_RestFramework.HEADER_NAME_CONTENT_TYPE, libak_RestFramework.CONTENT_TYPE_APPLICATION_JSON);
36+
}
37+
38+
/**
39+
* Constructs a new `libak_JsonResponse` with the specified status code and JSON data as an object, serialized to a string.
40+
*
41+
* @param statusCode The HTTP status code for the response.
42+
* @param data The JSON data to include in the response.
43+
*/
44+
public libak_JsonResponse(Integer statusCode, Object data) {
45+
super(statusCode, Blob.valueOf(JSON.serialize(data)));
46+
this.addHeader(libak_RestFramework.HEADER_NAME_CONTENT_TYPE, libak_RestFramework.CONTENT_TYPE_APPLICATION_JSON);
47+
}
48+
}

force-app/main/default/classes/RestFramework.cls renamed to force-app/main/default/classes/libak_RestFramework.cls

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/**
2-
* The `RestFramework` class provides a structured framework for building RESTful web services in Salesforce Apex.
2+
* The `libak_RestFramework` class provides a structured framework for building RESTful web services in Salesforce Apex.
33
* It includes interfaces for routing, response handling, error management, and logging, along with core components
44
* for processing RESTful requests.
55
*/
6-
public class RestFramework {
6+
public class libak_RestFramework {
77
// Constants for HTTP status codes
88
public static final Integer HTTP_CODE_OK = 200;
99
public static final Integer HTTP_CODE_BAD_REQUEST = 400;
@@ -39,7 +39,7 @@ public class RestFramework {
3939
* @param loggerType The type of the logger to use for logging REST-related information (optional, can be null).
4040
*/
4141
public static void handleRequest(Type routerType, Type loggerType) {
42-
handleRequest(routerType, loggerType, ErrorResponseFactory.class);
42+
handleRequest(routerType, loggerType, libak_ErrorResponseFactory.class);
4343
}
4444

4545
/**
@@ -57,8 +57,8 @@ public class RestFramework {
5757
errorResponseFactory = (IErrorResponseFactory)errorResponseFactoryType?.newInstance();
5858
restLogger = (IRestLogger)loggerType?.newInstance();
5959
restLogger?.initLog(RestContext.request);
60-
RestRouter router = ((RestRouter)routerType.newInstance()).setRoutes();
61-
RestProcessor processor = router
60+
libak_RestRouter router = ((libak_RestRouter)routerType.newInstance()).setRoutes();
61+
libak_RestProcessor processor = router
6262
.newRestProcessor(RestContext.request, errorResponseFactory, restLogger);
6363
response = processor.process();
6464
} catch (Exception exc) {

0 commit comments

Comments
 (0)