Skip to content

Commit

Permalink
Merge pull request #3 from anas-elgarhy/fix-bugs
Browse files Browse the repository at this point in the history
Fix bugs and remove the cach system
  • Loading branch information
0x61nas committed Jul 5, 2022
2 parents 8186ba6 + 1400869 commit 9a286fb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 71 deletions.
45 changes: 13 additions & 32 deletions src/main/java/com/anas/alqurancloudapi/QuranAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,11 @@ public static Edition[] getEditions(final EditionFormat format,
if (language != null && (language.length() != 2)) {
throw new IllegalArgumentException("Language must be 2 characters long");
}
final var jsonFile = Requester.sendRequest("edition" +
final var inputStream = Requester.sendRequest("edition" +
(format != null ? "?format=" + format.name().toLowerCase() : "") +
(type != null ? (format != null ? "&" : "?") + "type=" + type.name().toLowerCase() : "") +
(language != null ? (format != null || type != null ? "&" : "?") + "language=" + language : ""));
final var editions = mapper.readValue(jsonFile, Edition[].class);
jsonFile.delete();
return editions;
return mapper.readValue(inputStream, Edition[].class);
}

/**
Expand Down Expand Up @@ -110,10 +108,7 @@ public static Edition[] getEditions() throws IOException {
* @throws IOException If an error occurs while communicating with the API.
*/
public static String[] getAllEditionsLanguages() throws IOException {
final var jsonFile = Requester.sendRequest("edition/language");
final var languages = mapper.readValue(jsonFile, String[].class);
jsonFile.delete();
return languages;
return mapper.readValue(Requester.sendRequest("edition/language"), String[].class);
}

/**
Expand Down Expand Up @@ -212,14 +207,11 @@ public static Surah getSurah(final int surahNumber,
(Surahs.values()[surahNumber - 1].getNumberOfAyahs() - 1));
}

final var jsonFile = Requester.sendRequest("surah/" + surahNumber +
final var inputStream = Requester.sendRequest("surah/" + surahNumber +
(edition != null ? "/" + edition.getIdentifier() : "") +
(offset > -1 ? "?offset=" + offset : "") +
(limit > -1 ? ((offset > -1 ? "&" : "?") + "limit=") + limit : ""));
final var o = mapper.readValue(jsonFile, Surah.class);
// It deletes the temporary file that was created by the `Requester` class.
jsonFile.delete();
return o; // Surah object
return mapper.readValue(inputStream, Surah.class); // Surah object
}

/**
Expand Down Expand Up @@ -436,12 +428,9 @@ public static Ayah getAyah(final int ayahNumber, final Edition edition) throws I
if (ayahNumber < 1 || ayahNumber > Constants.AYAHS_COUNT) {
throw new IllegalArgumentException("Ayah number must be between 1 and " + Constants.AYAHS_COUNT);
}
final var jsonFile = Requester.sendRequest("ayah/" + ayahNumber
final var inputStream = Requester.sendRequest("ayah/" + ayahNumber
+ (edition != null ? "/" + edition.getIdentifier() : ""));
final var o = mapper.readValue(jsonFile, Ayah.class);
// It deletes the temporary file that was created by the `Requester` class.
jsonFile.delete();
return o; // Ayah object
return mapper.readValue(inputStream, Ayah.class); // Ayah object
}

/**
Expand Down Expand Up @@ -589,12 +578,9 @@ public static QuranCollection getPage(final int pageNumber, final Edition editio
if (pageNumber < 1) {
throw new IllegalArgumentException("Page number must be greater than 0");
}
final var jsonFile = Requester.sendRequest("page/" + pageNumber
final var inputStream = Requester.sendRequest("page/" + pageNumber
+ (edition != null ? "/" + edition.getIdentifier() : ""));
final var o = mapper.readValue(jsonFile, QuranCollection.class);
// It deletes the temporary file that was created by the `Requester` class.
jsonFile.delete();
return o; // Page object
return mapper.readValue(inputStream, QuranCollection.class); // Page object
}

/**
Expand Down Expand Up @@ -641,14 +627,11 @@ public static QuranCollection getJuz(final int juzNumber,
if (juzNumber < 1 || juzNumber > 30) {
throw new IllegalArgumentException("Juz number must be greater than 0 and less than 31");
}
final var jsonFile = Requester.sendRequest("juz/" + juzNumber +
final var inputStream = Requester.sendRequest("juz/" + juzNumber +
(edition != null ? "/" + edition.getIdentifier() : "") +
(offset > -1 ? "?offset=" + offset : "") +
(limit > -1 ? ((offset > -1 ? "&" : "?") + "limit=" + limit) : ""));
final var o = mapper.readValue(jsonFile, QuranCollection.class);
// It deletes the temporary file that was created by the `Requester` class.
jsonFile.delete();
return o; // Page object
return mapper.readValue(inputStream, QuranCollection.class); // Page object
}

/**
Expand Down Expand Up @@ -730,13 +713,11 @@ public static Ayah[] search(final String keyword,
if (keyword == null || keyword.isEmpty()) {
throw new IllegalArgumentException("Keyword cannot be null or empty");
}
final var jsonFile = Requester.sendRequest("search/" +
final var inputStream = Requester.sendRequest("search/" +
keyword.replace(" ", "%20") + "/" +
(surahNumber - 1 > -1 ? surahNumber : "all") + "/" +
(edition != null ? "/" + edition.getIdentifier() : "en"));
final var o = mapper.readValue(jsonFile, SearchResult.class);
// It deletes the temporary file that was created by the `Requester` class.
jsonFile.delete();
final var o = mapper.readValue(inputStream, SearchResult.class);
return o.matches;
}

Expand Down
25 changes: 0 additions & 25 deletions src/main/java/com/anas/alqurancloudapi/api/CacheHelper.java

This file was deleted.

21 changes: 7 additions & 14 deletions src/main/java/com/anas/alqurancloudapi/api/Requester.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.anas.alqurancloudapi.api;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

Expand All @@ -22,10 +20,10 @@ public class Requester {
* It sends a GET request to the API, remove the ignored properties, and saves the response to a file and returns it.
*
* @param endPoint The endpoint to send the request to.
* @return A json file containing the response.
* @return A input stream of the response.
* @throws IOException If an error occurs while sending the request or saving the response to a file.
*/
public static File sendRequest(final String endPoint) throws IOException {
public static InputStream sendRequest(final String endPoint) throws IOException {
final var connection = (HttpURLConnection) new URL(BASE_URL + endPoint).openConnection();
connection.setConnectTimeout(connectTimeout);
connection.setRequestMethod("GET");
Expand All @@ -36,7 +34,9 @@ public static File sendRequest(final String endPoint) throws IOException {
connection.connect();
try {
Thread.sleep(50);
} catch (InterruptedException ignored) { }
} catch (InterruptedException ie) {
Thread.currentThread().interrupt(); // Restore the interrupted status
}
} while (connection.getResponseCode() == 429 && i++ < 3);

// It checks if the response code is not 200, then it throws an exception.
Expand All @@ -56,14 +56,7 @@ public static File sendRequest(final String endPoint) throws IOException {
// It removes the ignored properties from the response.
response = response.substring(response.indexOf("\"data\":") + 7, response.length() - 1);

// It creates a file in the cache folder with the name of the endpoint.
final var file = CacheHelper.getCacheFile(endPoint);
// It writes the response to the file.
final var writer = new FileWriter(file);
writer.write(response);
writer.close();

return file;
return new ByteArrayInputStream(response.getBytes());
}

/**
Expand Down

0 comments on commit 9a286fb

Please sign in to comment.