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 25f8b79156ff00af81df88a957dbaa1036f8b138
parent 8a8c01ceca52004e1b269975adf4ffcc14a91cb6
Author: Ilyass Sfar <42755577+cloud0x9@users.noreply.github.com>
Date:   Sun, 29 Mar 2020 20:08:54 -0400

Added a Email API (#49)

* Add API package and interface

* Add API call method and Adapter

* Change which file TIME and DAY use

* Jamison/gui change (#46)

* refactor

* style guide

* refactor

* Add API package and interface

* Add API call method and Adapter

* Change which file TIME and DAY use

* change field in Semester class

* update UI design; remove generateSchedules bug; refactor

* merge sfar branch; add lib package with necessary jar file

* implement CRN pop up box

* implement show CRN pop up

* lib package and jar file

* Improve CRN pop up design (#47)

* refactor

* style guide

* refactor

* Add API package and interface

* Add API call method and Adapter

* Change which file TIME and DAY use

* change field in Semester class

* update UI design; remove generateSchedules bug; refactor

* merge sfar branch; add lib package with necessary jar file

* implement CRN pop up box

* implement show CRN pop up

* lib package and jar file

* improve CRN pop up design

* Completely delete old API, set up email API interface, and add all necessary libraries for the email API.

* Added Email API interface, adapter, and direct call method.
Also added example in test class.

* Added dedicated email for the project

* remove old API

* remove old API

* remove old API

Co-authored-by: jamisonv93 <47033011+jamisonv93@users.noreply.github.com>
Co-authored-by: Sfar <isfar314@gmail.com>
Diffstat:
AScheduleCreator/API/APIInterface.java | 24++++++++++++++++++++++++
DScheduleCreator/API/ApiAdapter.java | 28----------------------------
DScheduleCreator/API/ApiInterface.java | 21---------------------
AScheduleCreator/API/EmailAPI.java | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
AScheduleCreator/API/EmailAdapter.java | 31+++++++++++++++++++++++++++++++
DScheduleCreator/API/YesNoApi.java | 76----------------------------------------------------------------------------
MScheduleCreator/Tests.java | 20++++++++------------
AScheduleCreator/lib/http-client-java-0.89.jar | 0
AScheduleCreator/lib/junit-4.11.jar | 0
AScheduleCreator/lib/mailjet-client-4.2.0.jar | 0
10 files changed, 124 insertions(+), 137 deletions(-)

diff --git a/ScheduleCreator/API/APIInterface.java b/ScheduleCreator/API/APIInterface.java @@ -0,0 +1,24 @@ +package ScheduleCreator.API; + +/** + * Interface for email API calls for MailJet. + * + * @author Ilyass Sfar + * + * Last Updated: 3/18/2020 + */ +import com.mailjet.client.errors.MailjetException; +import com.mailjet.client.errors.MailjetSocketTimeoutException; + +public interface APIInterface { + /** + * Send email's using an API. + * + * @param _email The email that is being sent to, this comes from the user. + * @param _message This is what is being sent in the email. + * @return + * @throws MailjetException + * @throws MailjetSocketTimeoutException + */ + public void SendEmail(String _email, String _message)throws MailjetException, MailjetSocketTimeoutException; +} diff --git a/ScheduleCreator/API/ApiAdapter.java b/ScheduleCreator/API/ApiAdapter.java @@ -1,27 +0,0 @@ -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 @@ -1,21 +0,0 @@ -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/EmailAPI.java b/ScheduleCreator/API/EmailAPI.java @@ -0,0 +1,61 @@ +package ScheduleCreator.API; + +/** + * Makes direct calls to the MailJet API. + * + * @author Ilyass Sfar + * + * Last Updated: 3/29/2020 + */ +import com.mailjet.client.errors.MailjetException; +import com.mailjet.client.errors.MailjetSocketTimeoutException; +import com.mailjet.client.MailjetClient; +import com.mailjet.client.MailjetRequest; +import com.mailjet.client.MailjetResponse; +import com.mailjet.client.ClientOptions; +import com.mailjet.client.resource.Emailv31; +import org.json.JSONArray; +import org.json.JSONObject; + +public class EmailAPI implements APIInterface { + + /** + * Send email's using an API. + * + * @param _email The email that is being sent to, this comes from the user. + * @param _message This is what is being sent in the email. + * @return + * @throws MailjetException + * @throws MailjetSocketTimeoutException + */ + @Override + public void SendEmail(String _email, String _message) throws MailjetException, MailjetSocketTimeoutException { + //Initalize the API requirements + MailjetClient client; + MailjetRequest request; + MailjetResponse response; + //Create a new client to send emails, using public and private api keys, and speify which version of the client is being used + client = new MailjetClient(("4ed58dd4eac88294f8decd6ebcb37802"), ("2ebe9765bf49eeac4c25fa0436edbcbf"), new ClientOptions("v3.1")); + request = new MailjetRequest(Emailv31.resource) + .property(Emailv31.MESSAGES, new JSONArray() + .put(new JSONObject() + //The email that is being used to send, this is controled from the mailjet webiste + .put(Emailv31.Message.FROM, new JSONObject() + .put("Email", "ScheduleCreatorUNCG@gmail.com") + .put("Name", "Schedule Creator")) + //The email that is being send to + .put(Emailv31.Message.TO, new JSONArray() + .put(new JSONObject() + .put("Email", _email) + .put("Name", "Student"))) + //Subject line of email + .put(Emailv31.Message.SUBJECT, "Schedule Creator") + //Body of email + .put(Emailv31.Message.TEXTPART, _message))); + response = client.post(request); + + //Print out what email was just sent, it was sent or not, and any errors from the API + System.out.println(response.getStatus()); + System.out.println(response.getData()); + } +} diff --git a/ScheduleCreator/API/EmailAdapter.java b/ScheduleCreator/API/EmailAdapter.java @@ -0,0 +1,31 @@ +package ScheduleCreator.API; + +/** + * Adapter class for EmailAPI, so it doesn't have to be called directly. + * + * @author Ilyass Sfar + * + * Last Updated: 3/29/2020 + */ +import com.mailjet.client.errors.MailjetException; +import com.mailjet.client.errors.MailjetSocketTimeoutException; + +public class EmailAdapter implements APIInterface { + + protected static final APIInterface thisApi = new EmailAPI(); + + /** + * Send email's using an API. + * + * @param _email The email that is being sent to, this comes from the user. + * @param _message This is what is being sent in the email. + * @return + * @throws MailjetException + * @throws MailjetSocketTimeoutException + */ + @Override + public void SendEmail(String _email, String _message) throws MailjetException, MailjetSocketTimeoutException { + EmailAdapter.thisApi.SendEmail(_email, _message); + } + +} diff --git a/ScheduleCreator/API/YesNoApi.java b/ScheduleCreator/API/YesNoApi.java @@ -1,76 +0,0 @@ -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 @@ -7,7 +7,10 @@ package ScheduleCreator; * * Last Updated: 3/27/2020 */ -import ScheduleCreator.API.ApiAdapter; + +import ScheduleCreator.API.EmailAdapter; +import com.mailjet.client.errors.MailjetException; +import com.mailjet.client.errors.MailjetSocketTimeoutException; import java.io.IOException; import java.util.List; import java.util.logging.Level; @@ -15,21 +18,14 @@ import java.util.logging.Logger; public class Tests { - public static void main(String[] args) throws IOException { + public static void main(String[] args) throws IOException, MailjetException, MailjetSocketTimeoutException { //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(); + //Test api call, + EmailAdapter testAPI = new EmailAdapter(); + testAPI.SendEmail("isfar314@gmail.com", "Testing Email API Call"); } public static void testSemester() throws IOException { diff --git a/ScheduleCreator/lib/http-client-java-0.89.jar b/ScheduleCreator/lib/http-client-java-0.89.jar Binary files differ. diff --git a/ScheduleCreator/lib/junit-4.11.jar b/ScheduleCreator/lib/junit-4.11.jar Binary files differ. diff --git a/ScheduleCreator/lib/mailjet-client-4.2.0.jar b/ScheduleCreator/lib/mailjet-client-4.2.0.jar Binary files differ.