Skip to content

Commit

Permalink
Fixed tests and removed controller advice
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Villavicencio authored and Daniel Villavicencio committed Mar 13, 2024
1 parent 631c949 commit edcc0c7
Show file tree
Hide file tree
Showing 25 changed files with 434 additions and 386 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ dependencies {
implementation "commons-codec:commons-codec:${commonsCodecVersion}"
implementation "software.pando.crypto:salty-coffee:${pandoCryptoVersion}"
implementation "org.apache.commons:commons-collections4:${apacheCollectionsVersion}"
implementation 'org.springframework.boot:spring-boot-starter-actuator'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
annotationProcessor "org.mapstruct:mapstruct-processor:${mapStructVersion}"
Expand Down
28 changes: 2 additions & 26 deletions src/main/java/com/danielvm/destiny2bot/Destiny2botApplication.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package com.danielvm.destiny2bot;

import com.danielvm.destiny2bot.exception.ExternalServiceException;
import com.danielvm.destiny2bot.exception.InternalServerException;
import com.danielvm.destiny2bot.filter.SignatureFilterFunction;
import com.danielvm.destiny2bot.handler.InteractionHandler;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.nio.charset.StandardCharsets;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand All @@ -17,13 +14,10 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

@Slf4j
@EnableCaching
Expand All @@ -36,7 +30,7 @@ public static void main(String[] args) {
}

@Bean
RouterFunction<ServerResponse> interactionFilterFunction(
RouterFunction<ServerResponse> interactionRouterFunction(
InteractionHandler interactionHandler,
SignatureFilterFunction signatureFilterFunction) {
return RouterFunctions.route()
Expand Down Expand Up @@ -72,25 +66,7 @@ CacheManager inMemoryCacheManager() {
*/
@Bean
public WebClient.Builder webClient() {
return WebClient.builder()
.defaultStatusHandler(
HttpStatusCode::is5xxServerError,
clientResponse -> clientResponse.createException()
.flatMap(ce -> Mono.error(new ExternalServiceException(
ce.getResponseBodyAsString(StandardCharsets.UTF_8),
HttpStatus.INTERNAL_SERVER_ERROR,
ce.getCause()))
)
)
.defaultStatusHandler(
HttpStatusCode::is4xxClientError,
clientResponse -> clientResponse.createException()
.flatMap(ce -> Mono.error(
new InternalServerException(
ce.getResponseBodyAsString(StandardCharsets.UTF_8),
HttpStatus.BAD_REQUEST)
))
);
return WebClient.builder();
}

@Bean
Expand Down
22 changes: 18 additions & 4 deletions src/main/java/com/danielvm/destiny2bot/client/BungieClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

import com.danielvm.destiny2bot.dto.destiny.ActivitiesResponse;
import com.danielvm.destiny2bot.dto.destiny.BungieResponse;
import com.danielvm.destiny2bot.dto.destiny.ExactUserSearchRequest;
import com.danielvm.destiny2bot.dto.destiny.ExactUserSearchResponse;
import com.danielvm.destiny2bot.dto.destiny.MemberGroupResponse;
import com.danielvm.destiny2bot.dto.destiny.MembershipResponse;
import com.danielvm.destiny2bot.dto.destiny.PostGameCarnageReport;
import com.danielvm.destiny2bot.dto.destiny.SearchResult;
import com.danielvm.destiny2bot.dto.destiny.UserGlobalSearchBody;
import com.danielvm.destiny2bot.dto.destiny.characters.CharactersResponse;
import com.danielvm.destiny2bot.dto.destiny.manifest.ResponseFields;
import com.danielvm.destiny2bot.dto.destiny.manifest.ManifestResponseFields;
import com.danielvm.destiny2bot.dto.destiny.milestone.MilestoneEntry;
import com.danielvm.destiny2bot.enums.ManifestEntity;
import java.util.List;
import java.util.Map;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
Expand Down Expand Up @@ -40,10 +43,10 @@ Mono<BungieResponse<MembershipResponse>> getMembershipInfoById(
*
* @param entityType The entity type (see {@link ManifestEntity})
* @param hashIdentifier The entity hash identifier
* @return {@link Mono} of {@link ResponseFields}
* @return {@link Mono} of {@link ManifestResponseFields}
*/
@GetExchange("/Destiny2/Manifest/{entityType}/{hashIdentifier}/")
Mono<BungieResponse<ResponseFields>> getManifestEntity(
Mono<BungieResponse<ManifestResponseFields>> getManifestEntity(
@PathVariable String entityType, @PathVariable Long hashIdentifier);

/**
Expand All @@ -52,7 +55,7 @@ Mono<BungieResponse<ResponseFields>> getManifestEntity(
* @return {@link Mono} of Map of {@link MilestoneEntry}
*/
@GetExchange("/Destiny2/Milestones/")
Mono<BungieResponse<Map<String, MilestoneEntry>>> getPublicMilestonesRx();
Mono<BungieResponse<Map<String, MilestoneEntry>>> getPublicMilestones();

/**
* Get a user characters
Expand Down Expand Up @@ -121,4 +124,15 @@ Mono<BungieResponse<ActivitiesResponse>> getActivityHistory(@PathVariable Intege
Mono<BungieResponse<PostGameCarnageReport>> getPostGameCarnageReport(
@PathVariable Long activityId
);

/**
* Search for a Destiny 2 membership using exact parameters, that is, name and code
*
* @param request The request to make to Bungie for an exact user search
* @return {@link ExactUserSearchRequest}
*/
@PostExchange("/Destiny2/SearchDestinyPlayerByBungieName/-1/")
Mono<BungieResponse<List<ExactUserSearchResponse>>> searchUserByExactNameAndCode(
@RequestBody ExactUserSearchRequest request
);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public BungieClient bungieCharacterClient(WebClient.Builder builder) {
.clientConnector(new ReactorClientHttpConnector(httpClient))
.defaultHeader(API_KEY_HEADER_NAME, this.key)
.codecs(clientCodecConfigurer -> clientCodecConfigurer.defaultCodecs()
.maxInMemorySize(1024 * 1024))
.maxInMemorySize(1024 * 512))
.build();
return HttpServiceProxyFactory.builder()
.exchangeAdapter(WebClientAdapter.create(webClient))
Expand All @@ -95,16 +95,13 @@ public BungieClient bungieCharacterClient(WebClient.Builder builder) {
@Bean(name = "pgcrBungieClient")
public BungieClient pgcrBungieClient(WebClient.Builder builder) {
// Don't keep alive connections with Bungie.net
HttpClient httpClient = HttpClient.create()
.keepAlive(false);
var webClient = builder
.baseUrl(this.statsBaseUrl)
.clientConnector(new ReactorClientHttpConnector(httpClient))
.defaultHeader(API_KEY_HEADER_NAME, this.key)
.defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.codecs(clientCodecConfigurer -> clientCodecConfigurer.defaultCodecs()
.maxInMemorySize(1024 * 1024 * 5))
.maxInMemorySize(1024 * 20))
.build();
return HttpServiceProxyFactory.builder()
.exchangeAdapter(WebClientAdapter.create(webClient))
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.danielvm.destiny2bot.dto.destiny;

import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class ExactUserSearchRequest {

private String displayName;

private Integer displayNameCode;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.danielvm.destiny2bot.dto.destiny;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ExactUserSearchResponse {

private String bungieGlobalDisplayName;

private Integer bungieGlobalDisplayNameCode;

private Integer membershipType;

private String membershipId;

private String displayName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class ResponseFields {
public class ManifestResponseFields {

private DisplayProperties displayProperties;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.danielvm.destiny2bot.dto.discord;

import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -29,4 +30,9 @@ public class Option {
*/
private Boolean focused;

/**
* The list of options that belong to this sub-command or sub-command group
*/
private List<Option> options;

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class SignatureFilterFunction implements

private static final String SIGNATURE_HEADER_NAME = "X-Signature-Ed25519";
private static final String TIMESTAMP_HEADER_NAME = "X-Signature-Timestamp";

private final DiscordConfiguration discordConfiguration;

public SignatureFilterFunction(DiscordConfiguration discordConfiguration) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.danielvm.destiny2bot.enums.InteractionResponseType;
import com.danielvm.destiny2bot.enums.InteractionType;
import com.danielvm.destiny2bot.enums.SlashCommand;
import com.danielvm.destiny2bot.exception.BaseException;
import com.danielvm.destiny2bot.factory.ApplicationCommandFactory;
import com.danielvm.destiny2bot.factory.AutocompleteFactory;
import com.danielvm.destiny2bot.factory.MessageComponentFactory;
Expand All @@ -16,6 +17,7 @@
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.ProblemDetail;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.BodyInserters;
Expand Down Expand Up @@ -67,7 +69,15 @@ public Mono<ServerResponse> handle(ServerRequest request) {
BodyInserters.fromProducer(attachmentsResponse(interaction, response),
multiValueReference) :
BodyInserters.fromValue(response));
});
})
.onErrorResume(BaseException.class,
ex -> {
ProblemDetail problemDetail = ProblemDetail.forStatus(ex.getStatus());
problemDetail.setDetail(ex.getMessage());
return ServerResponse.status(ex.getStatus())
.body(BodyInserters.fromValue(ex.getMessage()));
}
);
});
}

Expand Down
Loading

0 comments on commit edcc0c7

Please sign in to comment.