Skip to content

Commit 94237d9

Browse files
authored
Merge pull request #1 from JacobDevelopment/development
Development
2 parents 5385534 + f310d38 commit 94237d9

23 files changed

+1496
-145
lines changed

readme.md

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<h1>J-SWAPI</h1>
2-
<p>J(ava)-SWAPI, this is a helper library for <a href="https://swapi.dev/">SWAPI</a> written completely in java. It allows you to retrieve data from the Star Wars API for any applications in need of Star Wars data.</p>
2+
<p>J(ava)-SWAPI, this is an API for <a href="https://swapi.dev/">SWAPI</a> written completely in java. It allows you to retrieve data from the Star Wars API for any applications in need of Star Wars data.</p>
33

44
<h1>Dependencies</h1>
55
<ul>
@@ -11,9 +11,7 @@
1111
This API does not handle rate limiting, and according to the SWAPI website it allows 10,000 API requests <b>per day</b>.
1212
<a href="https://swapi.dev/documentation#rate">Source Here.</a>
1313

14-
No authentication is required per the website as well.
15-
16-
<b>As soon as the base is complete, it will be added to MavenCentral so it can be added as a dependency.</b>
14+
No authentication is required per the website as well.
1715
</p>
1816

1917

@@ -27,10 +25,12 @@ class Started {
2725
}
2826
```
2927

30-
From here, we can all specific resources that we want to retrieve. We retrieve the following resources from what SWAPI offers:
28+
From here, we can all specific resources that we want to retrieve. We retrieve the following resources from what SWAPI
29+
offers:
3130
<b>People, Films, Starships, Vehicles, Species, Planets.</b>
3231

3332
For example, if we want to retrieve a specific person via <b>ID</b> we can call the following people action:
33+
3434
```java
3535
class Started {
3636
public static void main(String[] args) {
@@ -41,9 +41,11 @@ class Started {
4141
}
4242
```
4343

44-
This API does allow search querying, or retrieving all data for specific resources. All resource(s) have the following methods tied to them:
44+
This API does allow search querying, or retrieving all data for specific resources. All resource(s) have the following
45+
methods tied to them:
4546
<br/>
4647
<b>Resource#getById(Int)</b>
48+
4749
```java
4850
class Started {
4951
public static void main(String[] args) {
@@ -53,9 +55,11 @@ class Started {
5355
}
5456
}
5557
```
58+
5659
<br/>
5760

5861
<b>List&lt;Resource&gt;#getBySearch(String)</b>
62+
5963
```java
6064
class Started {
6165
public static void main(String[] args) {
@@ -65,30 +69,34 @@ class Started {
6569
}
6670
}
6771
```
72+
6873
<br/>
6974

7075
<b>List&lt;Resource&gt;#getAll()</b>
76+
7177
```java
7278
class Started {
7379
public static void main(String[] args) {
7480
API api = new API.Builder().build();
7581
var movies = api.films().getAll(); // The URL forming here is: https://swapi.dev/api/films/?format=json
7682
for (Films film : movies) {
7783
System.out.print(film.getEpisodeId() + " ");
78-
}
84+
}
7985
// Prints 4 5 6 1 2 3 (Unordered for whatever reason!).
8086
}
8187
}
8288
```
89+
8390
<br/>
8491

8592
<b>List&lt;Resource&gt;#getByFilter(Predicate&lt;Resource&gt;)</b>
93+
8694
```java
8795
class Started {
8896
public static void main(String[] args) {
8997
API api = new API.Builder().build();
90-
var filtered = api.films().getByFilter(
91-
film -> film.getEpisodeId() <= 3
98+
var filtered = api.films().getByFilter(
99+
film -> film.getEpisodeId() <= 3
92100
);
93101
// Forms URL: https://swapi.dev/api/films/?format=json
94102
// It then just sorts utilizing streams filter methods.
@@ -100,9 +108,11 @@ class Started {
100108
}
101109
}
102110
```
111+
103112
<br/>
104113

105114
<b>Optional&lt;Resource&gt;#getFirstBySearch(String)</b>.
115+
106116
```java
107117
class Started {
108118
public static void main(String[] args) {
@@ -113,11 +123,18 @@ class Started {
113123
}
114124
```
115125

116-
You can find more examples here: <a href="https://github.com/JacobDevelopment/j-swapi/tree/master/src/test/java/io/jking/jswapi">Examples</a>
126+
You can find more examples
127+
here: <a href="https://github.com/JacobDevelopment/j-swapi/tree/master/src/test/java/io/jking/jswapi">Examples</a>
117128

118129
<h1>Todo</h1>
119130

120-
| Item #1 | Description | Done? |
121-
|---------|------------------------------|-------|
122-
| #1 | Finish other resources. | [ ] |
123-
| #2 | Get rid of executor service. | [X] |
131+
| Item #1 | Description | Done? |
132+
|---------|---------------------------------------|-------|
133+
| #1 | Finish other resources. | [X] |
134+
| #2 | Get rid of executor service. | [X] |
135+
| #3 | Duplicate code among request actions. | [X] |
136+
| #4 | Fix readme discrepenacies. | [ ] |
137+
| #5 | Handle 404 errors / page not found. | [X] |
138+
| #6 | Add cache internally so we don't have to request all the time. | [ ] |
139+
140+
Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,79 @@
11
package io.jking.jswapi.action;
22

3-
import static io.jking.jswapi.utility.Binder.*;
4-
5-
import com.fasterxml.jackson.databind.ObjectMapper;
3+
import io.jking.jswapi.internal.Resources;
64
import io.jking.jswapi.resources.BaseResource;
75
import io.jking.jswapi.resources.GeneralQuery;
86
import io.jking.jswapi.utility.Binder;
97

108
import java.io.IOException;
11-
import java.net.MalformedURLException;
9+
import java.util.ArrayList;
10+
import java.util.Collections;
1211
import java.util.List;
1312
import java.util.Optional;
1413
import java.util.function.Predicate;
1514
import java.util.stream.Collectors;
15+
import java.util.stream.Stream;
16+
17+
public abstract class RequestAction<T extends BaseResource> {
18+
19+
private final Resources resource;
20+
private final Class<T> clazz;
21+
22+
public RequestAction(Resources resource, Class<T> clazz) {
23+
this.resource = resource;
24+
this.clazz = clazz;
25+
}
26+
27+
public T getById(final int id) throws IOException {
28+
final String route = resource.getRouteById(id);
29+
return Binder.read(route, clazz);
30+
}
1631

17-
public interface RequestAction<T extends BaseResource> {
32+
public List<T> getAll() throws IOException {
33+
final String route = resource.getAllRoute();
34+
final GeneralQuery<T> generalQuery = Binder.bind(route, clazz);
35+
if (generalQuery.getResults().isEmpty())
36+
return Collections.emptyList();
1837

19-
T getById(final int id) throws IOException;
38+
if (generalQuery.getCount() > 9)
39+
return getAllByPages(generalQuery);
2040

21-
List<T> getAll() throws IOException;
41+
return generalQuery.getResults();
42+
}
2243

23-
List<T> getBySearch(final String searchQuery) throws IOException;
44+
public List<T> getBySearch(final String searchParameter) throws IOException {
45+
final String route = resource.getRouteBySearch(searchParameter);
46+
final GeneralQuery<T> generalQuery = Binder.bind(route, clazz);
47+
if (generalQuery.getResults().isEmpty())
48+
return Collections.emptyList();
2449

25-
default GeneralQuery<T> getGeneralQuery(final String route, Class<T> clazz) throws IOException {
26-
return Binder.bind(route, clazz);
50+
return generalQuery.getResults();
2751
}
2852

53+
public List<T> getByFilter(final Predicate<T> filter) throws IOException {
54+
return getAll().stream().filter(filter).collect(Collectors.toList());
55+
}
2956

30-
default List<T> getByFilter(final Predicate<T> filter) throws IOException {
31-
return getAll()
32-
.stream()
33-
.filter(filter)
34-
.collect(Collectors.toList());
57+
public Optional<T> getFirstBySearch(final String searchParameter) throws IOException {
58+
return getBySearch(searchParameter).stream().findFirst();
3559
}
3660

37-
default Optional<T> getFirstBySearch(final String searchQuery) throws IOException {
38-
return getBySearch(searchQuery)
39-
.stream()
40-
.findFirst();
61+
public Optional<T> getFirst() throws IOException {
62+
return getAll().stream().findFirst();
4163
}
4264

65+
private List<T> getAllByPages(GeneralQuery<T> generalQuery) throws IOException {
66+
final List<T> allOfResources = new ArrayList<>();
67+
final int totalPages = (int) (Math.ceil(generalQuery.getCount() / 10.0));
68+
for (int i = 1; i <= totalPages; i++) {
69+
final String PAGE_ROUTE = resource.getPageRoute(i);
70+
final GeneralQuery<T> newGeneralQuery = Binder.bind(PAGE_ROUTE, clazz);
71+
if (newGeneralQuery.getResults().isEmpty())
72+
break;
73+
74+
allOfResources.addAll(newGeneralQuery.getResults());
75+
}
76+
77+
return allOfResources;
78+
}
4379
}

src/main/java/io/jking/jswapi/action/impl/FilmsAction.java

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,10 @@
22

33
import io.jking.jswapi.action.RequestAction;
44
import io.jking.jswapi.internal.Resources;
5-
import io.jking.jswapi.resources.GeneralQuery;
65
import io.jking.jswapi.resources.impl.Films;
7-
import io.jking.jswapi.utility.Binder;
86

9-
import java.io.IOException;
10-
import java.util.Collections;
11-
import java.util.List;
12-
13-
public class FilmsAction implements RequestAction<Films> {
14-
@Override
15-
public Films getById(int id) throws IOException {
16-
final String ROUTE = Resources.FILMS.getRouteById(id);
17-
return Binder.read(ROUTE, Films.class);
18-
}
19-
20-
@Override
21-
public List<Films> getAll() throws IOException {
22-
final String ROUTE = Resources.FILMS.getAllRoute();
23-
final GeneralQuery<Films> generalQuery = getGeneralQuery(ROUTE, Films.class);
24-
25-
if (generalQuery.getResults().isEmpty())
26-
return Collections.emptyList();
27-
28-
return generalQuery.getResults();
29-
}
30-
31-
@Override
32-
public List<Films> getBySearch(String searchQuery) throws IOException {
33-
final String ROUTE = Resources.FILMS.getRouteBySearch(searchQuery);
34-
final GeneralQuery<Films> generalQuery = getGeneralQuery(ROUTE, Films.class);
35-
36-
if (generalQuery.getResults().isEmpty())
37-
return Collections.emptyList();
38-
39-
return generalQuery.getResults();
7+
public class FilmsAction extends RequestAction<Films> {
8+
public FilmsAction() {
9+
super(Resources.FILMS, Films.class);
4010
}
4111
}
Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,11 @@
11
package io.jking.jswapi.action.impl;
22

3-
import com.fasterxml.jackson.core.type.TypeReference;
4-
import com.fasterxml.jackson.databind.ObjectMapper;
53
import io.jking.jswapi.action.RequestAction;
64
import io.jking.jswapi.internal.Resources;
75
import io.jking.jswapi.resources.impl.People;
8-
import io.jking.jswapi.resources.GeneralQuery;
9-
import io.jking.jswapi.utility.Binder;
106

11-
import java.io.IOException;
12-
import java.net.URL;
13-
import java.util.ArrayList;
14-
import java.util.Collections;
15-
import java.util.List;
16-
import java.util.stream.Collectors;
17-
18-
public class PeopleAction implements RequestAction<People> {
19-
@Override
20-
public People getById(int id) throws IOException {
21-
final String ROUTE = Resources.PEOPLE.getRouteById(id);
22-
return Binder.read(ROUTE, People.class);
23-
}
24-
25-
// Needs Optimization
26-
@Override
27-
public List<People> getAll() throws IOException {
28-
final String ROUTE = Resources.PEOPLE.getAllRoute();
29-
final GeneralQuery<People> generalQuery = getGeneralQuery(ROUTE, People.class);
30-
if (generalQuery.getResults().isEmpty())
31-
return Collections.emptyList();
32-
33-
final List<People> allPeople = new ArrayList<>();
34-
final int totalPages = (int) (Math.round(generalQuery.getCount() / 10.0) + 1);
35-
for (int i = 1; i <= totalPages; i++) {
36-
final String PAGE_ROUTE = Resources.PEOPLE.getPageRoute(i);
37-
final GeneralQuery<People> newQuery = getGeneralQuery(PAGE_ROUTE, People.class);
38-
if (newQuery.getResults().isEmpty())
39-
break;
40-
41-
allPeople.addAll(newQuery.getResults());
42-
}
43-
44-
return allPeople;
7+
public class PeopleAction extends RequestAction<People> {
8+
public PeopleAction() {
9+
super(Resources.PEOPLE, People.class);
4510
}
46-
47-
@Override
48-
public List<People> getBySearch(String searchQuery) throws IOException {
49-
final String ROUTE = Resources.PEOPLE.getRouteBySearch(searchQuery);
50-
final GeneralQuery<People> generalQuery = getGeneralQuery(ROUTE, People.class);
51-
52-
if (generalQuery.getResults().isEmpty())
53-
return Collections.emptyList();
54-
55-
return generalQuery.getResults();
56-
}
57-
5811
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.jking.jswapi.action.impl;
2+
3+
import io.jking.jswapi.action.RequestAction;
4+
import io.jking.jswapi.internal.Resources;
5+
import io.jking.jswapi.resources.impl.Planets;
6+
7+
public class PlanetsAction extends RequestAction<Planets> {
8+
public PlanetsAction() {
9+
super(Resources.PLANETS, Planets.class);
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.jking.jswapi.action.impl;
2+
3+
import io.jking.jswapi.action.RequestAction;
4+
import io.jking.jswapi.internal.Resources;
5+
import io.jking.jswapi.resources.impl.Species;
6+
7+
public class SpeciesAction extends RequestAction<Species> {
8+
public SpeciesAction() {
9+
super(Resources.SPECIES, Species.class);
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.jking.jswapi.action.impl;
2+
3+
import io.jking.jswapi.action.RequestAction;
4+
import io.jking.jswapi.internal.Resources;
5+
import io.jking.jswapi.resources.impl.Starships;
6+
7+
public class StarshipsAction extends RequestAction<Starships> {
8+
public StarshipsAction() {
9+
super(Resources.STARSHIPS, Starships.class);
10+
}
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.jking.jswapi.action.impl;
2+
3+
import io.jking.jswapi.action.RequestAction;
4+
import io.jking.jswapi.internal.Resources;
5+
import io.jking.jswapi.resources.impl.Vehicles;
6+
7+
public class VehiclesAction extends RequestAction<Vehicles> {
8+
public VehiclesAction() {
9+
super(Resources.VEHICLES, Vehicles.class);
10+
11+
}
12+
}

0 commit comments

Comments
 (0)