Schedule-Creator

Java GUI to create schedules for UNCG students
git clone git://git.wrycode.com/wrycode/archive/Schedule-Creator.git
Log | Files | Refs | README

commit b351e20ec739e7fbcd49e397de9f79e477e48db2
parent 47ae032adb14749ef10aa18b4331cdc0da847c4b
Author: Ilyass Sfar <42755577+cloud0x9@users.noreply.github.com>
Date:   Sun, 29 Mar 2020 10:36:13 -0400

Setup basic API (#45)

* Add API package and interface

* Add API call method and Adapter

* Change which file TIME and DAY use
Diffstat:
AScheduleCreator/API/ApiAdapter.java | 28++++++++++++++++++++++++++++
AScheduleCreator/API/ApiInterface.java | 21+++++++++++++++++++++
AScheduleCreator/API/YesNoApi.java | 76++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
MScheduleCreator/Tests.java | 21++++++++++++++++-----
MScheduleCreator/Translator.java | 4++--
5 files changed, 143 insertions(+), 7 deletions(-)

diff --git a/ScheduleCreator/API/ApiAdapter.java b/ScheduleCreator/API/ApiAdapter.java @@ -0,0 +1,27 @@ +package ScheduleCreator.API; + +/** + * Adapter class for "YesNoApi", so it doesn't have to be called directly. + * + * @author Ilyass Sfar + * + * Last Updated: 3/27/2020 + */ + + +public class ApiAdapter implements ApiInterface { + + protected static final ApiInterface thisApi = new YesNoApi(); + + /** + * + * @param _decision The decision the API returns can be forced to a specific choice + * this is left blank here, so the API makes its own choice. + * @return + */ + @Override + public String decisionForce(String _decision) { + return ApiAdapter.thisApi.decisionForce(_decision); + } + +}+ \ No newline at end of file diff --git a/ScheduleCreator/API/ApiInterface.java b/ScheduleCreator/API/ApiInterface.java @@ -0,0 +1,21 @@ +package ScheduleCreator.API; + +/** + * Interface for the "YesNoApi" that sets a standard for any calls made to the API. + * + * @author Ilyass Sfar + * + * Last Updated: 3/27/2020 + */ + +public interface ApiInterface { + /** + * The API has only one field that is also optional, the field is left blank. + * + * @param _decision The decision the API returns can be forced to a specific choice + * this is left blank here, so the API makes its own choice. + * @return + */ + public String decisionForce(String _decision); + +} diff --git a/ScheduleCreator/API/YesNoApi.java b/ScheduleCreator/API/YesNoApi.java @@ -0,0 +1,76 @@ +package ScheduleCreator.API; + +/** + * Makes direct calls to that "YesNoApi" using https calls, and JSON to extract + * fields. + * + * @author Ilyass Sfar + * + * Last Updated: 3/27/2020 + */ +import java.io.IOException; +import java.net.URL; +import java.util.Scanner; +import javax.net.ssl.HttpsURLConnection; +//https://github.com/stleary/JSON-java +import org.json.JSONObject; + +public class YesNoApi implements ApiInterface { + + //refrence https://yesno.wtf#api for api docs + //base url for api calls + private static final String baseURL = "https://yesno.wtf/api/?force={"; + + @Override + public String decisionForce(String _decision) { + //inline will store the JSON data streamed in string format + String inline = ""; + //placeholder for answer from the api + String answer = null; + try { + //add a forced decision if applicable + URL url = new URL(baseURL + _decision + "}"); + + //set protocols being used, I ran into some issues and added this, but currently not needed + //System.setProperty("https.protocols", "TLSv1.1,TLSv1.2"); + //Pass url so the connection can be set up + HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); + //Set the request for api to GET + connection.setRequestMethod("GET"); + //make this connection to the api website + connection.connect(); + //Get the response status of the API, used to failsafe + int responsecode = connection.getResponseCode(); + System.out.println("Response code is: " + responsecode); + + /** + * If response code is not 200, something went wrong, throw + * Exception and fail safely else get the JSON from the API and + * extract necessary fields + */ + if (responsecode != 200) { + throw new RuntimeException("HttpResponseCode: " + responsecode); + } else { + //Read JSON from API website + Scanner sc = new Scanner(url.openStream()); + while (sc.hasNext()) { + inline += sc.nextLine(); + } + //Create a JSON object from the string from the website + JSONObject jsonObject = new JSONObject(inline); + //Get nesseary feild from the JSON, and cast back into a String + String decision = (String) jsonObject.get("answer"); + answer = decision; + //Close the stream when reading the data has been finished + sc.close(); + } + //Disconnect the HttpURLConnection stream + connection.disconnect(); + } catch (IOException | RuntimeException e) { + } + //return the answer that the API gave + return answer; + + } + +} diff --git a/ScheduleCreator/Tests.java b/ScheduleCreator/Tests.java @@ -1,25 +1,36 @@ package ScheduleCreator; +/** + * Test class + * + * @author Nick Econopouly, Ilyass Sfar + * + * Last Updated: 3/27/2020 + */ +import ScheduleCreator.API.ApiAdapter; import java.io.IOException; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; -/** - * - * @author wrycode - */ public class Tests { public static void main(String[] args) throws IOException { + //regen databse Admin.regenDB(); + + //test api call + //ApiAdapter testAPI = new ApiAdapter(); + //System.out.println("API returned "+testAPI.decisionForce("")); + // // TODO: add courselist regex; // fix regenDB() so that it doesn't rely on classpath resources (so tests always work the first time) // getSections() // regenerate the database + // // test how DBAdapter works with current semesters - // testSemester(); + //testSemester(); } public static void testSemester() throws IOException { diff --git a/ScheduleCreator/Translator.java b/ScheduleCreator/Translator.java @@ -139,11 +139,11 @@ public class Translator { // this still needs to be finished, we need to account for different times on different days case TIME: regex = "(?<=([ ]\\b[A-Z]{3}\\b.\\b[0-9]{3}\\b.+ [0-9]{2}\\b)).+?(?=\\b((TR\\b|MW\\b|MWF\\b|M\\b|T\\b|W\\b|R\\b|F\\b|( TBA\\b))))"; - dataFileType = "times_and_dates"; + //dataFileType = "times_and_dates"; break; case DAY: regex = "\\b([ ](TR\\b|MW\\b|MWF\\b|M\\b|T\\b|W\\b|R\\b|F\\b|(TBA\\b)))\\b"; - dataFileType = "times_and_dates"; + //dataFileType = "times_and_dates"; break; case BUILDING: regex = "(?<=\\b([ ](TR\\b|MW\\b|MWF\\b|M\\b|T\\b|W\\b|R\\b|F\\b|(TBA\\b)))\\b).*(?=\\=)";