Skip to content

Commit

Permalink
Add Songoda update checker and enable Dependabot
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuasing committed May 18, 2021
1 parent 5f16177 commit 1b62d6f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .github/dependabot.yml
@@ -0,0 +1,10 @@
version: 2
updates:
- package-ecosystem: "maven"
directory: "/"
schedule:
interval: "daily"
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
@@ -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();
}

}

0 comments on commit 1b62d6f

Please sign in to comment.