-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDealerCatalogApiClient.java
More file actions
57 lines (47 loc) · 2.03 KB
/
DealerCatalogApiClient.java
File metadata and controls
57 lines (47 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//------------------------------------------------------------------
// Copyright 2020 mobile.de GmbH.
// Author/Developer: Philipp Bartsch
//
// This code is licensed under MIT license (see LICENSE for details)
//------------------------------------------------------------------
package org.example.moveclient;
import static java.lang.String.format;
import ecg.move.sellermodel.dealer.DealerOverviewResponse;
import java.util.List;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status.Family;
import javax.ws.rs.core.Response.StatusType;
public class DealerCatalogApiClient {
private final String bearerToken;
private final String moveBaseUrl;
public DealerCatalogApiClient(
String bearerToken,
String moveBaseUrl) {
this.bearerToken = bearerToken;
this.moveBaseUrl = moveBaseUrl;
}
public List<DealerOverviewResponse> getDealerCatalog() {
Client client = ClientBuilder.newClient();
WebTarget webTarget = client.target(moveBaseUrl)
.path("/v2/dealers")
.queryParam("page", 0)
.queryParam("pageSize", 1000); // using hard coded page sizes here for simplicity
Response response = webTarget
.request()
.header(HttpHeaders.AUTHORIZATION, "Bearer " + bearerToken)
.accept(MediaType.APPLICATION_JSON)
.get();
StatusType statusInfo = response.getStatusInfo();
if (!statusInfo.getFamily().equals(Family.SUCCESSFUL)) {
throw new IllegalStateException(format("Unexpected response while GETting list of known dealers: %s, reason: %s", statusInfo, response.readEntity(String.class)));
}
List<DealerOverviewResponse> dealerOverviewResponses = response.readEntity(new GenericType<>() {});
return dealerOverviewResponses;
}
}