@@ -15,7 +15,7 @@ public class DevRant {
1515 private final DevRantAuth devRantAuth ;
1616
1717 private RequestHandler requestHandler ;
18- Auth auth ;
18+ private Auth auth ;
1919
2020 static {
2121 INJECTOR = Guice .createInjector ();
@@ -40,6 +40,14 @@ RequestHandler getRequestHandler() {
4040 return requestHandler ;
4141 }
4242
43+ Auth getAuthObject () {
44+ return auth ;
45+ }
46+
47+ void setAuthObject (Auth auth ) {
48+ this .auth = auth ;
49+ }
50+
4351 /**
4452 * Access the devRant feed.
4553 *
@@ -70,8 +78,9 @@ public DevRantAuth getAuth() {
7078 * @param id The id of the rant.
7179 * @return The rant.
7280 */
73- public Result <CommentedRant > getRant (int id ) {
74- return requestHandler .get (ApiEndpoint .RANTS .toString () + '/' + id , CommentedRantResponse .class );
81+ public CommentedRant getRant (int id ) {
82+ return requestHandler .get (ApiEndpoint .RANTS .toString () + '/' + id , CommentedRantResponse .class )
83+ .getValue ().orElseThrow (() -> new NoSuchRantException (id ));
7584 }
7685
7786 /**
@@ -80,16 +89,11 @@ public Result<CommentedRant> getRant(int id) {
8089 * @param username The username of the user.
8190 * @return The user.
8291 */
83- public Result <User > getUser (String username ) {
84- Result <Integer > result = requestHandler .get (ApiEndpoint .USER_ID , UserIdResponse .class , new BasicNameValuePair ("username" , username ));
92+ public User getUser (String username ) {
93+ Integer result = requestHandler .get (ApiEndpoint .USER_ID , UserIdResponse .class , new BasicNameValuePair ("username" , username ))
94+ .getValue ().orElseThrow (() -> new NoSuchUsernameException (username ));
8595
86- // Check the result.
87- if (!result .getValue ().isPresent ()) {
88- // When the username is invalid, no error message is returned by the API.
89- return new Result <>("Invalid username specified." );
90- }
91-
92- return getUser (result .getValue ().get ());
96+ return getUser (result );
9397 }
9498
9599 /**
@@ -98,28 +102,23 @@ public Result<User> getUser(String username) {
98102 * @param id The id of the user.
99103 * @return The user.
100104 */
101- public Result <User > getUser (int id ) {
102- Result <User > result = requestHandler .get (ApiEndpoint .USERS .toString () + '/' + id , UserResponse .class );
103-
104- // Check the result.
105- if (!result .getValue ().isPresent ()) {
106- // When the user id is invalid, no error message is returned by the API.
107- return new Result <>("Invalid user id specified." );
108- }
105+ public User getUser (int id ) {
106+ User user = requestHandler .get (ApiEndpoint .USERS .toString () + '/' + id , UserResponse .class )
107+ .getValue ().orElseThrow (() -> new NoSuchUserIdException (id ));
109108
110109 // Set the id, as that is not part of the response.
111- result . getValue (). get () .setId (id );
110+ user .setId (id );
112111
113- return result ;
112+ return user ;
114113 }
115114
116115 /**
117116 * Get a random rant.
118117 *
119118 * @return A random rant.
120119 */
121- public Result < Rant > getSurprise () {
122- return requestHandler .get (ApiEndpoint .SURPRISE , RantResponse .class );
120+ public Rant getSurprise () {
121+ return requestHandler .get (ApiEndpoint .SURPRISE , RantResponse .class ). getValueOrError () ;
123122 }
124123
125124 /**
@@ -128,8 +127,9 @@ public Result<Rant> getSurprise() {
128127 * @param id The id of the collab.
129128 * @return The collab.
130129 */
131- public Result <Collab > getCollab (int id ) {
132- return requestHandler .get (ApiEndpoint .RANTS .toString () + '/' + id , CollabResponse .class );
130+ public Collab getCollab (int id ) {
131+ return requestHandler .get (ApiEndpoint .RANTS .toString () + '/' + id , CollabResponse .class )
132+ .getValue ().orElseThrow (() -> new NoSuchRantException (id ));
133133 }
134134
135135 /**
@@ -143,17 +143,17 @@ public Result<Collab> getCollab(int id) {
143143 public boolean login (String username , char [] password ) {
144144 logout ();
145145
146- Result < Auth > response = requestHandler .post (ApiEndpoint .AUTH_TOKEN , AuthResponse .class ,
146+ Auth response = requestHandler .post (ApiEndpoint .AUTH_TOKEN , AuthResponse .class ,
147147 new BasicNameValuePair ("username" , username ),
148148 new BasicNameValuePair ("password" , String .valueOf (password ))
149- );
149+ ). getValue (). orElseThrow (() -> new DevRantException ( "Could not log in." )) ;
150150
151151 // Clear the password.
152152 for (int i = 0 ; i < password .length ; i ++) {
153153 password [i ] = 0 ;
154154 }
155155
156- response . getValue (). ifPresent ( r -> auth = r ) ;
156+ auth = response ;
157157 return isLoggedIn ();
158158 }
159159
0 commit comments