-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ refactor code for better testability #23
- Loading branch information
Showing
4 changed files
with
125 additions
and
14 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
src/main/java/swiss/fihlon/apus/plugin/social/bluesky/BlueSkyException.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,28 @@ | ||
/* | ||
* Apus - A social wall for conferences with additional features. | ||
* Copyright (C) Marcus Fihlon and the individual contributors to Apus. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package swiss.fihlon.apus.plugin.social.bluesky; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class BlueSkyException extends Exception { | ||
|
||
public BlueSkyException(@NotNull final String message, @NotNull final Exception e) { | ||
super(message, e); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/swiss/fihlon/apus/plugin/social/bluesky/BlueSkyLoader.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,27 @@ | ||
/* | ||
* Apus - A social wall for conferences with additional features. | ||
* Copyright (C) Marcus Fihlon and the individual contributors to Apus. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package swiss.fihlon.apus.plugin.social.bluesky; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.json.JSONArray; | ||
|
||
public interface BlueSkyLoader { | ||
|
||
@NotNull JSONArray getPosts(@NotNull String instance, @NotNull String hashtag, @NotNull String postAPI) throws BlueSkyException; | ||
|
||
} |
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
45 changes: 45 additions & 0 deletions
45
src/main/java/swiss/fihlon/apus/plugin/social/bluesky/DefaultBlueSkyLoader.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,45 @@ | ||
/* | ||
* Apus - A social wall for conferences with additional features. | ||
* Copyright (C) Marcus Fihlon and the individual contributors to Apus. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package swiss.fihlon.apus.plugin.social.bluesky; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
import org.springframework.stereotype.Service; | ||
import swiss.fihlon.apus.util.DownloadUtil; | ||
|
||
@Service | ||
public final class DefaultBlueSkyLoader implements BlueSkyLoader { | ||
|
||
@Override | ||
@NotNull | ||
public JSONArray getPosts(@NotNull final String instance, @NotNull final String hashtag, @NotNull String postAPI) throws BlueSkyException { | ||
try { | ||
final var url = String.format(postAPI, instance, hashtag); | ||
final var json = DownloadUtil.getString(url); | ||
final var jsonPosts = new JSONObject(json).getJSONArray("posts"); | ||
if (jsonPosts != null) { | ||
return jsonPosts; | ||
} | ||
throw new NullPointerException(String.format("No posts with hashtag '%s' from BlueSky instance '%s'", hashtag, instance)); | ||
} catch (final Exception e) { | ||
throw new BlueSkyException(String.format("Unable to load posts with hashtag '%s' from BlueSky instance '%s'", hashtag, instance), e); | ||
} | ||
} | ||
|
||
} |