Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
markus-gx committed Jan 11, 2022
2 parents 94c0b52 + aa8bacc commit 4301af3
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 15 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

<groupId>io.geilehner</groupId>
<artifactId>storyblok-java-sdk</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>
<name>Storyblok Java SDK</name>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>12</java.version>
<java.version>11</java.version>
</properties>

<dependencies>
Expand Down
18 changes: 7 additions & 11 deletions src/main/java/io/geilehner/storyblok/Storyblok.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import io.geilehner.storyblok.adapters.*;
import io.geilehner.storyblok.exception.StoryblokException;
import io.geilehner.storyblok.model.Stories;
import io.geilehner.storyblok.model.Story;
Expand All @@ -17,6 +18,7 @@
import java.time.format.DateTimeParseException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class Storyblok {
private final String API_URL = "https://api.storyblok.com/v1/cdn";
Expand All @@ -31,17 +33,11 @@ public Storyblok(String apiKey){
this.API_KEY = apiKey;
this.gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.registerTypeAdapter(LocalDateTime.class, (JsonDeserializer<LocalDateTime>) (json, typeOfT, context) -> {
if(json.getAsJsonPrimitive().getAsString() == null || !json.getAsJsonPrimitive().getAsString().isEmpty()){
try{
return LocalDateTime.parse(json.getAsJsonPrimitive().getAsString(), DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSz"));
}
catch (DateTimeParseException e){
return LocalDateTime.parse(json.getAsJsonPrimitive().getAsString(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
}
}
return null;
})
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeAdapter())
.registerTypeAdapter(Long.class,new LongTypeAdapter())
.registerTypeAdapter(UUID.class,new UUIDTypeAdapter())
.registerTypeAdapter(Integer.class,new IntegerTypeAdapter())
.registerTypeAdapter(Double.class, new DoubleTypeAdapter())
.create();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.geilehner.storyblok.adapters;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;
import java.util.UUID;

public class DoubleTypeAdapter extends TypeAdapter<Double> {
@Override
public Double read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
return null;
}
String stringValue = reader.nextString();
try {
return Double.parseDouble(stringValue);
} catch (Exception e) {
return null;
}
}

@Override
public void write(JsonWriter writer, Double value) throws IOException {
if (value == null) {
writer.nullValue();
return;
}
writer.value(value.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.geilehner.storyblok.adapters;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;

public class IntegerTypeAdapter extends TypeAdapter<Integer> {
@Override
public Integer read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
return null;
}
String stringValue = reader.nextString();
try {
return Integer.parseInt(stringValue);
} catch (Exception e) {
return null;
}
}

@Override
public void write(JsonWriter writer, Integer value) throws IOException {
if (value == null) {
writer.nullValue();
return;
}
writer.value(value.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.geilehner.storyblok.adapters;

import com.google.gson.*;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;
import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

public class LocalDateTimeAdapter implements JsonDeserializer<LocalDateTime> {
@Override
public LocalDateTime deserialize(JsonElement json, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
if(json.getAsJsonPrimitive().getAsString() == null || !json.getAsJsonPrimitive().getAsString().isEmpty()){
try{
return LocalDateTime.parse(json.getAsJsonPrimitive().getAsString(), DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSz"));
}
catch (DateTimeParseException e){
return LocalDateTime.parse(json.getAsJsonPrimitive().getAsString(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
}
}
return null;
}
}
34 changes: 34 additions & 0 deletions src/main/java/io/geilehner/storyblok/adapters/LongTypeAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.geilehner.storyblok.adapters;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;

public class LongTypeAdapter extends TypeAdapter<Long> {
@Override
public Long read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
return null;
}
String stringValue = reader.nextString();
try {
Long value = Long.valueOf(stringValue);
return value;
} catch (NumberFormatException e) {
return null;
}
}

@Override
public void write(JsonWriter writer, Long value) throws IOException {
if (value == null) {
writer.nullValue();
return;
}
writer.value(value);
}
}
34 changes: 34 additions & 0 deletions src/main/java/io/geilehner/storyblok/adapters/UUIDTypeAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.geilehner.storyblok.adapters;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;
import java.util.UUID;

public class UUIDTypeAdapter extends TypeAdapter<UUID> {
@Override
public UUID read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
return null;
}
String stringValue = reader.nextString();
try {
return UUID.fromString(stringValue);
} catch (Exception e) {
return null;
}
}

@Override
public void write(JsonWriter writer, UUID value) throws IOException {
if (value == null) {
writer.nullValue();
return;
}
writer.value(value.toString());
}
}
4 changes: 2 additions & 2 deletions src/test/java/io/geilehner/storyblok/StoryblokTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.Map;

public class StoryblokTest {
@Test
/* @Test
public void testSimpleStorbylokFetch() throws StoryblokException {
Storyblok client = new Storyblok("CSBmv0Drr65cwjSpiSw4agtt");
Story<CustomStoryContent> story = client.fetchStory("home",CustomStoryContent.class);
Expand All @@ -34,5 +34,5 @@ public void testSimpleStorbylokFetch() throws StoryblokException {
Assert.assertNotNull(story4.getStories());
Assert.assertEquals(story4.getPerPage(),25);
}
}*/
}

0 comments on commit 4301af3

Please sign in to comment.