Skip to content

Commit

Permalink
Merge pull request #84 from MaciejDybowski/common
Browse files Browse the repository at this point in the history
Dodanie do clienta obsługi endpointu do pobierania faktur bez uwierzy…
  • Loading branch information
alapierre authored Oct 4, 2023
2 parents 8b6b736 + 2d3b942 commit c0d0035
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.alapierre.io.IOUtils;
import io.alapierre.ksef.client.ApiClient;
import io.alapierre.ksef.client.ApiException;
import io.alapierre.ksef.client.model.rest.common.*;
import io.alapierre.ksef.client.model.rest.invoice.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -80,13 +81,22 @@ public void getInvoice(@NotNull String referenceNumber, @NotNull String token, @
apiClient.getStream(endpoint, token, os);
}


/**
* Pobranie faktury bez uwierzytelniania na podstawie parametrów podanych przez KSeF zgodnie ze specyfikacją endpoint'u
* common/Invoice/KSeF. Limit w sekwencji 2 użycia, czas odnowy 60 minut
*/
public void getInvoice(@NotNull InvoiceRequest invoiceRequest, @NotNull OutputStream os) throws ApiException {
val endpoint = "common/Invoice/KSeF";
apiClient.postStream(endpoint, invoiceRequest, os);
}

/**
* Pobiera UPO dla podanego numeru referencyjnego sesji interaktywnej lub wsadowej. Przekształca wynik zwracany
* z API ze String na ciąg bajtów zakodowany w UTF-8. Jeśli UPO nie jest dostępne, pole UpoDTO.upo będzie miało
* wartość null.
*
* @param referenceNumber numer referencyjny zakończonej sesji interaktywnej lub wsadowej
*
* @return Odpowiedź z API z UPO w postaci ciągu bajtów (jeśli UPO jest dostępne)
*/
public UpoDTO getUpo(@NotNull String referenceNumber) throws ApiException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.alapierre.ksef.client.model.rest.common;

import io.alapierre.ksef.client.model.rest.query.InvoiceQueryResponse;
import lombok.*;

@Data
@Builder
public class InvoiceRequest {
private InvoiceDetails invoiceDetails;
private String ksefReferenceNumber;


@Data
@Builder
public static class InvoiceDetails {
private String dueValue;
private String invoiceOryginalNumber;
private InvoiceQueryResponse.SubjectTo subjectTo;

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.alapierre.ksef.client.model.rest.query;

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

import java.util.List;

Expand Down Expand Up @@ -36,20 +38,26 @@ public static class InvoiceHeaderList{
}

@Data
@AllArgsConstructor
@NoArgsConstructor
public static class SubjectTo{
private IssuedToIdentifier issuedToIdentifier;
private IssuedToName issuedToName;
}


@Data
@AllArgsConstructor
@NoArgsConstructor
public static class IssuedToName{
private String type;
private String tradeName;
private String fullName;
}

@Data
@AllArgsConstructor
@NoArgsConstructor
public static class IssuedToIdentifier{
private String type;
private String identifier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,25 @@ public void getStream(@NotNull String endpoint, @NotNull String token, @NotNull
throw new IllegalStateException("Not implemented yet");
}

@Override
public void postStream(@NotNull String endpoint, @NotNull Object body, @NotNull OutputStream os) throws ApiException {
HttpRequest request = preparePostRequest(endpoint, Collections.emptyMap(), HttpRequest.BodyPublishers.ofString(serializer.toJson(body)), true);

try {
HttpResponse<InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
if (response.body() != null) {
try (InputStream is = response.body()) {
IOUtils.copy(is, os);
}
}
} catch (IOException | InterruptedException e) {
throw new ApiException(e);
}

throw new IllegalStateException("Not implemented yet");
}


private HttpRequest prepareGetRequest(@NotNull String endpoint, @NotNull Map<String, String> headers) throws ApiException {
List<String> headersList = prepareHeaders(headers);
URI uri = prepareURI(endpoint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

import java.io.IOException;
import java.io.OutputStream;
import java.util.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
* @author Adrian Lapierre {@literal [email protected]}
Expand Down Expand Up @@ -123,7 +126,7 @@ public void getStream(@NotNull String endpoint, @NotNull String token, @NotNull
throw createException(response);
}

if(response.body() != null) {
if (response.body() != null) {
try (val is = response.body().byteStream()) {
IOUtils.copy(is, os);
}
Expand All @@ -133,7 +136,28 @@ public void getStream(@NotNull String endpoint, @NotNull String token, @NotNull
}
}

protected <B, R> Optional<R> doPostJson(@NotNull String endpoint, @NotNull B body, @NotNull Class<R> classOfR, Map<String, String> headers) throws ApiException {
@Override
public void postStream(@NotNull String endpoint, @NotNull Object body, @NotNull OutputStream os) throws ApiException {
RequestBody requestBody = RequestBody.create(serializer.toJson(body), JSON);
Request request = createRequest(endpoint, requestBody, Collections.emptyMap(), true);

try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw createException(response);
}

if (response.body() != null) {
try (val is = response.body().byteStream()) {
IOUtils.copy(is, os);
}
}
} catch (IOException ex) {
throw new ApiException(ex);
}
}


protected <B, R> Optional<R> doPostJson(@NotNull String endpoint, @NotNull B body, @NotNull Class<R> classOfR, Map<String, String> headers) throws ApiException {

RequestBody requestBody = RequestBody.create(serializer.toJson(body), JSON);
Request request = createRequest(endpoint, requestBody, headers, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ public interface ApiClient {
<R> Optional<R> postXML(@NotNull String endpoint, @NotNull Object body, @NotNull Class<R> classOfR) throws ApiException;

void getStream(@NotNull String endpoint, @NotNull String token, @NotNull OutputStream os) throws ApiException;
void postStream(@NotNull String endpoint, @NotNull Object body, @NotNull OutputStream os) throws ApiException;
}
39 changes: 39 additions & 0 deletions ksef-sample/src/main/java/io/alapierre/ksef/sample/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@
import io.alapierre.ksef.client.iterator.InvoiceQueryResponseAdapter;
import io.alapierre.ksef.client.iterator.KsefResultStream;
import io.alapierre.ksef.client.model.rest.auth.InitSignedResponse;
import io.alapierre.ksef.client.model.rest.common.InvoiceRequest;
import io.alapierre.ksef.client.model.rest.query.InvoiceQueryRequest;
import io.alapierre.ksef.client.model.rest.query.InvoiceQueryResponse;
import io.alapierre.ksef.client.okhttp.OkHttpApiClient;
import io.alapierre.ksef.client.serializer.gson.GsonJsonSerializer;
import io.alapierre.ksef.token.facade.KsefTokenFacade;
import lombok.val;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.KeyStore;
import java.text.ParseException;

Expand Down Expand Up @@ -66,6 +71,40 @@ public static void main(String[] args) {
}
}

public static void fetchInvoiceByCommonAPI(){
try {
val invoiceApi = new InterfejsyInteraktywneFakturaApi(client);
InvoiceQueryResponse.IssuedToIdentifier issued = new InvoiceQueryResponse.IssuedToIdentifier("onip", "9655573052");
InvoiceQueryResponse.IssuedToName name = new InvoiceQueryResponse.IssuedToName("fn", null, "Firma Janowski");
InvoiceQueryResponse.SubjectTo subject = new InvoiceQueryResponse.SubjectTo(issued, name);
val invoiceDetails = InvoiceRequest.InvoiceDetails.builder()
.invoiceOryginalNumber("FK2023/09/14")
.subjectTo(subject)
.dueValue("100")
.build();

val requestBody = InvoiceRequest.builder()
.ksefReferenceNumber("5282740347-20230914-2979E373175E-25")
.invoiceDetails(invoiceDetails)
.build();


ByteArrayOutputStream out = new ByteArrayOutputStream(0);
invoiceApi.getInvoice(requestBody, out);

try (FileOutputStream fos = new FileOutputStream(requestBody.getKsefReferenceNumber() + ".xml")) {
fos.write(out.toByteArray());
} catch (IOException ex) {
throw new RuntimeException(ex);
}

} catch (ApiException ex) {
System.out.printf("Błąd wywołania API %d (%s) opis błędu %s", ex.getCode(), ex.getMessage(), ex.getResponseBody());
} catch (Exception e) {
System.out.println(e.getMessage());
}
}

// public static InitSignedResponse loginBySignature() throws IOException, ApiException {
//
// val signer = new P12Signer(pas, tokenFile);
Expand Down

0 comments on commit c0d0035

Please sign in to comment.