Initial commit
This commit is contained in:
parent
8dc2fd2a76
commit
298758030f
38
.gitignore
vendored
Normal file
38
.gitignore
vendored
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
target/
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
!**/src/main/**/target/
|
||||||
|
!**/src/test/**/target/
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea/modules.xml
|
||||||
|
.idea/jarRepositories.xml
|
||||||
|
.idea/compiler.xml
|
||||||
|
.idea/libraries/
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### Eclipse ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
build/
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
### Mac OS ###
|
||||||
|
.DS_Store
|
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
7
.idea/discord.xml
generated
Normal file
7
.idea/discord.xml
generated
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="DiscordProjectSettings">
|
||||||
|
<option name="show" value="PROJECT_FILES" />
|
||||||
|
<option name="description" value="" />
|
||||||
|
</component>
|
||||||
|
</project>
|
7
.idea/encodings.xml
generated
Normal file
7
.idea/encodings.xml
generated
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Encoding">
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||||
|
</component>
|
||||||
|
</project>
|
8
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
8
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<profile version="1.0">
|
||||||
|
<option name="myName" value="Project Default" />
|
||||||
|
<inspection_tool class="AutoCloseableResource" enabled="true" level="WARNING" enabled_by_default="true">
|
||||||
|
<option name="METHOD_MATCHER_CONFIG" value="java.util.Formatter,format,java.io.Writer,append,com.google.common.base.Preconditions,checkNotNull,org.hibernate.Session,close,java.io.PrintWriter,printf,java.io.PrintStream,printf,java.net.http.HttpClient,newHttpClient" />
|
||||||
|
</inspection_tool>
|
||||||
|
</profile>
|
||||||
|
</component>
|
20
.idea/misc.xml
generated
Normal file
20
.idea/misc.xml
generated
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||||
|
<component name="MavenProjectsManager">
|
||||||
|
<option name="originalFiles">
|
||||||
|
<list>
|
||||||
|
<option value="$PROJECT_DIR$/pom.xml" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
<option name="ignoredFiles">
|
||||||
|
<set>
|
||||||
|
<option value="$PROJECT_DIR$/pom.xml" />
|
||||||
|
</set>
|
||||||
|
</option>
|
||||||
|
<option name="workspaceImportForciblyTurnedOn" value="true" />
|
||||||
|
</component>
|
||||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="openjdk-21" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
118
pom.xml
Normal file
118
pom.xml
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<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>dev.marcsync</groupId>
|
||||||
|
<artifactId>marcsync-java-client</artifactId>
|
||||||
|
<version>0.1.0</version>
|
||||||
|
|
||||||
|
<name>marcsync-java-client</name>
|
||||||
|
<description>Java client for the MarcSync API</description>
|
||||||
|
<url>https://github.com/marcsync/marcsync-java-client</url>
|
||||||
|
<developers>
|
||||||
|
<developer>
|
||||||
|
<name>Marcel Lorbeer</name>
|
||||||
|
<email>marcthedev@marcsync.dev</email>
|
||||||
|
<organization>MarcSync</organization>
|
||||||
|
<organizationUrl>https://marcsync.dev</organizationUrl>
|
||||||
|
</developer>
|
||||||
|
</developers>
|
||||||
|
<licenses>
|
||||||
|
<license>
|
||||||
|
<name>MIT License</name>
|
||||||
|
</license>
|
||||||
|
</licenses>
|
||||||
|
<scm>
|
||||||
|
<url>scm:git:git://github.com/marcsync/marcsync-java-client</url>
|
||||||
|
</scm>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<gpg.skip>true</gpg.skip>
|
||||||
|
<maven.compiler.source>21</maven.compiler.source>
|
||||||
|
<maven.compiler.target>21</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.code.gson</groupId>
|
||||||
|
<artifactId>gson</artifactId>
|
||||||
|
<version>2.10.1</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||||
|
<artifactId>jackson-dataformat-xml</artifactId>
|
||||||
|
<version>2.16.0</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.sonatype.central</groupId>
|
||||||
|
<artifactId>central-publishing-maven-plugin</artifactId>
|
||||||
|
<version>0.1.2</version>
|
||||||
|
<extensions>true</extensions>
|
||||||
|
<configuration>
|
||||||
|
<publishingServerId>central</publishingServerId>
|
||||||
|
<tokenEnabled>true</tokenEnabled>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-gpg-plugin</artifactId>
|
||||||
|
<version>3.1.0</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>sign-artifacts</id>
|
||||||
|
<phase>verify</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>sign</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-source-plugin</artifactId>
|
||||||
|
<version>2.2.1</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>attach-sources</id>
|
||||||
|
<goals>
|
||||||
|
<goal>jar-no-fork</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
|
<version>2.9.1</version>
|
||||||
|
<configuration>
|
||||||
|
<javadocExecutable>${java.home}/bin/javadoc</javadocExecutable>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>attach-javadocs</id>
|
||||||
|
<goals>
|
||||||
|
<goal>jar</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<profiles>
|
||||||
|
<profile>
|
||||||
|
<id>release</id>
|
||||||
|
<properties>
|
||||||
|
<gpg.skip>false</gpg.skip>
|
||||||
|
</properties>
|
||||||
|
</profile>
|
||||||
|
</profiles>
|
||||||
|
|
||||||
|
</project>
|
74
src/main/java/MarcSync/Client.java
Normal file
74
src/main/java/MarcSync/Client.java
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
package MarcSync;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.*;
|
||||||
|
|
||||||
|
public class Client {
|
||||||
|
|
||||||
|
private final String _accessToken;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new MarcSync client.
|
||||||
|
* @param accessToken The access token to use for communication with MarcSync
|
||||||
|
*/
|
||||||
|
public Client(String accessToken) {
|
||||||
|
_accessToken = accessToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param collectionName The name of the collection to use
|
||||||
|
* @return A new instance of the MarcSync collection
|
||||||
|
*
|
||||||
|
* @see Collection
|
||||||
|
*/
|
||||||
|
public Collection getCollection(String collectionName) {
|
||||||
|
return new Collection(_accessToken, collectionName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param collectionName The name of the collection to use
|
||||||
|
* @return A new instance of the MarcSync collection
|
||||||
|
*
|
||||||
|
* @throws IOException
|
||||||
|
* @throws URISyntaxException
|
||||||
|
* @see Collection
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* Note: This method is useful if you want to fetch the collection from the server to check if it exists before using it.
|
||||||
|
* </p>
|
||||||
|
*/
|
||||||
|
public Collection fetchCollection(String collectionName) throws IOException, URISyntaxException {
|
||||||
|
URL url = new URI("https://api.marcsync.dev/v0/collection/" + collectionName).toURL();
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
|
connection.setRequestProperty("accept", "application/json");
|
||||||
|
connection.setRequestProperty("authorization", _accessToken);
|
||||||
|
|
||||||
|
if (connection.getResponseCode() != 200) {
|
||||||
|
throw new IOException("Failed to fetch collection: " + connection.getResponseCode() + " " + connection.getResponseMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Collection(_accessToken, collectionName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param collectionName The name of the collection to create
|
||||||
|
* @return A new instance of the MarcSync collection
|
||||||
|
*
|
||||||
|
* @throws IOException
|
||||||
|
* @throws URISyntaxException
|
||||||
|
*/
|
||||||
|
public Collection createCollection(String collectionName) throws IOException, URISyntaxException {
|
||||||
|
URL url = new URI("https://api.marcsync.dev/v0/collection/" + collectionName).toURL();
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
|
connection.setRequestMethod("POST");
|
||||||
|
connection.setRequestProperty("accept", "application/json");
|
||||||
|
connection.setRequestProperty("authorization", _accessToken);
|
||||||
|
|
||||||
|
if (connection.getResponseCode() != 200) {
|
||||||
|
throw new IOException("Failed to create collection: " + connection.getResponseCode() + " " + connection.getResponseMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Collection(_accessToken, collectionName);
|
||||||
|
}
|
||||||
|
}
|
314
src/main/java/MarcSync/Collection.java
Normal file
314
src/main/java/MarcSync/Collection.java
Normal file
@ -0,0 +1,314 @@
|
|||||||
|
package MarcSync;
|
||||||
|
|
||||||
|
import MarcSync.classes.*;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||||
|
import com.google.gson.*;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.*;
|
||||||
|
import java.net.http.HttpClient;
|
||||||
|
import java.net.http.HttpRequest;
|
||||||
|
import java.net.http.HttpResponse;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class Collection {
|
||||||
|
|
||||||
|
private final String _accessToken;
|
||||||
|
private final String _collectionName;
|
||||||
|
private final ObjectMapper _mapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new instance of the MarcSync collection
|
||||||
|
* @param accessToken The access token to use for communication with MarcSync
|
||||||
|
* @param collectionName The name of the collection to use
|
||||||
|
*
|
||||||
|
* @see Client
|
||||||
|
**/
|
||||||
|
public Collection(String accessToken, String collectionName) {
|
||||||
|
_accessToken = accessToken;
|
||||||
|
_collectionName = collectionName;
|
||||||
|
|
||||||
|
_mapper = new ObjectMapper();
|
||||||
|
_mapper.enable(SerializationFeature.INDENT_OUTPUT);
|
||||||
|
_mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Warning:
|
||||||
|
* @throws URISyntaxException
|
||||||
|
* @throws IOException
|
||||||
|
* <p>
|
||||||
|
* Note: This method will delete the collection and all of its entries. This action cannot be undone.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public void drop() throws URISyntaxException, IOException {
|
||||||
|
URL url = new URI("https://api.marcsync.dev/v0/collection/" + _collectionName).toURL();
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
|
connection.setRequestMethod("DELETE");
|
||||||
|
connection.setRequestProperty("accept", "application/json");
|
||||||
|
connection.setRequestProperty("authorization", _accessToken);
|
||||||
|
|
||||||
|
if (connection.getResponseCode() != 200) {
|
||||||
|
throw new IOException("Failed to drop collection: " + connection.getResponseCode() + " " + connection.getResponseMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The name of the collection
|
||||||
|
*/
|
||||||
|
public String setName(String name) throws URISyntaxException, IOException {
|
||||||
|
URL url = new URI("https://api.marcsync.dev/v0/collection/" + _collectionName).toURL();
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
|
connection.setRequestMethod("PUT");
|
||||||
|
connection.setRequestProperty("accept", "application/json");
|
||||||
|
connection.setRequestProperty("authorization", _accessToken);
|
||||||
|
connection.setRequestProperty("content-type", "application/json");
|
||||||
|
connection.setDoOutput(true);
|
||||||
|
|
||||||
|
try (OutputStream outputStream = connection.getOutputStream()) {
|
||||||
|
outputStream.write(new Gson().toJson(new CollectionUpdatePayload(name)).getBytes(StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (connection.getResponseCode() != 200) {
|
||||||
|
throw new IOException("Failed to rename collection: " + connection.getResponseCode() + " " + connection.getResponseMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The name of the collection
|
||||||
|
*/
|
||||||
|
public String getName() {
|
||||||
|
return _collectionName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return Whether or not the collection exists
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* Note: This method is useful if you want to fetch the collection from the server to check if it exists before using it.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public boolean exists() {
|
||||||
|
try {
|
||||||
|
URL url = new URI("https://api.marcsync.dev/v0/collection/" + _collectionName).toURL();
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
|
connection.setRequestMethod("GET");
|
||||||
|
connection.setRequestProperty("accept", "application/json");
|
||||||
|
connection.setRequestProperty("authorization", _accessToken);
|
||||||
|
|
||||||
|
return connection.getResponseCode() == 200;
|
||||||
|
} catch (Exception e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an entry in the collection
|
||||||
|
*
|
||||||
|
* @return A new instance of the MarcSync entry
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public Entry createEntry(EntryData entryData) throws URISyntaxException, IOException {
|
||||||
|
URL url = new URI("https://api.marcsync.dev/v0/entries/" + _collectionName).toURL();
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
|
connection.setRequestMethod("POST");
|
||||||
|
connection.setRequestProperty("accept", "application/json");
|
||||||
|
connection.setRequestProperty("authorization", _accessToken);
|
||||||
|
connection.setRequestProperty("content-type", "application/json");
|
||||||
|
connection.setDoOutput(true);
|
||||||
|
|
||||||
|
try (OutputStream outputStream = connection.getOutputStream()) {
|
||||||
|
outputStream.write(_mapper.writeValueAsString(new EntryDataPayload(entryData)).getBytes(StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (connection.getResponseCode() != 200) {
|
||||||
|
throw new IOException("Failed to create entry: " + connection.getResponseCode() + " " + connection.getResponseMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Entry(_accessToken, _collectionName, entryData);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return The entry with the specified ID
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public Entry getEntryById(String id) throws URISyntaxException, IOException, InterruptedException {
|
||||||
|
HttpClient client = HttpClient.newHttpClient();
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.method("GET", HttpRequest.BodyPublishers.ofString(_mapper.writeValueAsString(new EntryFilterPayload(new EntryData() {{
|
||||||
|
put("_id", id);
|
||||||
|
}}))))
|
||||||
|
.uri(new URI("https://api.marcsync.dev/v1/entries/" + _collectionName))
|
||||||
|
.header("accept", "application/json")
|
||||||
|
.header("authorization", _accessToken)
|
||||||
|
.header("content-type", "application/json")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
if (response.statusCode() != 200) {
|
||||||
|
throw new IOException("Failed to get entry: " + response.statusCode() + " " + response.body());
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Entry(_accessToken, _collectionName, new Gson().fromJson(response.body(), EntryResponse.class).entries[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return The entries with the specified filter
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* Note: This method is useful if you want to fetch multiple entries from the server at once.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @see Entry
|
||||||
|
* @see EntryData
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public Entry[] getEntries(EntryData filters) throws IOException, InterruptedException, URISyntaxException {
|
||||||
|
HttpClient client = HttpClient.newHttpClient();
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.method("GET", HttpRequest.BodyPublishers.ofString(_mapper.writeValueAsString(new EntryFilterPayload(filters))))
|
||||||
|
.uri(new URI("https://api.marcsync.dev/v1/entries/" + _collectionName))
|
||||||
|
.header("accept", "application/json")
|
||||||
|
.header("authorization", _accessToken)
|
||||||
|
.header("content-type", "application/json")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
if (response.statusCode() != 200) {
|
||||||
|
throw new IOException("Failed to get entries: " + response.statusCode() + " " + response.body());
|
||||||
|
}
|
||||||
|
|
||||||
|
return Arrays.stream(new Gson().fromJson(response.body(), EntryResponse.class).entries).map(entryData -> new Entry(_accessToken, _collectionName, entryData)).toArray(Entry[]::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Deletes the entry with the specified ID
|
||||||
|
* <p>
|
||||||
|
* Note: Will delete the entry from the collection. This action cannot be undone.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public void deleteEntryById(String id) throws URISyntaxException, IOException {
|
||||||
|
URL url = new URI("https://api.marcsync.dev/v1/entries/" + _collectionName).toURL();
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
|
connection.setRequestMethod("DELETE");
|
||||||
|
connection.setRequestProperty("accept", "application/json");
|
||||||
|
connection.setRequestProperty("authorization", _accessToken);
|
||||||
|
connection.setRequestProperty("content-type", "application/json");
|
||||||
|
connection.setDoOutput(true);
|
||||||
|
|
||||||
|
try (OutputStream outputStream = connection.getOutputStream()) {
|
||||||
|
outputStream.write(new Gson().toJson(new EntryFilterPayload(new EntryData() {{
|
||||||
|
put("_id", id);
|
||||||
|
}})).getBytes(StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (connection.getResponseCode() != 200) {
|
||||||
|
throw new IOException("Failed to delete entry: " + connection.getResponseCode() + " " + connection.getResponseMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Deletes the entries matching with the specified filter
|
||||||
|
* <p>
|
||||||
|
* Note: Will delete the matching entries from the collection. This action cannot be undone.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @see EntryData
|
||||||
|
*/
|
||||||
|
public void deleteEntries(EntryData filters) throws URISyntaxException, IOException {
|
||||||
|
URL url = new URI("https://api.marcsync.dev/v1/entries/" + _collectionName).toURL();
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
|
connection.setRequestMethod("DELETE");
|
||||||
|
connection.setRequestProperty("accept", "application/json");
|
||||||
|
connection.setRequestProperty("authorization", _accessToken);
|
||||||
|
connection.setRequestProperty("content-type", "application/json");
|
||||||
|
connection.setDoOutput(true);
|
||||||
|
|
||||||
|
try (OutputStream outputStream = connection.getOutputStream()) {
|
||||||
|
outputStream.write(_mapper.writeValueAsString(new EntryFilterPayload(filters)).getBytes(StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (connection.getResponseCode() != 200) {
|
||||||
|
throw new IOException("Failed to delete entries: " + connection.getResponseCode() + " " + connection.getResponseMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Updates the entry with the specified ID
|
||||||
|
*
|
||||||
|
* @see Entry
|
||||||
|
* @see EntryData
|
||||||
|
*/
|
||||||
|
public void updateEntryById(String id, EntryData entryData) throws URISyntaxException, IOException {
|
||||||
|
URL url = new URI("https://api.marcsync.dev/v1/entries/" + _collectionName).toURL();
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
|
connection.setRequestMethod("PUT");
|
||||||
|
connection.setRequestProperty("accept", "application/json");
|
||||||
|
connection.setRequestProperty("authorization", _accessToken);
|
||||||
|
connection.setRequestProperty("content-type", "application/json");
|
||||||
|
connection.setDoOutput(true);
|
||||||
|
|
||||||
|
try (OutputStream outputStream = connection.getOutputStream()) {
|
||||||
|
outputStream.write(_mapper.writeValueAsString(new EntryUpdatePayload(new EntryData() {{
|
||||||
|
put("_id", id);
|
||||||
|
}}, entryData)).getBytes(StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (connection.getResponseCode() != 200) {
|
||||||
|
throw new IOException("Failed to update entry: " + connection.getResponseCode() + " " + connection.getResponseMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Updates the entries matching with the specified filter
|
||||||
|
*
|
||||||
|
* @see Entry
|
||||||
|
* @see EntryData
|
||||||
|
*/
|
||||||
|
public void updateEntries(EntryData filters, EntryData entryData) throws URISyntaxException, IOException {
|
||||||
|
URL url = new URI("https://api.marcsync.dev/v1/entries/" + _collectionName).toURL();
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
|
connection.setRequestMethod("PUT");
|
||||||
|
connection.setRequestProperty("accept", "application/json");
|
||||||
|
connection.setRequestProperty("authorization", _accessToken);
|
||||||
|
connection.setRequestProperty("content-type", "application/json");
|
||||||
|
connection.setDoOutput(true);
|
||||||
|
|
||||||
|
try (OutputStream outputStream = connection.getOutputStream()) {
|
||||||
|
outputStream.write(_mapper.writeValueAsString(new EntryUpdatePayload(filters, entryData)).getBytes(StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (connection.getResponseCode() != 200) {
|
||||||
|
throw new IOException("Failed to update entries: " + connection.getResponseCode() + " " + connection.getResponseMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String readResponse(InputStream inputStream) throws IOException {
|
||||||
|
StringBuilder response = new StringBuilder();
|
||||||
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
|
||||||
|
String line;
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
response.append(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return response.toString();
|
||||||
|
}
|
||||||
|
}
|
169
src/main/java/MarcSync/Entry.java
Normal file
169
src/main/java/MarcSync/Entry.java
Normal file
@ -0,0 +1,169 @@
|
|||||||
|
package MarcSync;
|
||||||
|
|
||||||
|
import MarcSync.classes.EntryData;
|
||||||
|
import MarcSync.classes.EntryFilterPayload;
|
||||||
|
import MarcSync.classes.EntryUpdatePayload;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.*;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
public class Entry {
|
||||||
|
|
||||||
|
private final String _accessToken;
|
||||||
|
private final String _collectionName;
|
||||||
|
private final EntryData _entryData;
|
||||||
|
private final ObjectMapper _mapper;
|
||||||
|
|
||||||
|
public Entry(String accessToken, String collectionName, EntryData entryData) {
|
||||||
|
_accessToken = accessToken;
|
||||||
|
_collectionName = collectionName;
|
||||||
|
_entryData = entryData;
|
||||||
|
|
||||||
|
_mapper = new ObjectMapper();
|
||||||
|
_mapper.enable(SerializationFeature.INDENT_OUTPUT);
|
||||||
|
_mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return The EntryData object of the entry
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* Note: This method is useful if you want to get the values of the entry.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @see EntryData
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public EntryData getValues() {
|
||||||
|
return _entryData;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param key The key of the value to get
|
||||||
|
* @return The value of the specified key
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* Note: This method is useful if you want to get the value of a specific key without specifying the type.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @see EntryData
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public Object getValue(String key) {
|
||||||
|
return _entryData.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return The name of the collection of the entry
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public String getCollectionName() {
|
||||||
|
return _collectionName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param key he key of the value to update
|
||||||
|
* @param value The value to update
|
||||||
|
* @return The values of the entry after update
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* Note: This method is useful if you want to update the value of a specific key.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public EntryData updateValue(String key, Object value) throws URISyntaxException, IOException {
|
||||||
|
URL url = new URI("https://api.marcsync.dev/v1/entries/" + _collectionName).toURL();
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
|
connection.setRequestMethod("PUT");
|
||||||
|
connection.setRequestProperty("accept", "application/json");
|
||||||
|
connection.setRequestProperty("authorization", _accessToken);
|
||||||
|
connection.setRequestProperty("content-type", "application/json");
|
||||||
|
connection.setDoOutput(true);
|
||||||
|
|
||||||
|
try (OutputStream outputStream = connection.getOutputStream()) {
|
||||||
|
outputStream.write(_mapper.writeValueAsString(new EntryUpdatePayload(new EntryData() {{
|
||||||
|
put("_id", _entryData.get("_id"));
|
||||||
|
}}, new EntryData() {{
|
||||||
|
put(key, value);
|
||||||
|
}})).getBytes(StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (connection.getResponseCode() != 200) {
|
||||||
|
throw new IOException("Failed to update entry: " + connection.getResponseCode() + " " + connection.getResponseMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return _entryData;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param entryData The values to update
|
||||||
|
* @return The values of the entry after update
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* Note: This method is useful if you want to update multiple values of the entry.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @see EntryData
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public EntryData updateValues(EntryData entryData) throws URISyntaxException, IOException {
|
||||||
|
URL url = new URI("https://api.marcsync.dev/v1/entries/" + _collectionName).toURL();
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
|
connection.setRequestMethod("PUT");
|
||||||
|
connection.setRequestProperty("accept", "application/json");
|
||||||
|
connection.setRequestProperty("authorization", _accessToken);
|
||||||
|
connection.setRequestProperty("content-type", "application/json");
|
||||||
|
connection.setDoOutput(true);
|
||||||
|
|
||||||
|
try (OutputStream outputStream = connection.getOutputStream()) {
|
||||||
|
outputStream.write(_mapper.writeValueAsString(new EntryUpdatePayload(new EntryData() {{
|
||||||
|
put("_id", _entryData.get("_id"));
|
||||||
|
}}, entryData)).getBytes(StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (connection.getResponseCode() != 200) {
|
||||||
|
throw new IOException("Failed to update entry: " + connection.getResponseCode() + " " + connection.getResponseMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return _entryData;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Deletes the entry
|
||||||
|
* <p>
|
||||||
|
* Note: Will delete the entry from the collection. This action cannot be undone.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public void delete() throws IOException, URISyntaxException {
|
||||||
|
URL url = new URI("https://api.marcsync.dev/v1/entries/" + _collectionName).toURL();
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
|
connection.setRequestMethod("DELETE");
|
||||||
|
connection.setRequestProperty("accept", "application/json");
|
||||||
|
connection.setRequestProperty("authorization", _accessToken);
|
||||||
|
connection.setRequestProperty("content-type", "application/json");
|
||||||
|
connection.setDoOutput(true);
|
||||||
|
|
||||||
|
try (OutputStream outputStream = connection.getOutputStream()) {
|
||||||
|
outputStream.write(_mapper.writeValueAsString(new EntryFilterPayload(new EntryData() {{
|
||||||
|
put("_id", _entryData.get("_id"));
|
||||||
|
}})).getBytes(StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (connection.getResponseCode() != 200) {
|
||||||
|
throw new IOException("Failed to delete entry: " + connection.getResponseCode() + " " + connection.getResponseMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package MarcSync.classes;
|
||||||
|
|
||||||
|
public class CollectionUpdatePayload {
|
||||||
|
public String collectionName;
|
||||||
|
|
||||||
|
public CollectionUpdatePayload(String collectionName) {
|
||||||
|
this.collectionName = collectionName;
|
||||||
|
}
|
||||||
|
}
|
11
src/main/java/MarcSync/classes/EntryData.java
Normal file
11
src/main/java/MarcSync/classes/EntryData.java
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package MarcSync.classes;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
public class EntryData extends HashMap<String, Object> {
|
||||||
|
public static EntryData fromJson(String json) {
|
||||||
|
return new Gson().fromJson(json, EntryData.class);
|
||||||
|
}
|
||||||
|
}
|
9
src/main/java/MarcSync/classes/EntryDataPayload.java
Normal file
9
src/main/java/MarcSync/classes/EntryDataPayload.java
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package MarcSync.classes;
|
||||||
|
|
||||||
|
public class EntryDataPayload {
|
||||||
|
public EntryData data;
|
||||||
|
|
||||||
|
public EntryDataPayload(EntryData entryData) {
|
||||||
|
this.data = entryData;
|
||||||
|
}
|
||||||
|
}
|
9
src/main/java/MarcSync/classes/EntryFilterPayload.java
Normal file
9
src/main/java/MarcSync/classes/EntryFilterPayload.java
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package MarcSync.classes;
|
||||||
|
|
||||||
|
public class EntryFilterPayload {
|
||||||
|
public EntryData filters;
|
||||||
|
|
||||||
|
public EntryFilterPayload(EntryData entryData) {
|
||||||
|
this.filters = entryData;
|
||||||
|
}
|
||||||
|
}
|
5
src/main/java/MarcSync/classes/EntryResponse.java
Normal file
5
src/main/java/MarcSync/classes/EntryResponse.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package MarcSync.classes;
|
||||||
|
|
||||||
|
public class EntryResponse {
|
||||||
|
public EntryData[] entries;
|
||||||
|
}
|
11
src/main/java/MarcSync/classes/EntryUpdatePayload.java
Normal file
11
src/main/java/MarcSync/classes/EntryUpdatePayload.java
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package MarcSync.classes;
|
||||||
|
|
||||||
|
public class EntryUpdatePayload {
|
||||||
|
public EntryData filters;
|
||||||
|
public EntryData data;
|
||||||
|
|
||||||
|
public EntryUpdatePayload(EntryData filters, EntryData data) {
|
||||||
|
this.filters = filters;
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user