Skip to content

Commit 704892b

Browse files
Merge pull request #22 from ButterCMS/feature/api-changes
feat: Update due to API changes
2 parents 6090906 + 00123f2 commit 704892b

File tree

9 files changed

+229
-13
lines changed

9 files changed

+229
-13
lines changed

Demo/pom.xml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<!-- Demo/pom.xml -->
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.example</groupId>
7+
<artifactId>buttercms-demo</artifactId>
8+
<version>1.0.0</version>
9+
<packaging>jar</packaging>
10+
11+
<dependencies>
12+
<!-- Dependency on the local SDK project -->
13+
<dependency>
14+
<groupId>com.buttercms</groupId>
15+
<artifactId>buttercmsclient</artifactId>
16+
<version>1.12.1</version>
17+
</dependency>
18+
</dependencies>
19+
20+
<build>
21+
<plugins>
22+
<!-- Compile Java sources -->
23+
<plugin>
24+
<groupId>org.apache.maven.plugins</groupId>
25+
<artifactId>maven-compiler-plugin</artifactId>
26+
<version>3.8.1</version>
27+
<configuration>
28+
<source>1.8</source>
29+
<target>1.8</target>
30+
</configuration>
31+
</plugin>
32+
33+
<!-- Create an executable JAR with dependencies -->
34+
<plugin>
35+
<groupId>org.apache.maven.plugins</groupId>
36+
<artifactId>maven-assembly-plugin</artifactId>
37+
<version>3.3.0</version>
38+
<executions>
39+
<execution>
40+
<id>make-assembly</id>
41+
<phase>package</phase>
42+
<goals>
43+
<goal>single</goal>
44+
</goals>
45+
<configuration>
46+
<archive>
47+
<manifest>
48+
<mainClass>com.demo.Demo</mainClass>
49+
</manifest>
50+
</archive>
51+
<descriptorRefs>
52+
<descriptorRef>jar-with-dependencies</descriptorRef>
53+
</descriptorRefs>
54+
</configuration>
55+
</execution>
56+
</executions>
57+
</plugin>
58+
</plugins>
59+
</build>
60+
</project>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.demo;
2+
3+
import com.buttercms.ButterCMSClient;
4+
import com.buttercms.model.Page;
5+
import com.buttercms.model.PageResponse;
6+
import com.buttercms.model.PostResponse;
7+
import com.buttercms.model.Post;
8+
import com.demo.PageFields;
9+
10+
import java.util.Map;
11+
import java.util.HashMap;
12+
13+
14+
public class Demo {
15+
public static void main(String[] args) {
16+
String apiKey = System.getenv("API_KEY");
17+
if (apiKey == null || apiKey.isEmpty()) {
18+
System.out.println("API_KEY environment variable is not set");
19+
return;
20+
}
21+
22+
ButterCMSClient client = new ButterCMSClient(apiKey, true);
23+
24+
try {
25+
PageResponse<PageFields> pageResponse = client.getPage("*", "test-page-1", null, PageFields.class);
26+
Page<PageFields> page = pageResponse.getData();
27+
28+
System.out.println("Page Slug: " + page.getSlug());
29+
System.out.println("Page Status: " + page.getStatus());
30+
System.out.println("Page Scheduled date: " + page.getScheduled());
31+
} catch (Exception e) {
32+
System.out.println("Error fetching page: " + e.getMessage());
33+
}
34+
35+
try {
36+
PostResponse postResponse = client.getPost("test-blog-post");
37+
Post post = postResponse.getData();
38+
39+
System.out.println("Post Title: " + post.getTitle());
40+
System.out.println("Post Status: " + post.getStatus());
41+
System.out.println("Post Published date: " + post.getPublished());
42+
System.out.println("Post Scheduled date: " + post.getScheduled());
43+
} catch (Exception e) {
44+
System.out.println("Error fetching post: " + e.getMessage());
45+
}
46+
}
47+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.demo;
2+
3+
public class PageFields {
4+
public String title;
5+
public String description;
6+
}

Dockerfile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Stage 1: Build Java SDK and Demo
2+
FROM openjdk:8-jdk-alpine AS build_java
3+
4+
WORKDIR /src
5+
COPY . .
6+
RUN ./mvnw clean install -DskipTests
7+
RUN ./mvnw -f /src/Demo/pom.xml clean package -DskipTests
8+
9+
# Stage 2: Run the Demo
10+
FROM openjdk:8-jdk-alpine
11+
WORKDIR /app
12+
COPY --from=build_java /src/Demo/target/buttercms-demo-1.0.0-jar-with-dependencies.jar .
13+
14+
ENV API_BASE_URL=https://api.buttercms.com/v2
15+
ENV API_KEY=your_api_key
16+
17+
CMD ["java", "-jar", "buttercms-demo-1.0.0-jar-with-dependencies.jar"]

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,7 @@ PagesResponse posts = butterClient.getSearchPages(queryParams);
488488
|url|String|
489489
|created|Date|
490490
|published|Date|
491+
|scheduled|Date|
491492
|Author|[Author](#author-class)|
492493
|Categories|List&lt;[Category](#category-class)&gt;|
493494
|Tags|List&lt;[Tag](#tag-class)&gt;|
@@ -507,6 +508,7 @@ PagesResponse posts = butterClient.getSearchPages(queryParams);
507508
|---|---|
508509
|Draft|1|
509510
|Published|2|
511+
|Scheduled|3|
510512

511513
### PostResponse Class
512514

@@ -638,10 +640,14 @@ PagesResponse posts = butterClient.getSearchPages(queryParams);
638640
| Property | Type|
639641
|----|---|
640642
|slug| string|
643+
|pageType|string|
641644
|fields|T|
645+
|status|[Status](#status-enum)|
646+
|scheduled|Date|
642647

643648
## Exceptions
644649

645650
### ButterCMSResponseException
646651

647652
General RunTime exception that wraps API error responses
653+

src/main/java/com/buttercms/ButterCMSClient.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,15 @@ private Document readXML(String url) {
245245
}
246246

247247
private String buildURL(final String path, final Map<String, String> queryParameters) {
248-
final URIBuilder uriBuilder = new URIBuilder(URI.create(ButterCMSAPIConfig.API_BASE + path));
248+
String baseUrlFromEnv = System.getenv("API_BASE_URL");
249+
String baseUrl;
250+
if (baseUrlFromEnv != null) {
251+
baseUrl = baseUrlFromEnv;
252+
} else {
253+
baseUrl = ButterCMSAPIConfig.API_BASE_DEFAULT;
254+
}
255+
256+
final URIBuilder uriBuilder = new URIBuilder(URI.create(baseUrl + path));
249257
uriBuilder.addParameter("auth_token", this.authToken);
250258
uriBuilder.addParameter("preview", this.preview.toString());
251259
if (queryParameters != null) {

src/main/java/com/buttercms/config/ButterCMSAPIConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.buttercms.config;
22

33
public class ButterCMSAPIConfig {
4-
public static String API_BASE = "https://api.buttercms.com/v2";
4+
public static String API_BASE_DEFAULT = "https://api.buttercms.com/v2";
55
public static String AUTHORS = "/authors/";
66
public static String CATEGORIES = "/categories/";
77
public static String PAGES = "/pages/";

src/main/java/com/buttercms/model/Page.java

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
package com.buttercms.model;
22

33
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
45

6+
import java.util.Date;
57
import java.util.Objects;
68
import java.util.StringJoiner;
9+
import java.util.Optional;
710

811
@JsonIgnoreProperties(ignoreUnknown = true)
912
public class Page<T> {
1013
private String slug;
11-
private String pageType;
14+
private String name;
1215
private T fields;
16+
private Status status;
17+
private Date updated;
18+
private Optional<String> pageType;
19+
private Optional<Date> published;
20+
private Optional<Date> scheduled;
1321

1422
public String getSlug() {
1523
return slug;
@@ -19,14 +27,6 @@ public void setSlug(String slug) {
1927
this.slug = slug;
2028
}
2129

22-
public String getPageType() {
23-
return pageType;
24-
}
25-
26-
public void setPageType(String pageType) {
27-
this.pageType = pageType;
28-
}
29-
3030
public T getFields() {
3131
return fields;
3232
}
@@ -35,6 +35,54 @@ public void setFields(T fields) {
3535
this.fields = fields;
3636
}
3737

38+
public Status getStatus() {
39+
return status;
40+
}
41+
42+
public void setStatus(Status status) {
43+
this.status = status;
44+
}
45+
46+
public Optional<Date> getScheduled() {
47+
return scheduled;
48+
}
49+
50+
public void setScheduled(Date scheduled) {
51+
this.scheduled = Optional.ofNullable(scheduled);
52+
}
53+
54+
public Date getUpdated() {
55+
return updated;
56+
}
57+
58+
public void setUpdated(Date updated) {
59+
this.updated = updated;
60+
}
61+
62+
public Optional<Date> getPublished() {
63+
return published;
64+
}
65+
66+
public void setPublished(Date published) {
67+
this.published = Optional.ofNullable(published);
68+
}
69+
70+
public String getName() {
71+
return name;
72+
}
73+
74+
public void setName(String name) {
75+
this.name = name;
76+
}
77+
78+
public Optional<String> getPageType() {
79+
return pageType;
80+
}
81+
82+
public void setPageType(String pageType) {
83+
this.pageType = Optional.ofNullable(pageType);
84+
}
85+
3886
@Override
3987
public boolean equals(Object o) {
4088
if (this == o) return true;
@@ -56,4 +104,14 @@ public String toString() {
56104
.add("fields=" + fields)
57105
.toString();
58106
}
107+
108+
public enum Status {
109+
@JsonProperty("draft")
110+
DRAFT,
111+
@JsonProperty("published")
112+
PUBLISHED,
113+
@JsonProperty("scheduled")
114+
SCHEDULED
115+
}
59116
}
117+

src/main/java/com/buttercms/model/Post.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class Post {
1616
private Date created;
1717
private String metaDescription;
1818
private Date published;
19+
private Date scheduled;
1920
private String seoTitle;
2021
private String slug;
2122
private Status status;
@@ -74,6 +75,14 @@ public void setPublished(Date published) {
7475
this.published = published;
7576
}
7677

78+
public Date getScheduled() {
79+
return scheduled;
80+
}
81+
82+
public void setScheduled(Date scheduled) {
83+
this.scheduled = scheduled;
84+
}
85+
7786
public String getSeoTitle() {
7887
return seoTitle;
7988
}
@@ -151,6 +160,7 @@ public boolean equals(Object o) {
151160
Objects.equals(created, post.created) &&
152161
Objects.equals(metaDescription, post.metaDescription) &&
153162
Objects.equals(published, post.published) &&
163+
Objects.equals(scheduled, post.scheduled) &&
154164
Objects.equals(seoTitle, post.seoTitle) &&
155165
Objects.equals(slug, post.slug) &&
156166
status == post.status &&
@@ -164,7 +174,7 @@ public boolean equals(Object o) {
164174

165175
@Override
166176
public int hashCode() {
167-
return Objects.hash(author, body, categories, created, metaDescription, published, seoTitle, slug, status, summary, tags, title, url, featuredImage, featuredImageAlt);
177+
return Objects.hash(author, body, categories, created, metaDescription, published, scheduled, seoTitle, slug, status, summary, tags, title, url, featuredImage, featuredImageAlt);
168178
}
169179

170180
@Override
@@ -188,6 +198,10 @@ public enum Status {
188198
DRAFT,
189199

190200
@JsonProperty("published")
191-
PUBLISHED
201+
PUBLISHED,
202+
203+
@JsonProperty("scheduled")
204+
SCHEDULED
192205
}
193206
}
207+

0 commit comments

Comments
 (0)