Add REST API for Caruna/Helen parser
This commit is contained in:
parent
51acb20cdd
commit
b1f4a74152
|
@ -1,5 +1,6 @@
|
||||||
package com.devsoap.parsers.composites;
|
package com.devsoap.parsers.composites;
|
||||||
|
|
||||||
|
import java.io.PrintStream;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -10,7 +11,10 @@ public class CarunaHelenParser {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
var carunaFile = Path.of(args[0]);
|
var carunaFile = Path.of(args[0]);
|
||||||
var helenFile = Path.of(args[1]);
|
var helenFile = Path.of(args[1]);
|
||||||
|
run(carunaFile, helenFile, System.out);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void run(Path carunaFile, Path helenFile, PrintStream result) {
|
||||||
var carunaPeriods = com.devsoap.parsers.caruna.Parser.parse(carunaFile);
|
var carunaPeriods = com.devsoap.parsers.caruna.Parser.parse(carunaFile);
|
||||||
var nightSiirto = carunaPeriods.entrySet().stream()
|
var nightSiirto = carunaPeriods.entrySet().stream()
|
||||||
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().transferNightKwh));
|
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().transferNightKwh));
|
||||||
|
@ -18,7 +22,7 @@ public class CarunaHelenParser {
|
||||||
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().transferDayKwh));
|
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().transferDayKwh));
|
||||||
var helenPeriods = com.devsoap.parsers.helen.Parser.parse(helenFile, daySiirto, nightSiirto);
|
var helenPeriods = com.devsoap.parsers.helen.Parser.parse(helenFile, daySiirto, nightSiirto);
|
||||||
|
|
||||||
System.out.println("Kuukausi,Perusmaksu (energia),Perusmaksu (siirto),Päiväenergia (kWh),Päiväenergia " +
|
result.println("Kuukausi,Perusmaksu (energia),Perusmaksu (siirto),Päiväenergia (kWh),Päiväenergia " +
|
||||||
"(EUR),Yöenergia (kWh),Yöenergia (EUR),Päiväsiirto (kWh),Päiväsiirto (EUR),Yösiirto (kWh)" +
|
"(EUR),Yöenergia (kWh),Yöenergia (EUR),Päiväsiirto (kWh),Päiväsiirto (EUR),Yösiirto (kWh)" +
|
||||||
",Yösiirto (EUR),Vero");
|
",Yösiirto (EUR),Vero");
|
||||||
|
|
||||||
|
@ -33,7 +37,7 @@ public class CarunaHelenParser {
|
||||||
hp.nightEnergyEur, cp.transferDayKwh, cp.transferDayTotal, cp.transferNightKwh,
|
hp.nightEnergyEur, cp.transferDayKwh, cp.transferDayTotal, cp.transferNightKwh,
|
||||||
cp.transferNightTotal, cp.tax);
|
cp.transferNightTotal, cp.tax);
|
||||||
csv = csv.replace(",0,",",,").replace(",0.00",",");
|
csv = csv.replace(",0,",",,").replace(",0.00",",");
|
||||||
System.out.println(csv);
|
result.println(csv);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<form method="post" enctype="multipart/form-data">
|
||||||
|
<label for="carunaFile">Caruna File:</label>
|
||||||
|
<input type="file" id="carunaFile" name="caruna.pdf" />
|
||||||
|
<label for="helenFile">Helen File:</label>
|
||||||
|
<input type="file" id="helenFile" name="helen.pfd" />
|
||||||
|
<input type="submit" value="Submit files">
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -0,0 +1,11 @@
|
||||||
|
dependencies {
|
||||||
|
implementation 'io.javalin:javalin:3.13.8'
|
||||||
|
implementation "org.thymeleaf:thymeleaf:3.0.11.RELEASE"
|
||||||
|
|
||||||
|
implementation project(':composite-parsers')
|
||||||
|
|
||||||
|
implementation 'org.apache.logging.log4j:log4j-core:2.11.0'
|
||||||
|
implementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.11.0'
|
||||||
|
}
|
||||||
|
|
||||||
|
application.mainClass='com.devsoap.parsers.api.Api'
|
|
@ -0,0 +1,84 @@
|
||||||
|
package com.devsoap.parsers.api;
|
||||||
|
|
||||||
|
import com.devsoap.parsers.composites.CarunaHelenParser;
|
||||||
|
import io.javalin.Javalin;
|
||||||
|
import io.javalin.http.Context;
|
||||||
|
import io.javalin.http.UploadedFile;
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
import org.apache.logging.log4j.Level;
|
||||||
|
import org.apache.logging.log4j.core.config.Configurator;
|
||||||
|
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.StandardCopyOption;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
import static io.javalin.apibuilder.ApiBuilder.*;
|
||||||
|
|
||||||
|
public class Api {
|
||||||
|
|
||||||
|
static {
|
||||||
|
var logBuilder = ConfigurationBuilderFactory.newConfigurationBuilder();
|
||||||
|
logBuilder.add(logBuilder.newAppender("stdout", "Console"));
|
||||||
|
|
||||||
|
var rootLogger = logBuilder.newRootLogger(Level.INFO);
|
||||||
|
rootLogger.add(logBuilder.newAppenderRef("stdout"));
|
||||||
|
logBuilder.add(rootLogger);
|
||||||
|
|
||||||
|
Configurator.initialize(logBuilder.build());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final Logger LOGGER = Logger.getLogger(Api.class);
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
var app = Javalin.create().start(7000);
|
||||||
|
app.routes(() -> {
|
||||||
|
get(ctx -> { ctx.result("PDF Parsers REST API"); });
|
||||||
|
path("parsers", () -> {
|
||||||
|
path(CarunaHelenParser.class.getSimpleName(), () -> {
|
||||||
|
get(ctx -> ctx.render(renderParserStaticPage(CarunaHelenParser.class, "upload.html")));
|
||||||
|
post(ctx -> {
|
||||||
|
withUploadedFile(ctx.uploadedFiles().get(1), carunaFile -> {
|
||||||
|
withUploadedFile(ctx.uploadedFiles().get(0), helenFile -> {
|
||||||
|
var resultStream = new ByteArrayOutputStream();
|
||||||
|
try (var result = new PrintStream(resultStream)) {
|
||||||
|
CarunaHelenParser.run(carunaFile, helenFile, result);
|
||||||
|
}
|
||||||
|
renderReport(ctx, new ByteArrayInputStream(resultStream.toByteArray()),
|
||||||
|
"caruna-helen-report.csv");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void withUploadedFile(UploadedFile fileUpload, Consumer<Path> processor) {
|
||||||
|
try {
|
||||||
|
var file = Files.createTempFile(fileUpload.getFilename(), fileUpload.getExtension());
|
||||||
|
Files.copy(fileUpload.getContent(), file, StandardCopyOption.REPLACE_EXISTING);
|
||||||
|
try {
|
||||||
|
processor.accept(file);
|
||||||
|
} finally {
|
||||||
|
file.toFile().delete();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
LOGGER.error("Failed to process file " + fileUpload.getFilename(), e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void renderReport(Context context, InputStream result, String filename) {
|
||||||
|
context.result(result)
|
||||||
|
.header("Content-Type", "text/csv; charset=utf-8")
|
||||||
|
.header("Content-Disposition","inline; filename=\""+filename+"\"");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String renderParserStaticPage(Class<?> clazz, String page) {
|
||||||
|
var folder = clazz.getPackageName().replace(".", "/");
|
||||||
|
return folder + "/" + page;
|
||||||
|
}
|
||||||
|
}
|
|
@ -18,4 +18,5 @@ include 'caruna-invoice'
|
||||||
include 'helen-invoice'
|
include 'helen-invoice'
|
||||||
include 'composite-parsers'
|
include 'composite-parsers'
|
||||||
include 'plugsurfing-invoice'
|
include 'plugsurfing-invoice'
|
||||||
|
include 'rest-api'
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue