Skip to content

Commit cd9822e

Browse files
committed
Implement user about api
1 parent 4dbaff6 commit cd9822e

12 files changed

Lines changed: 358 additions & 1 deletion

File tree

library/src/main/java/me/proxer/library/api/DateAdapter.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ Date fromJson(final String date) throws ParseException {
2424
if (dateAsLong != null) {
2525
return new Date(dateAsLong * DATE_MULTIPLICAND);
2626
} else {
27-
return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.GERMANY).parse(date);
27+
// Distinguish between dates with time and dates without.
28+
if (date.contains(":")) {
29+
return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.GERMANY).parse(date);
30+
} else {
31+
return new SimpleDateFormat("yyyy-MM-dd", Locale.GERMANY).parse(date);
32+
}
2833
}
2934
}
3035

library/src/main/java/me/proxer/library/api/user/InternalApi.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import me.proxer.library.api.ProxerCall;
44
import me.proxer.library.entity.user.TopTenEntry;
55
import me.proxer.library.entity.user.User;
6+
import me.proxer.library.entity.user.UserAbout;
67
import me.proxer.library.entity.user.UserComment;
78
import me.proxer.library.entity.user.UserHistoryEntry;
89
import me.proxer.library.entity.user.UserInfo;
@@ -44,6 +45,10 @@ ProxerCall<List<TopTenEntry>> topTen(@Query("uid") String userId,
4445
ProxerCall<UserInfo> userInfo(@Query("uid") String userId,
4546
@Query("username") String username);
4647

48+
@GET("user/about")
49+
ProxerCall<UserAbout> userAbout(@Query("uid") String userId,
50+
@Query("username") String username);
51+
4752
@GET("user/list")
4853
ProxerCall<List<UserMediaListEntry>> userMediaList(@Query("uid") String userId,
4954
@Query("username") String username,
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package me.proxer.library.api.user;
2+
3+
import lombok.experimental.Accessors;
4+
import me.proxer.library.api.Endpoint;
5+
import me.proxer.library.api.ProxerCall;
6+
import me.proxer.library.entity.user.UserAbout;
7+
8+
import javax.annotation.Nullable;
9+
10+
/**
11+
* Endpoint for requesting all information of an user.
12+
*
13+
* @author Ruben Gees
14+
*/
15+
@Accessors(fluent = true)
16+
public final class UserAboutEndpoint implements Endpoint<UserAbout> {
17+
18+
private final InternalApi internalApi;
19+
20+
@Nullable
21+
private final String userId;
22+
23+
@Nullable
24+
private final String username;
25+
26+
UserAboutEndpoint(final InternalApi internalApi, @Nullable final String userId, @Nullable final String username) {
27+
if (userId == null && username == null) {
28+
throw new IllegalArgumentException("You must pass either an userId or an username.");
29+
}
30+
31+
this.internalApi = internalApi;
32+
this.userId = userId;
33+
this.username = username;
34+
}
35+
36+
/**
37+
* {@inheritDoc}
38+
*/
39+
@Override
40+
public ProxerCall<UserAbout> build() {
41+
return internalApi.userAbout(userId, username);
42+
}
43+
}

library/src/main/java/me/proxer/library/api/user/UserApi.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ public UserInfoEndpoint info(@Nullable final String userId, @Nullable final Stri
5050
return new UserInfoEndpoint(internalApi, userId, username);
5151
}
5252

53+
/**
54+
* Returns the respective endpoint.
55+
*/
56+
public UserAboutEndpoint about(@Nullable final String userId, @Nullable final String username) {
57+
return new UserAboutEndpoint(internalApi, userId, username);
58+
}
59+
5360
/**
5461
* Returns the respective endpoint.
5562
*/
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package me.proxer.library.entity.user;
2+
3+
import com.squareup.moshi.Json;
4+
import lombok.EqualsAndHashCode;
5+
import lombok.Value;
6+
import me.proxer.library.enums.Gender;
7+
import me.proxer.library.enums.RelationshipStatus;
8+
9+
import javax.annotation.Nullable;
10+
import java.util.Date;
11+
12+
/**
13+
* Entity holding all info of a user.
14+
*
15+
* @author Ruben Gees
16+
*/
17+
@Value
18+
@EqualsAndHashCode(onParam = @__({@Nullable}))
19+
public class UserAbout {
20+
21+
/**
22+
* Returns the website of the user. Can be empty, a link or a name.
23+
*/
24+
@Json(name = "info_website")
25+
private String website;
26+
27+
/**
28+
* Returns the occupation of the user. Can be empty.
29+
*/
30+
@Json(name = "info_occupation")
31+
private String occupation;
32+
33+
/**
34+
* Returns the interests of the user. Can be empty.
35+
*/
36+
@Json(name = "info_interests")
37+
private String interests;
38+
39+
/**
40+
* Returns the city of the user. Can be empty.
41+
*/
42+
@Json(name = "info_city")
43+
private String city;
44+
45+
/**
46+
* Returns the country of the user. Can be empty.
47+
*/
48+
@Json(name = "info_country")
49+
private String country;
50+
51+
/**
52+
* Returns the about text of the user. Can be empty and can contain HTML-Tags.
53+
*/
54+
@Json(name = "info_about")
55+
private String about;
56+
57+
/**
58+
* Returns the facebook info of the user. Can be empty, a link or a name.
59+
*/
60+
@Json(name = "info_facebook")
61+
private String facebook;
62+
63+
/**
64+
* Returns the youtube info of the user. Can be empty, a link or a name.
65+
*/
66+
@Json(name = "info_youtube")
67+
private String youtube;
68+
69+
/**
70+
* Returns the chatango info of the user. Can be empty, a link or a name.
71+
*/
72+
@Json(name = "info_chatango")
73+
private String chatango;
74+
75+
/**
76+
* Returns the twitter info of the user. Can be empty, a link or a name.
77+
*/
78+
@Json(name = "info_twitter")
79+
private String twitter;
80+
81+
/**
82+
* Returns the skype info of the user. Can be empty, a link or a name.
83+
*/
84+
@Json(name = "info_skype")
85+
private String skype;
86+
87+
/**
88+
* Returns the deviantart info of the user. Can be empty, a link or a name.
89+
*/
90+
@Json(name = "info_deviantart")
91+
private String deviantart;
92+
93+
/**
94+
* Returns the birthday of the user. Can be an actual date or 00.00.0000. The format is dd.MM.yyyy.
95+
*/
96+
@Json(name = "info_birthday")
97+
private Date birthday;
98+
99+
/**
100+
* Returns the gender of the user.
101+
*/
102+
@Json(name = "info_gender")
103+
private Gender gender;
104+
105+
/**
106+
* Returns the relationship status of the user.
107+
*/
108+
@Json(name = "info_relationshipstatus")
109+
private RelationshipStatus relationshipStatus;
110+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package me.proxer.library.enums;
2+
3+
import com.serjltt.moshi.adapters.FallbackEnum;
4+
import com.squareup.moshi.Json;
5+
6+
/**
7+
* Enum holding the available genders.
8+
*
9+
* @author Ruben Gees
10+
*/
11+
@FallbackEnum(name = "OTHER")
12+
public enum Gender {
13+
@Json(name = "m") MALE,
14+
@Json(name = "f") FEMALE,
15+
@Json(name = "o") OTHER
16+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package me.proxer.library.enums;
2+
3+
import com.serjltt.moshi.adapters.FallbackEnum;
4+
import com.squareup.moshi.Json;
5+
6+
/**
7+
* Enum holding the available relationship statuses.
8+
*
9+
* @author Ruben Gees
10+
*/
11+
@FallbackEnum(name = "UNKNOWN")
12+
public enum RelationshipStatus {
13+
@Json(name = "single") SINGLE,
14+
@Json(name = "in_relation") IN_RELATION,
15+
@Json(name = "engaged") ENGAGED,
16+
@Json(name = "complicated") COMPLICATED,
17+
@Json(name = "married") MARRIED,
18+
@Json(name = "searching") SEARCHING,
19+
@Json(name = "not-searching") NOT_SEARCHING,
20+
@Json(name = "") UNKNOWN,
21+
}

library/src/test/java/me/proxer/library/api/DateAdapterTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ public void testFromJsonIso() throws ParseException {
3434
.isEqualTo(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.GERMANY).parse("2010-01-01 23:12:10"));
3535
}
3636

37+
@Test
38+
public void testFromJsonIsoNoTime() throws ParseException {
39+
assertThat(adapter.fromJson("2010-01-01"))
40+
.isEqualTo(new SimpleDateFormat("yyyy-MM-dd", Locale.GERMANY).parse("2010-01-01"));
41+
}
42+
43+
@Test
44+
public void testFromJsonIsoNoTimeEmpty() throws ParseException {
45+
assertThat(adapter.fromJson("0000-00-00"))
46+
.isEqualTo(new SimpleDateFormat("yyyy-MM-dd", Locale.GERMANY).parse("0000-00-00"));
47+
}
48+
3749
@Test
3850
public void testFromJsonMalformed() {
3951
assertThatExceptionOfType(ParseException.class).isThrownBy(() -> adapter.fromJson("malformed"));
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package me.proxer.library.api.user;
2+
3+
import me.proxer.library.ProxerTest;
4+
import me.proxer.library.api.ProxerException;
5+
import me.proxer.library.entity.user.UserAbout;
6+
import me.proxer.library.enums.Gender;
7+
import me.proxer.library.enums.RelationshipStatus;
8+
import okhttp3.mockwebserver.MockResponse;
9+
import org.junit.Test;
10+
11+
import java.io.IOException;
12+
import java.text.ParseException;
13+
import java.text.SimpleDateFormat;
14+
import java.util.Locale;
15+
16+
import static org.assertj.core.api.Assertions.assertThat;
17+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
18+
19+
/**
20+
* @author Ruben Gees
21+
*/
22+
public class UserAboutEndpointTest extends ProxerTest {
23+
24+
@Test
25+
public void testDefault() throws ProxerException, IOException, ParseException {
26+
server.enqueue(new MockResponse().setBody(fromResource("user_about.json")));
27+
28+
final UserAbout result = api.user()
29+
.about(null, "rubygee")
30+
.build()
31+
.execute();
32+
33+
assertThat(result).isEqualTo(buildTestAbout());
34+
}
35+
36+
@Test
37+
public void testPath() throws ProxerException, IOException, InterruptedException {
38+
server.enqueue(new MockResponse().setBody(fromResource("user_about.json")));
39+
40+
api.user().about("123", "rubygee")
41+
.build()
42+
.execute();
43+
44+
assertThat(server.takeRequest().getPath()).isEqualTo("/api/v1/user/about?uid=123&username=rubygee");
45+
}
46+
47+
@Test
48+
public void testUserIdAndUsernameNull() {
49+
assertThatExceptionOfType(IllegalArgumentException.class)
50+
.isThrownBy(() -> api.user().info(null, null));
51+
}
52+
53+
private UserAbout buildTestAbout() throws ParseException {
54+
return new UserAbout("", "Developer", "Anime", "A City", "Some Country",
55+
"<p>Hello there", "", "", "", "", "skypeTest",
56+
"", new SimpleDateFormat("yyyy-MM-dd", Locale.GERMANY).parse("0000-06-02"),
57+
Gender.MALE, RelationshipStatus.UNKNOWN);
58+
}
59+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package me.proxer.library.enums;
2+
3+
import me.proxer.library.ProxerTest;
4+
import org.junit.Test;
5+
6+
import java.io.IOException;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
/**
11+
* @author Ruben Gees
12+
*/
13+
public class GenderTest extends ProxerTest {
14+
15+
@Test
16+
public void testDefault() throws IOException {
17+
final Gender gender = api.moshi().adapter(Gender.class).fromJson("\"f\"");
18+
19+
assertThat(gender).isSameAs(Gender.FEMALE);
20+
}
21+
22+
@Test
23+
public void testFallback() throws IOException {
24+
final Gender gender = api.moshi().adapter(Gender.class).fromJson("\"xyz\"");
25+
26+
assertThat(gender).isSameAs(Gender.OTHER);
27+
}
28+
}

0 commit comments

Comments
 (0)