Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Songoda update checker and enable Dependabot
- Loading branch information
1 parent
5f16177
commit 1b62d6f
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
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,10 @@ | ||
version: 2 | ||
updates: | ||
- package-ecosystem: "maven" | ||
directory: "/" | ||
schedule: | ||
interval: "daily" | ||
- package-ecosystem: "github-actions" | ||
directory: "/" | ||
schedule: | ||
interval: "daily" |
68 changes: 68 additions & 0 deletions
68
src/main/java/dev/hypera/updatelib/checkers/impl/SongodaUpdateChecker.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,68 @@ | ||
/* | ||
* Copyright (c) 2021 Joshua Sing <joshua@hypera.dev> | ||
* | ||
* Permission to use, copy, modify, and distribute this software for any | ||
* purpose with or without fee is hereby granted, provided that the above | ||
* copyright notice and this permission notice appear in all copies. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
|
||
package dev.hypera.updatelib.checkers.impl; | ||
|
||
import dev.hypera.updatelib.checkers.UpdateChecker; | ||
import dev.hypera.updatelib.data.CheckData; | ||
import dev.hypera.updatelib.exceptions.InvalidResourceException; | ||
import dev.hypera.updatelib.objects.UpdateStatus; | ||
import dev.hypera.updatelib.objects.UpdateStatusBuilder; | ||
import dev.hypera.updatelib.utils.ReaderUtils; | ||
import org.json.simple.JSONArray; | ||
import org.json.simple.JSONObject; | ||
import org.json.simple.JSONValue; | ||
|
||
import javax.net.ssl.HttpsURLConnection; | ||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.net.URL; | ||
|
||
public class SongodaUpdateChecker implements UpdateChecker { | ||
|
||
private static final String URL_FORMAT = "https://songoda.com/api/v2/products/id/%s"; | ||
|
||
/** | ||
* Check for an update using Songoda's API. | ||
* | ||
* @param data Check data. | ||
* | ||
* @return {@link UpdateStatus} | ||
* @throws Exception Any exceptions that occur while checking for updates. | ||
* @since 3.1.0-SNAPSHOT | ||
*/ | ||
@Override | ||
public UpdateStatus check(CheckData data) throws Exception { | ||
URL url = new URL(String.format(URL_FORMAT, data.getResourceId())); | ||
|
||
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection(); | ||
httpsURLConnection.setConnectTimeout(data.getTimeout()); | ||
httpsURLConnection.setReadTimeout(data.getTimeout()); | ||
|
||
if(httpsURLConnection.getResponseCode() == 404) | ||
throw new InvalidResourceException("Cannot find Songoda resource with id '" + data.getResourceId() + "'"); | ||
|
||
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpsURLConnection.getInputStream())); | ||
JSONObject json = (JSONObject) JSONValue.parse(ReaderUtils.readAll(bufferedReader)); | ||
bufferedReader.close(); | ||
|
||
JSONObject songodaData = (JSONObject) json.get("data"); | ||
String distributedVersion = ((JSONObject) ((JSONArray) songodaData.get("versions")).get(0)).get("version").toString(); | ||
|
||
return UpdateStatusBuilder.create(distributedVersion, data.getCurrentVersion()).build(); | ||
} | ||
|
||
} |