|
| 1 | +package org.maproulette.client.api; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.Optional; |
| 5 | + |
| 6 | +import org.apache.commons.lang.StringUtils; |
| 7 | +import org.maproulette.client.connection.IMapRouletteConnection; |
| 8 | +import org.maproulette.client.connection.MapRouletteConfiguration; |
| 9 | +import org.maproulette.client.connection.MapRouletteConnection; |
| 10 | +import org.maproulette.client.connection.Query; |
| 11 | +import org.maproulette.client.exception.MapRouletteException; |
| 12 | +import org.maproulette.client.model.User; |
| 13 | + |
| 14 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 15 | + |
| 16 | +import lombok.RequiredArgsConstructor; |
| 17 | + |
| 18 | +/** |
| 19 | + * That User service handles the API requests for Users |
| 20 | + * |
| 21 | + * @author pdevkota1 |
| 22 | + */ |
| 23 | +@RequiredArgsConstructor |
| 24 | +public class UserAPI |
| 25 | +{ |
| 26 | + |
| 27 | + private final ObjectMapper mapper = new ObjectMapper(); |
| 28 | + private final IMapRouletteConnection connection; |
| 29 | + |
| 30 | + public UserAPI(final MapRouletteConfiguration configuration) |
| 31 | + { |
| 32 | + this(new MapRouletteConnection(configuration)); |
| 33 | + } |
| 34 | + |
| 35 | + public Optional<User> getPublicFromId(final long identifier) throws MapRouletteException |
| 36 | + { |
| 37 | + final var query = Query.builder() |
| 38 | + .get(String.format(QueryConstants.URI_USER_PUBLIC_BY_ID, identifier)).build(); |
| 39 | + return this.parseResponse(this.connection.execute(query).orElse("")); |
| 40 | + } |
| 41 | + |
| 42 | + private Optional<User> parseResponse(final String response) throws MapRouletteException |
| 43 | + { |
| 44 | + if (StringUtils.isEmpty(response)) |
| 45 | + { |
| 46 | + return Optional.empty(); |
| 47 | + } |
| 48 | + try |
| 49 | + { |
| 50 | + return Optional.of(this.mapper.readValue(response, User.class)); |
| 51 | + } |
| 52 | + catch (final IOException e) |
| 53 | + { |
| 54 | + throw new MapRouletteException(e); |
| 55 | + } |
| 56 | + } |
| 57 | +} |
0 commit comments