Skip to content

Commit 4b299c7

Browse files
committed
Included new wrapper class for remote API responses containing extended error and pagination data.
1 parent e0f6500 commit 4b299c7

File tree

2 files changed

+288
-0
lines changed

2 files changed

+288
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.github.m0nk3y2k4.thetvdb.api.model;
2+
3+
4+
import java.util.List;
5+
import java.util.Optional;
6+
7+
/**
8+
* Interface for extended remote API responses. Some API routes will not only return the actually requested data but will also report additional
9+
* information with regards to error and pagination handling.
10+
* <p/>
11+
* <b>Note:</b><br/>
12+
* The type and amount of additional information returned by the remote service depends on the actual API route. Some will only return the requested
13+
* records, other will also report additional error information in case invalid parameters of filters have been used by the client. API calls that
14+
* support pagination might also add some additional information which can be used to navigate through the "pages".
15+
* <p/>
16+
*
17+
* @param <T> Type of actual payload data returned by the remote service
18+
*/
19+
public interface APIResponse<T> {
20+
21+
/**
22+
* Returns the actually requested records mapped as Java object.
23+
*
24+
* @return Data returned by the API call
25+
*/
26+
T getData();
27+
28+
/**
29+
* Returns an Optional representing the additional error information returned by the remote service. If the request was fully compliant or if the
30+
* requested resource does not support additional error reporting the returned Optional might be empty.
31+
*
32+
* @return Optional containing additional error details or empty Optional if no errors occured or error reporting is not supported by the requested resource
33+
*/
34+
Optional<JSONErrors> getErrors();
35+
36+
/**
37+
* Returns an Optional representing the additional paging information returned by the remote service. If the requested resource does not support pagination
38+
* the returned Optional might be empty.
39+
*
40+
* @return Optional containing additional paging information or empty Optional if pagination is not supported by the requested resource
41+
*/
42+
Optional<Links> getLinks();
43+
44+
/**
45+
* Interface representing optional soft errors that might occur while requesting data from the remote service
46+
*/
47+
interface JSONErrors {
48+
49+
/**
50+
* Get the invalidFilters
51+
*
52+
* @return the invalidFilters
53+
*/
54+
List<String> getInvalidFilters();
55+
56+
/**
57+
* Get the invalidLanguage
58+
*
59+
* @return the invalidLanguage
60+
*/
61+
String getInvalidLanguage();
62+
63+
/**
64+
* Get the invalidQueryParams
65+
*
66+
* @return the invalidQueryParams
67+
*/
68+
List<String> getInvalidQueryParams();
69+
}
70+
71+
/**
72+
* Interface representing optional paging information for remote service requests supporting pagination
73+
*/
74+
interface Links {
75+
76+
/**
77+
* Get the number of the first available page
78+
*
79+
* @return the number of the first available page
80+
*/
81+
int getFirst();
82+
83+
/**
84+
* Get the number of the last available page
85+
*
86+
* @return the number of the last available page
87+
*/
88+
int getLast();
89+
90+
/**
91+
* Get the number of the next page (if available)
92+
*
93+
* @return the number of the next page
94+
*/
95+
int getNext();
96+
97+
/**
98+
* Get the number of the previous page (if available)
99+
*
100+
* @return the number of the previous page
101+
*/
102+
int getPrevious();
103+
}
104+
}
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
package com.github.m0nk3y2k4.thetvdb.internal.api.impl.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.github.m0nk3y2k4.thetvdb.api.model.APIResponse;
5+
6+
import java.util.Collections;
7+
import java.util.List;
8+
import java.util.Optional;
9+
10+
@JsonIgnoreProperties(ignoreUnknown = true)
11+
public class APIResponseImpl<T> implements APIResponse<T> {
12+
13+
/** Requested record data */
14+
private T data;
15+
/** Additional error information */
16+
private JSONErrors errors;
17+
/** Additional paging information */
18+
private Links links;
19+
20+
@Override
21+
public T getData() {
22+
return data;
23+
}
24+
25+
/**
26+
* Set the data
27+
*
28+
* @param data the data to set
29+
*/
30+
public void setData(T data) {
31+
this.data = data;
32+
}
33+
34+
@Override
35+
public Optional<JSONErrors> getErrors() {
36+
return Optional.ofNullable(errors);
37+
}
38+
39+
/**
40+
* Set the errors
41+
*
42+
* @param errors the errors to set
43+
*/
44+
public void setErrors(JSONErrors errors) {
45+
this.errors = errors;
46+
}
47+
48+
@Override
49+
public Optional<Links> getLinks() {
50+
return Optional.ofNullable(links);
51+
}
52+
53+
/**
54+
* Set the links
55+
*
56+
* @param links the links to set
57+
*/
58+
public void setLinks(Links links) {
59+
this.links = links;
60+
}
61+
62+
@JsonIgnoreProperties(ignoreUnknown = true)
63+
public static class JSONErrorsImpl implements JSONErrors {
64+
65+
/** JSON properties mapped as Java types */
66+
private List<String> invalidFilters;
67+
private String invalidLanguage;
68+
private List<String> invalidQueryParams;
69+
70+
public JSONErrorsImpl() {
71+
super();
72+
this.invalidFilters = Collections.emptyList();
73+
this.invalidQueryParams = Collections.emptyList();
74+
}
75+
76+
@Override
77+
public List<String> getInvalidFilters() {
78+
return invalidFilters;
79+
}
80+
81+
/**
82+
* Set the invalidFilters
83+
*
84+
* @param invalidFilters the invalidFilters to set
85+
*/
86+
public void setInvalidFilters(List<String> invalidFilters) {
87+
this.invalidFilters = invalidFilters;
88+
}
89+
90+
@Override
91+
public String getInvalidLanguage() {
92+
return invalidLanguage;
93+
}
94+
95+
/**
96+
* Set the invalidLanguage
97+
*
98+
* @param invalidLanguage the invalidLanguage to set
99+
*/
100+
public void setInvalidLanguage(String invalidLanguage) {
101+
this.invalidLanguage = invalidLanguage;
102+
}
103+
104+
@Override
105+
public List<String> getInvalidQueryParams() {
106+
return invalidQueryParams;
107+
}
108+
109+
/**
110+
* Set the invalidQueryParams
111+
*
112+
* @param invalidQueryParams the invalidQueryParams to set
113+
*/
114+
public void setInvalidQueryParams(List<String> invalidQueryParams) {
115+
this.invalidQueryParams = invalidQueryParams;
116+
}
117+
}
118+
119+
@JsonIgnoreProperties(ignoreUnknown = true)
120+
public static class LinksImpl implements Links {
121+
122+
/** JSON properties mapped as Java types */
123+
private int first;
124+
private int last;
125+
private int next;
126+
private int previous;
127+
128+
@Override
129+
public int getFirst() {
130+
return first;
131+
}
132+
133+
/**
134+
* Set the first
135+
*
136+
* @param first the first to set
137+
*/
138+
public void setFirst(int first) {
139+
this.first = first;
140+
}
141+
142+
@Override
143+
public int getLast() {
144+
return last;
145+
}
146+
147+
/**
148+
* Set the last
149+
*
150+
* @param last the last to set
151+
*/
152+
public void setLast(int last) {
153+
this.last = last;
154+
}
155+
156+
@Override
157+
public int getNext() {
158+
return next;
159+
}
160+
161+
/**
162+
* Set the next
163+
*
164+
* @param next the next to set
165+
*/
166+
public void setNext(int next) {
167+
this.next = next;
168+
}
169+
170+
@Override
171+
public int getPrevious() {
172+
return previous;
173+
}
174+
175+
/**
176+
* Set the previous
177+
*
178+
* @param previous the previous to set
179+
*/
180+
public void setPrevious(int previous) {
181+
this.previous = previous;
182+
}
183+
}
184+
}

0 commit comments

Comments
 (0)