Skip to content
Open
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
23 changes: 22 additions & 1 deletion Paubox_Java/paubox-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,27 @@
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>

<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20211205</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>

</dependencies>
</project>
49 changes: 49 additions & 0 deletions Paubox_Java/paubox-java/src/com/paubox/data/DynamicTemplate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

package com.paubox.data;

public class DynamicTemplate {
private int id;
private String name;
private String content;

public DynamicTemplate(int id, String name) {
this.id = id;
this.name = name;
}

public DynamicTemplate(String name, String content) {
this.name = name;
this.content = content;
}

public DynamicTemplate(int id, String name, String content) {
this.id = id;
this.name = name;
this.content = content;
}

public int getId() {
return id;
}

public String getName() {
return name;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

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

public void setId(int id) {
this.id = id;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package com.paubox.service;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.paubox.common.Constants;
import com.paubox.data.DynamicTemplate;

public class DynamicTemplateService {

private String baseApiUrl = "https://api.paubox.net/v1/" + Constants.API_USER + "/";

public List<DynamicTemplate> getAllDynamicTemplates() throws IOException {
// Set API endpoint URL
String url = baseApiUrl + "dynamic_templates";

// Set request headers
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", getAuthorizationHeader());

// Create HTTP client and request object
HttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
// headers.forEach(httpGet::setHeader);
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpGet.setHeader(entry.getKey(), entry.getValue());
}

// Make API request
HttpResponse response = httpClient.execute(httpGet);

// Handle API response
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200 || statusCode == 201) {
HttpEntity entity = response.getEntity();
String responseBody = EntityUtils.toString(entity);
JSONArray jsonArray = new JSONArray(responseBody);
List<DynamicTemplate> templates = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
int templateId = jsonObject.getInt("id");
String templateName = jsonObject.getString("name");
DynamicTemplate template = new DynamicTemplate(templateId, templateName);
templates.add(template);
}
return templates;
} else {
throw new IOException("Paubox API Request Error: " + response.getStatusLine().getReasonPhrase());
}
}

public String createDynamicTemplate(String templateName, File templateFile) throws IOException {
String url = baseApiUrl + "dynamic_templates";
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", getAuthorizationHeader());

HttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpPost.setHeader(entry.getKey(), entry.getValue());
}
FileBody templateBody = new FileBody(templateFile, ContentType.TEXT_PLAIN);
HttpEntity requestEntity = MultipartEntityBuilder.create()
.addTextBody("data[name]", templateName)
.addPart("data[body]", templateBody)
.build();
httpPost.setEntity(requestEntity);
HttpResponse response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200 || statusCode == 201) {
HttpEntity entity = response.getEntity();
String responseBody = EntityUtils.toString(entity);
JSONObject jsonObject = new JSONObject(responseBody);
String message = jsonObject.getString("message");
return "Message: " + message;
} else {
throw new IOException("Paubox API Request Error: " + response.getStatusLine().getReasonPhrase());
}
}

public String updateDynamicTemplate(String templateId, String templateName, File templateFile) throws IOException {
String url = baseApiUrl + "dynamic_templates/" + templateId;
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", getAuthorizationHeader());

HttpClient httpClient = HttpClients.createDefault();
HttpPatch httpPatch = new HttpPatch(url);
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpPatch.setHeader(entry.getKey(), entry.getValue());
}
FileBody templateBody = new FileBody(templateFile, ContentType.TEXT_PLAIN);
HttpEntity requestEntity = MultipartEntityBuilder.create()
.addTextBody("data[name]", templateName)
.addPart("data[body]", templateBody)
.build();
httpPatch.setEntity(requestEntity);
HttpResponse response = httpClient.execute(httpPatch);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200 || statusCode == 201) {
HttpEntity entity = response.getEntity();
String responseBody = EntityUtils.toString(entity);
JSONObject jsonObject = new JSONObject(responseBody);
String message = jsonObject.getString("message");
return "Message: " + message;
} else {
throw new IOException("Paubox API Request Error: " + response.getStatusLine().getReasonPhrase());
}
}

public String deleteDynamicTemplate(String templateId) throws IOException {
String url = baseApiUrl + "dynamic_templates/" + templateId;
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", getAuthorizationHeader());

HttpClient httpClient = HttpClients.createDefault();
HttpDelete httpDelete = new HttpDelete(url);
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpDelete.setHeader(entry.getKey(), entry.getValue());
}
HttpResponse response = httpClient.execute(httpDelete);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200 || statusCode == 201) {
HttpEntity entity = response.getEntity();
String responseBody = EntityUtils.toString(entity);
JSONObject jsonObject = new JSONObject(responseBody);
String message = jsonObject.getString("message");
return "Message: " + message;
} else {
throw new IOException("Paubox API Request Error: " + response.getStatusLine().getReasonPhrase());
}
}

public DynamicTemplate getDynamicTemplate(String templateId) throws IOException {
String url = baseApiUrl + "dynamic_templates/" + templateId;
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", getAuthorizationHeader());

HttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpGet.setHeader(entry.getKey(), entry.getValue());
}
HttpResponse response = httpClient.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200 || statusCode == 201) {
HttpEntity entity = response.getEntity();
String responseBody = EntityUtils.toString(entity);
JSONObject jsonObject = new JSONObject(responseBody);
int templateIdFromAPI = jsonObject.getInt("id");
String templateName = jsonObject.getString("name");
DynamicTemplate template = new DynamicTemplate(templateIdFromAPI, templateName);
template.setContent(jsonObject.getString("body"));
return template;
} else {
throw new IOException("Paubox API Request Error: " + response.getStatusLine().getReasonPhrase());
}
}

private static String getAuthorizationHeader() {
return "Token token=" + Constants.API_KEY;
}
}
Loading