Skip to content

Commit

Permalink
better exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bitwheeze committed Jul 20, 2023
1 parent dd8a8f3 commit d286324
Show file tree
Hide file tree
Showing 64 changed files with 1,799 additions and 1,706 deletions.
20 changes: 11 additions & 9 deletions src/main/java/bitwheeze/golos/goloslib/ApplicationProperties.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package bitwheeze.golos.goloslib;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "golos")
public class ApplicationProperties {
String api;

}
package bitwheeze.golos.goloslib;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "golos")
public class ApplicationProperties {
String api;

}
136 changes: 68 additions & 68 deletions src/main/java/bitwheeze/golos/goloslib/GolosApiConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,68 +1,68 @@
package bitwheeze.golos.goloslib;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.http.codec.LoggingCodecSupport;
import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.http.codec.json.Jackson2JsonEncoder;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.web.reactive.function.client.ExchangeStrategies;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;

import java.util.Collections;

@Slf4j
@Configuration
public class GolosApiConfiguration {

@Value("${golos.api:https://api-golos.blckchnd.com}")
private String url;

@Value("${golos.media-type:application/json}")
private String mediaType;

@Bean(name = "golos_api")
public WebClient buildWebClient(ObjectMapper mapper) {
ExchangeStrategies exchangeStrategies = ExchangeStrategies.builder()
.codecs(clientDefaultCodecsConfigurer -> {
clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder(mapper, MediaType.APPLICATION_JSON));
clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(mapper, MediaType.valueOf(mediaType)));
})
.build();

exchangeStrategies
.messageWriters().stream()
.filter(LoggingCodecSupport.class::isInstance)
.forEach(writer -> ((LoggingCodecSupport)writer).setEnableLoggingRequestDetails(true));

log.info("using api url {}", url);
return WebClient.builder()
.baseUrl(url)
//.defaultCookie("cookieKey", "cookieValue")
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.defaultUriVariables(Collections.singletonMap("url", url))
.exchangeStrategies(exchangeStrategies)
.clientConnector(new ReactorClientHttpConnector(
HttpClient.create().wiretap(true)
))
.build();
}

@Primary
@Bean
public ObjectMapper buildObjectMapper(Jackson2ObjectMapperBuilder builder) {
var mapper = builder.build();
mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
return mapper;
}

}
package bitwheeze.golos.goloslib;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.http.codec.LoggingCodecSupport;
import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.http.codec.json.Jackson2JsonEncoder;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.web.reactive.function.client.ExchangeStrategies;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;

import java.util.Collections;

@Slf4j
@Configuration
public class GolosApiConfiguration {

@Value("${golos.api:https://api-golos.blckchnd.com}")
private String url;

@Value("${golos.media-type:application/json}")
private String mediaType;

@Bean(name = "golos_api")
public WebClient buildWebClient(ObjectMapper mapper) {
ExchangeStrategies exchangeStrategies = ExchangeStrategies.builder()
.codecs(clientDefaultCodecsConfigurer -> {
clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder(mapper, MediaType.APPLICATION_JSON));
clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(mapper, MediaType.valueOf(mediaType)));
})
.build();

exchangeStrategies
.messageWriters().stream()
.filter(LoggingCodecSupport.class::isInstance)
.forEach(writer -> ((LoggingCodecSupport)writer).setEnableLoggingRequestDetails(true));

log.info("using api url {}", url);
return WebClient.builder()
.baseUrl(url)
//.defaultCookie("cookieKey", "cookieValue")
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.defaultUriVariables(Collections.singletonMap("url", url))
.exchangeStrategies(exchangeStrategies)
.clientConnector(new ReactorClientHttpConnector(
HttpClient.create().wiretap(true)
))
.build();
}

@Primary
@Bean
public ObjectMapper buildObjectMapper(Jackson2ObjectMapperBuilder builder) {
var mapper = builder.build();
mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
return mapper;
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bitwheeze.golos.goloslib;

public class GolosApiProperties {
}
package bitwheeze.golos.goloslib;

public class GolosApiProperties {
}
Loading

0 comments on commit d286324

Please sign in to comment.