-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#126 - add GET/PATCH /configs API endpoints
- Loading branch information
Showing
9 changed files
with
460 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
lambda-api/src/main/java/com/formkiq/stacks/api/handler/ConfigsRequestHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/** | ||
* MIT License | ||
* | ||
* Copyright (c) 2018 - 2020 FormKiQ | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package com.formkiq.stacks.api.handler; | ||
|
||
import static com.formkiq.aws.services.lambda.ApiResponseStatus.SC_OK; | ||
import java.util.Map; | ||
import com.amazonaws.services.lambda.runtime.LambdaLogger; | ||
import com.formkiq.aws.dynamodb.DbKeys; | ||
import com.formkiq.aws.dynamodb.DynamicObject; | ||
import com.formkiq.aws.services.lambda.ApiAuthorizer; | ||
import com.formkiq.aws.services.lambda.ApiGatewayRequestEvent; | ||
import com.formkiq.aws.services.lambda.ApiGatewayRequestEventUtil; | ||
import com.formkiq.aws.services.lambda.ApiGatewayRequestHandler; | ||
import com.formkiq.aws.services.lambda.ApiMapResponse; | ||
import com.formkiq.aws.services.lambda.ApiRequestHandlerResponse; | ||
import com.formkiq.aws.services.lambda.exceptions.BadException; | ||
import com.formkiq.aws.services.lambda.exceptions.UnauthorizedException; | ||
import com.formkiq.aws.services.lambda.services.ConfigService; | ||
import com.formkiq.module.lambdaservices.AwsServiceCache; | ||
|
||
/** {@link ApiGatewayRequestHandler} for "/configs". */ | ||
public class ConfigsRequestHandler implements ApiGatewayRequestHandler, ApiGatewayRequestEventUtil { | ||
|
||
/** | ||
* constructor. | ||
* | ||
*/ | ||
public ConfigsRequestHandler() {} | ||
|
||
@Override | ||
public void beforeGet(final LambdaLogger logger, final ApiGatewayRequestEvent event, | ||
final ApiAuthorizer authorizer, final AwsServiceCache awsServices) throws Exception { | ||
checkPermissions(authorizer); | ||
} | ||
|
||
@Override | ||
public void beforePatch(final LambdaLogger logger, final ApiGatewayRequestEvent event, | ||
final ApiAuthorizer authorizer, final AwsServiceCache awsServices) throws Exception { | ||
checkPermissions(authorizer); | ||
} | ||
|
||
private void checkPermissions(final ApiAuthorizer authorizer) throws UnauthorizedException { | ||
if (!authorizer.isUserAdmin()) { | ||
throw new UnauthorizedException("user is unauthorized"); | ||
} | ||
} | ||
|
||
@Override | ||
public ApiRequestHandlerResponse get(final LambdaLogger logger, | ||
final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, | ||
final AwsServiceCache awsservice) throws Exception { | ||
|
||
String siteId = authorizer.getSiteId(); | ||
ConfigService configService = awsservice.getExtension(ConfigService.class); | ||
|
||
DynamicObject obj = configService.get(siteId); | ||
obj.remove(DbKeys.PK); | ||
obj.remove(DbKeys.SK); | ||
|
||
return new ApiRequestHandlerResponse(SC_OK, new ApiMapResponse(obj)); | ||
} | ||
|
||
@Override | ||
public String getRequestUrl() { | ||
return "/configs"; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public ApiRequestHandlerResponse patch(final LambdaLogger logger, | ||
final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, | ||
final AwsServiceCache awsservice) throws Exception { | ||
|
||
String siteId = authorizer.getSiteId(); | ||
|
||
Map<String, String> body = fromBodyToObject(logger, event, Map.class); | ||
if (!body.containsKey("key") && !body.containsKey("value")) { | ||
throw new BadException("'key' and 'value' are required"); | ||
} | ||
|
||
String key = body.get("key"); | ||
String value = body.get("value"); | ||
ConfigService configService = awsservice.getExtension(ConfigService.class); | ||
configService.save(siteId, new DynamicObject(Map.of(key, value))); | ||
|
||
return new ApiRequestHandlerResponse(SC_OK, | ||
new ApiMapResponse(Map.of("message", "Config saved"))); | ||
} | ||
} |
Oops, something went wrong.