-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from Suwayomi/serverVersionApi
Added Meta Info API Service for Suwayomi
- Loading branch information
Showing
4 changed files
with
232 additions
and
13 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
src/main/java/online/hatsunemiku/tachideskvaadinui/data/tachidesk/ServerVersion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package online.hatsunemiku.tachideskvaadinui.data.tachidesk; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import lombok.Getter; | ||
|
||
/** Represents the version of the Suwayomi Server. */ | ||
@Getter | ||
public class ServerVersion { | ||
|
||
/** The version of the server. Format: vX.Y.Z */ | ||
private final String version; | ||
|
||
/** The revision of the server. Format: r1234 */ | ||
private final String revision; | ||
|
||
/** | ||
* Creates a new instance of the {@link ServerVersion} class. | ||
* | ||
* @param version the version of the server | ||
* @param revision the revision of the server | ||
*/ | ||
// Private constructor so only Spring can create instances of this class | ||
@JsonCreator | ||
private ServerVersion(String version, String revision) { | ||
this.version = version; | ||
this.revision = revision; | ||
} | ||
|
||
/** | ||
* Gets the major version of the server. For example if the version is v1.2.3, this method will | ||
* return 1. | ||
* | ||
* @return the major version of the server | ||
*/ | ||
public int getMajorVersion() { | ||
String majorVersion = version.substring(1, version.indexOf('.')); | ||
return Integer.parseInt(majorVersion); | ||
} | ||
|
||
/** | ||
* Gets the minor version of the server. For example if the version is v1.2.3, this method will | ||
* return 2. | ||
* | ||
* @return the minor version of the server | ||
*/ | ||
public int getMinorVersion() { | ||
int firstDot = version.indexOf('.'); | ||
int secondDot = version.indexOf('.', firstDot + 1); | ||
String minorVersion = version.substring(firstDot + 1, secondDot); | ||
return Integer.parseInt(minorVersion); | ||
} | ||
|
||
/** | ||
* Gets the patch version of the server. For example if the version is v1.2.3, this method will | ||
* return 3. | ||
* | ||
* @return the patch version of the server | ||
*/ | ||
public int getPatchVersion() { | ||
int lastDot = version.lastIndexOf('.'); | ||
String patchVersion = version.substring(lastDot + 1); | ||
return Integer.parseInt(patchVersion); | ||
} | ||
|
||
/** | ||
* Gets the revision number of the server. For example if the revision is r1234, this method will | ||
* return 1234. | ||
* | ||
* @return the revision number of the server | ||
*/ | ||
public int getRevisionNumber() { | ||
String revisionNumber = revision.substring(1); | ||
return Integer.parseInt(revisionNumber); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/online/hatsunemiku/tachideskvaadinui/services/SuwayomiService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package online.hatsunemiku.tachideskvaadinui.services; | ||
|
||
import java.util.Optional; | ||
import online.hatsunemiku.tachideskvaadinui.data.tachidesk.ServerVersion; | ||
import online.hatsunemiku.tachideskvaadinui.services.client.suwayomi.SuwayomiMetaClient; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** | ||
* Responsible for retrieving data from the Suwayomi Server. This class is an abstraction over the | ||
* {@link SuwayomiMetaClient} class and provides methods for retrieving data from the Suwayomi | ||
* Server. | ||
*/ | ||
@Service | ||
public class SuwayomiService { | ||
|
||
private final SuwayomiMetaClient metaClient; | ||
|
||
/** | ||
* Creates a new instance of the {@link SuwayomiService} class. | ||
* | ||
* @param metaClient the {@link SuwayomiMetaClient} used for retrieving data from the Suwayomi | ||
* Server. | ||
*/ | ||
public SuwayomiService(SuwayomiMetaClient metaClient) { | ||
this.metaClient = metaClient; | ||
} | ||
|
||
/** | ||
* Retrieves the version of the Suwayomi Server. This method will block until the response is | ||
* received. | ||
* | ||
* @return either the version of the Suwayomi Server if successful or an empty {@link Optional} if | ||
* an error occurred. | ||
*/ | ||
public Optional<ServerVersion> getServerVersion() { | ||
try { | ||
return Optional.of(metaClient.getServerVersion()); | ||
} catch (Exception e) { | ||
return Optional.empty(); | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...ava/online/hatsunemiku/tachideskvaadinui/services/client/suwayomi/SuwayomiMetaClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package online.hatsunemiku.tachideskvaadinui.services.client.suwayomi; | ||
|
||
import online.hatsunemiku.tachideskvaadinui.data.tachidesk.ServerVersion; | ||
import online.hatsunemiku.tachideskvaadinui.services.WebClientService; | ||
import org.intellij.lang.annotations.Language; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** Retrieves metadata about the Suwayomi Server through its API. */ | ||
@Component | ||
public class SuwayomiMetaClient { | ||
|
||
private final WebClientService webClientService; | ||
|
||
/** | ||
* Creates a new instance of the {@link SuwayomiMetaClient} class. | ||
* | ||
* @param webClientService the {@link WebClientService} used for making API requests to the | ||
* Suwayomi Server. | ||
*/ | ||
public SuwayomiMetaClient(WebClientService webClientService) { | ||
this.webClientService = webClientService; | ||
} | ||
|
||
/** | ||
* Retrieves the version of the Suwayomi Server though its API. This method will block until the | ||
* response is received. | ||
* | ||
* @return the version of the Suwayomi Server. | ||
*/ | ||
public ServerVersion getServerVersion() { | ||
var client = webClientService.getDgsGraphQlClient(); | ||
|
||
@Language("graphql") | ||
String query = | ||
""" | ||
query { | ||
aboutServer { | ||
version | ||
revision | ||
} | ||
} | ||
"""; | ||
|
||
var response = client.reactiveExecuteQuery(query).block(); | ||
|
||
if (response == null) { | ||
throw new RuntimeException("Failed to retrieve server version"); | ||
} | ||
|
||
return response.extractValueAsObject("aboutServer", ServerVersion.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters