Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Update due to API changes #22

Merged
merged 5 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions Demo/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<!-- Demo/pom.xml -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>buttercms-demo</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<dependencies>
<!-- Dependency on the local SDK project -->
<dependency>
<groupId>com.buttercms</groupId>
<artifactId>buttercmsclient</artifactId>
<version>1.12.1</version>
</dependency>
</dependencies>

<build>
<plugins>
<!-- Compile Java sources -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

<!-- Create an executable JAR with dependencies -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>com.demo.Demo</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
47 changes: 47 additions & 0 deletions Demo/src/main/java/com/demo/Demo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.demo;

import com.buttercms.ButterCMSClient;
import com.buttercms.model.Page;
import com.buttercms.model.PageResponse;
import com.buttercms.model.PostResponse;
import com.buttercms.model.Post;
import com.demo.PageFields;

import java.util.Map;
import java.util.HashMap;


public class Demo {
public static void main(String[] args) {
String apiKey = System.getenv("API_KEY");
if (apiKey == null || apiKey.isEmpty()) {
System.out.println("API_KEY environment variable is not set");
return;
}

ButterCMSClient client = new ButterCMSClient(apiKey, true);

try {
PageResponse<PageFields> pageResponse = client.getPage("*", "test-page-1", null, PageFields.class);
Page<PageFields> page = pageResponse.getData();

System.out.println("Page Slug: " + page.getSlug());
System.out.println("Page Status: " + page.getStatus());
System.out.println("Page Scheduled date: " + page.getScheduled());
} catch (Exception e) {
System.out.println("Error fetching page: " + e.getMessage());
}

try {
PostResponse postResponse = client.getPost("test-blog-post");
Post post = postResponse.getData();

System.out.println("Post Title: " + post.getTitle());
System.out.println("Post Status: " + post.getStatus());
System.out.println("Post Published date: " + post.getPublished());
System.out.println("Post Scheduled date: " + post.getScheduled());
} catch (Exception e) {
System.out.println("Error fetching post: " + e.getMessage());
}
}
}
6 changes: 6 additions & 0 deletions Demo/src/main/java/com/demo/PageFields.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.demo;

public class PageFields {
public String title;
public String description;
}
17 changes: 17 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Stage 1: Build Java SDK and Demo
FROM openjdk:8-jdk-alpine AS build_java

WORKDIR /src
COPY . .
RUN ./mvnw clean install -DskipTests
RUN ./mvnw -f /src/Demo/pom.xml clean package -DskipTests

# Stage 2: Run the Demo
FROM openjdk:8-jdk-alpine
WORKDIR /app
COPY --from=build_java /src/Demo/target/buttercms-demo-1.0.0-jar-with-dependencies.jar .

ENV API_BASE_URL=https://api.buttercms.com/v2
ENV API_KEY=your_api_key

CMD ["java", "-jar", "buttercms-demo-1.0.0-jar-with-dependencies.jar"]
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ PagesResponse posts = butterClient.getSearchPages(queryParams);
|url|String|
|created|Date|
|published|Date|
|scheduled|Date|
|Author|[Author](#author-class)|
|Categories|List&lt;[Category](#category-class)&gt;|
|Tags|List&lt;[Tag](#tag-class)&gt;|
Expand All @@ -507,6 +508,7 @@ PagesResponse posts = butterClient.getSearchPages(queryParams);
|---|---|
|Draft|1|
|Published|2|
|Scheduled|3|

### PostResponse Class

Expand Down Expand Up @@ -638,10 +640,14 @@ PagesResponse posts = butterClient.getSearchPages(queryParams);
| Property | Type|
|----|---|
|slug| string|
|pageType|string|
|fields|T|
|status|[Status](#status-enum)|
|scheduled|Date|

## Exceptions

### ButterCMSResponseException

General RunTime exception that wraps API error responses

10 changes: 9 additions & 1 deletion src/main/java/com/buttercms/ButterCMSClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,15 @@ private Document readXML(String url) {
}

private String buildURL(final String path, final Map<String, String> queryParameters) {
final URIBuilder uriBuilder = new URIBuilder(URI.create(ButterCMSAPIConfig.API_BASE + path));
String baseUrlFromEnv = System.getenv("API_BASE_URL");
String baseUrl;
if (baseUrlFromEnv != null) {
baseUrl = baseUrlFromEnv;
} else {
baseUrl = ButterCMSAPIConfig.API_BASE_DEFAULT;
}

final URIBuilder uriBuilder = new URIBuilder(URI.create(baseUrl + path));
uriBuilder.addParameter("auth_token", this.authToken);
uriBuilder.addParameter("preview", this.preview.toString());
if (queryParameters != null) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/buttercms/config/ButterCMSAPIConfig.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.buttercms.config;

public class ButterCMSAPIConfig {
public static String API_BASE = "https://api.buttercms.com/v2";
public static String API_BASE_DEFAULT = "https://api.buttercms.com/v2";
public static String AUTHORS = "/authors/";
public static String CATEGORIES = "/categories/";
public static String PAGES = "/pages/";
Expand Down
76 changes: 67 additions & 9 deletions src/main/java/com/buttercms/model/Page.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package com.buttercms.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Date;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.Optional;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Page<T> {
private String slug;
private String pageType;
private String name;
private T fields;
private Status status;
private Date updated;
private Optional<String> pageType;
private Optional<Date> published;
private Optional<Date> scheduled;

public String getSlug() {
return slug;
Expand All @@ -19,14 +27,6 @@ public void setSlug(String slug) {
this.slug = slug;
}

public String getPageType() {
return pageType;
}

public void setPageType(String pageType) {
this.pageType = pageType;
}

public T getFields() {
return fields;
}
Expand All @@ -35,6 +35,54 @@ public void setFields(T fields) {
this.fields = fields;
}

public Status getStatus() {
return status;
}

public void setStatus(Status status) {
this.status = status;
}

public Optional<Date> getScheduled() {
return scheduled;
}

public void setScheduled(Date scheduled) {
this.scheduled = Optional.ofNullable(scheduled);
}

public Date getUpdated() {
return updated;
}

public void setUpdated(Date updated) {
this.updated = updated;
}

public Optional<Date> getPublished() {
return published;
}

public void setPublished(Date published) {
this.published = Optional.ofNullable(published);
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Optional<String> getPageType() {
return pageType;
}

public void setPageType(String pageType) {
this.pageType = Optional.ofNullable(pageType);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -56,4 +104,14 @@ public String toString() {
.add("fields=" + fields)
.toString();
}

public enum Status {
@JsonProperty("draft")
DRAFT,
@JsonProperty("published")
PUBLISHED,
@JsonProperty("scheduled")
SCHEDULED
}
}

18 changes: 16 additions & 2 deletions src/main/java/com/buttercms/model/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class Post {
private Date created;
private String metaDescription;
private Date published;
private Date scheduled;
private String seoTitle;
private String slug;
private Status status;
Expand Down Expand Up @@ -74,6 +75,14 @@ public void setPublished(Date published) {
this.published = published;
}

public Date getScheduled() {
return scheduled;
}

public void setScheduled(Date scheduled) {
this.scheduled = scheduled;
}

public String getSeoTitle() {
return seoTitle;
}
Expand Down Expand Up @@ -151,6 +160,7 @@ public boolean equals(Object o) {
Objects.equals(created, post.created) &&
Objects.equals(metaDescription, post.metaDescription) &&
Objects.equals(published, post.published) &&
Objects.equals(scheduled, post.scheduled) &&
Objects.equals(seoTitle, post.seoTitle) &&
Objects.equals(slug, post.slug) &&
status == post.status &&
Expand All @@ -164,7 +174,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return Objects.hash(author, body, categories, created, metaDescription, published, seoTitle, slug, status, summary, tags, title, url, featuredImage, featuredImageAlt);
return Objects.hash(author, body, categories, created, metaDescription, published, scheduled, seoTitle, slug, status, summary, tags, title, url, featuredImage, featuredImageAlt);
}

@Override
Expand All @@ -188,6 +198,10 @@ public enum Status {
DRAFT,

@JsonProperty("published")
PUBLISHED
PUBLISHED,

@JsonProperty("scheduled")
SCHEDULED
}
}