Skip to content
This repository has been archived by the owner on Jun 27, 2022. It is now read-only.

Commit

Permalink
Fix code style issues; update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
ayltai committed Feb 5, 2021
1 parent 362f4ae commit 35285ce
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 27 deletions.
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ configurations {
repositories {
jcenter()
mavenCentral()
maven { url 'https://dl.bintray.com/mockito/maven' }
}

ext {
retrofitVersion = '2.9.0'
lombokVersion = '1.18.18'
junitVersion = '5.7.0'
junitVersion = '5.7.1'
}

dependencies {
Expand Down Expand Up @@ -65,7 +66,7 @@ dependencies {
testAnnotationProcessor "org.projectlombok:lombok:$lombokVersion"

// Unit testing
testImplementation 'org.mockito:mockito-core:3.7.7'
testImplementation 'org.mockito:mockito-core:3.7.13'
testImplementation 'com.amazonaws:aws-lambda-java-tests:1.0.0'
testImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junitVersion"
Expand Down
11 changes: 1 addition & 10 deletions src/main/java/com/github/ayltai/hknews/handler/ApiHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@
import com.github.ayltai.hknews.Configuration;
import com.github.ayltai.hknews.util.ExpiringCache;

import org.jetbrains.annotations.NotNull;

public abstract class ApiHandler extends Handler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
private static Cache<String, Object> CACHE;

@NotNull
protected Cache<String, Object> getCache() {
if (ApiHandler.CACHE == null) ApiHandler.CACHE = new ExpiringCache<>(Configuration.DEFAULT.getCacheExpirationMillis());

return ApiHandler.CACHE;
}
protected static final Cache<String, Object> CACHE = new ExpiringCache<>(Configuration.DEFAULT.getCacheExpirationMillis());
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.Optional;

import com.amazonaws.cache.Cache;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
Expand All @@ -17,9 +16,7 @@ public final class ItemHandler extends ApiHandler {
@NotNull
@Override
public APIGatewayProxyResponseEvent handleRequest(@NotNull final APIGatewayProxyRequestEvent event, @NotNull final Context context) {
final Cache<String, Object> cache = this.getCache();
final Object response = cache.get(event.getPath());

final Object response = ApiHandler.CACHE.get(event.getPath());
if (response == null) {
final String uid = event.getPathParameters().get("uid");
if (uid == null) return APIGatewayProxyResponseEventFactory.badRequest();
Expand All @@ -28,7 +25,7 @@ public APIGatewayProxyResponseEvent handleRequest(@NotNull final APIGatewayProxy
final Optional<Item> item = new ItemService(new ItemRepository(AmazonDynamoDBFactory.create(), context.getLogger()), context.getLogger()).getItem(uid);
if (item.isEmpty()) return APIGatewayProxyResponseEventFactory.notFound();

cache.put(event.getPath(), item.get());
ApiHandler.CACHE.put(event.getPath(), item.get());

return APIGatewayProxyResponseEventFactory.ok(item.get());
} catch (final InterruptedException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.Map;
import java.util.stream.Collectors;

import com.amazonaws.cache.Cache;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
Expand All @@ -26,9 +25,7 @@ public final class ItemsHandler extends ApiHandler {
@NotNull
@Override
public APIGatewayProxyResponseEvent handleRequest(@NotNull final APIGatewayProxyRequestEvent event, @NotNull final Context context) {
final Cache<String, Object> cache = this.getCache();
final Object response = cache.get(event.getPath());

final Object response = ApiHandler.CACHE.get(event.getPath());
if (response == null) {
final Map<String, String> parameters = event.getPathParameters();
final Map<String, String> queries = event.getQueryStringParameters();
Expand All @@ -49,7 +46,7 @@ public APIGatewayProxyResponseEvent handleRequest(@NotNull final APIGatewayProxy
final boolean isEmpty = items.isEmpty() || pageable.getPageNumber() < 1 || pageable.getPageNumber() > totalPages;
final Page<Item> page = new Page<>(pageable, sort, pageable.getPageSize(), totalPages, items.size(), pagedItems.size(), pageable.getPageNumber() == 1, pageable.getPageNumber() == totalPages, isEmpty, pagedItems);

cache.put(event.getPath(), page);
ApiHandler.CACHE.put(event.getPath(), page);

return APIGatewayProxyResponseEventFactory.ok(page);
} catch (final InterruptedException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.Collection;
import java.util.stream.Collectors;

import com.amazonaws.cache.Cache;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
Expand All @@ -18,13 +17,11 @@ public final class SourceHandler extends ApiHandler {
@NotNull
@Override
public APIGatewayProxyResponseEvent handleRequest(@NotNull final APIGatewayProxyRequestEvent event, @NotNull final Context context) {
final Cache<String, Object> cache = this.getCache();
final Object response = cache.get(event.getPath());

final Object response = ApiHandler.CACHE.get(event.getPath());
if (response == null) {
try {
final Collection<String> sourceNames = new SourceService(new SourceRepository(AmazonDynamoDBFactory.create(), context.getLogger()), context.getLogger()).getSources().stream().map(Source::getSourceName).distinct().collect(Collectors.toList());
cache.put(event.getPath(), sourceNames);
ApiHandler.CACHE.put(event.getPath(), sourceNames);

return APIGatewayProxyResponseEventFactory.ok(sourceNames);
} catch (final InterruptedException e) {
Expand Down

0 comments on commit 35285ce

Please sign in to comment.