Skip to content

Commit

Permalink
obsługa dodatkowych błędów i możliwośc konfiguracji okhttp
Browse files Browse the repository at this point in the history
  • Loading branch information
alapierre committed Oct 23, 2023
1 parent dc874cb commit c56fa6a
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,30 @@ public class OkHttpApiClient extends AbstractApiClient {

public static final String API_EXCEPTION = "Błąd wywołania API";

private final OkHttpClient client = new OkHttpClient();
private final OkHttpClient client;

private final MediaType JSON = MediaType.get("application/json; charset=utf-8");
private final MediaType XML = MediaType.get("application/xml; charset=utf-8");
private final MediaType OCTET = MediaType.get("application/octet-stream; charset=utf-8");

public OkHttpApiClient(JsonSerializer serializer) {
super(serializer, Environment.TEST);
client = new OkHttpClient();
}

public OkHttpApiClient(JsonSerializer serializer, String url) {
super(serializer, url);
client = new OkHttpClient();
}

public OkHttpApiClient(JsonSerializer serializer, Environment environment) {
super(serializer, environment);
client = new OkHttpClient();
}

public OkHttpApiClient(JsonSerializer serializer, Environment environment, OkHttpClient client) {
super(serializer, environment);
this.client = client;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package io.alapierre.ksef.client;

import io.alapierre.ksef.client.exception.BadRequestException;
import io.alapierre.ksef.client.exception.ExceptionResponse;
import io.alapierre.ksef.client.exception.TooManyRequestsException;
import io.alapierre.ksef.client.exception.*;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Marshaller;
Expand All @@ -16,6 +14,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -83,15 +82,27 @@ protected List<ApiException.ExceptionDetail> getExceptionDetails(@Nullable Strin
}

protected ApiException mapHttpResponseStatusToException(int code, String message, Map<String, List<String>> headers, String body) {

val exceptionsFromKSef = getExceptionDetails(body);
val errorCodes = exceptionsFromKSef != null ? extractKsefExceptionsCodes(exceptionsFromKSef) : Collections.emptySet();

if(code == 429)
return new TooManyRequestsException(code, message, headers, body, getExceptionDetails(body));
return new TooManyRequestsException(code, message, headers, body, exceptionsFromKSef);
else if (code == 400) {
return new BadRequestException(code, message, headers, body, getExceptionDetails(body));

if(errorCodes.contains(21177)) return new MaxResultsExceededException(code, message, headers, body, exceptionsFromKSef);
if(errorCodes.contains(21301)) return new NoAuthorizationException(code, message, headers, body, exceptionsFromKSef);

return new BadRequestException(code, message, headers, body, exceptionsFromKSef);
}

return new ApiException(code, message, headers, body, getExceptionDetails(body));
}

protected Set<Integer> extractKsefExceptionsCodes(List<ApiException.ExceptionDetail> list) {
return list.stream().map(ApiException.ExceptionDetail::getExceptionCode).collect(Collectors.toSet());
}

@Getter
@RequiredArgsConstructor
public enum Environment {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.alapierre.ksef.client.exception;

import java.util.List;
import java.util.Map;

/**
* @author Adrian Lapierre {@literal [email protected]}
* Copyrights by original author 2023.10.23
*/
public class MaxResultsExceededException extends NonRepeatableException {

public MaxResultsExceededException(int code, String message, Map<String, List<String>> responseHeaders, String responseBody, List<ExceptionDetail> details) {
super(message, null, code, responseHeaders, responseBody, details);
}

public MaxResultsExceededException(String message, Throwable throwable, int code, Map<String, List<String>> responseHeaders, String responseBody, List<ExceptionDetail> details) {
super(message, throwable, code, responseHeaders, responseBody, details);
}

public MaxResultsExceededException(String message, Throwable throwable, int code, Map<String, List<String>> responseHeaders, String responseBody) {
super(message, throwable, code, responseHeaders, responseBody);
}

public MaxResultsExceededException(String message, int code, Map<String, List<String>> responseHeaders, String responseBody) {
super(message, code, responseHeaders, responseBody);
}

public MaxResultsExceededException(int code, String message, Map<String, List<String>> responseHeaders, String responseBody) {
super(code, message, responseHeaders, responseBody);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package io.alapierre.ksef.client.exception;

import io.alapierre.ksef.client.ApiException;

import java.util.List;
import java.util.Map;

/**
* @author Adrian Lapierre {@literal [email protected]}
* Copyrights by original author 2023.10.23
*/
public class NoAuthorizationException extends ApiException {

public NoAuthorizationException() {
}

public NoAuthorizationException(Throwable throwable) {
super(throwable);
}

public NoAuthorizationException(String message) {
super(message);
}

public NoAuthorizationException(String message, Throwable throwable, int code, Map<String, List<String>> responseHeaders, String responseBody, List<ExceptionDetail> details) {
super(message, throwable, code, responseHeaders, responseBody, details);
}

public NoAuthorizationException(String message, Throwable throwable, int code, Map<String, List<String>> responseHeaders, String responseBody) {
super(message, throwable, code, responseHeaders, responseBody);
}

public NoAuthorizationException(String message, int code, Map<String, List<String>> responseHeaders, String responseBody) {
super(message, code, responseHeaders, responseBody);
}

public NoAuthorizationException(String message, Throwable throwable, int code, Map<String, List<String>> responseHeaders) {
super(message, throwable, code, responseHeaders);
}

public NoAuthorizationException(int code, Map<String, List<String>> responseHeaders, String responseBody) {
super(code, responseHeaders, responseBody);
}

public NoAuthorizationException(int code, String message) {
super(code, message);
}

public NoAuthorizationException(int code, String message, Map<String, List<String>> responseHeaders, String responseBody, List<ExceptionDetail> details) {
super(code, message, responseHeaders, responseBody, details);
}

public NoAuthorizationException(int code, String message, Map<String, List<String>> responseHeaders, String responseBody) {
super(code, message, responseHeaders, responseBody);
}

public NoAuthorizationException(String message, Throwable throwable) {
super(message, throwable);
}
}

0 comments on commit c56fa6a

Please sign in to comment.