-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathListingLifecycleApiClient.java
More file actions
95 lines (79 loc) · 3.7 KB
/
Copy pathListingLifecycleApiClient.java
File metadata and controls
95 lines (79 loc) · 3.7 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//------------------------------------------------------------------
// 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.listing.CreatedAdResponse;
import ecg.move.sellermodel.listing.Listing;
import java.util.Optional;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
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;
/*
The Listing API is the main API you likely will interact with. It offers a number of resources allowing you do basic
CRUD operations. It furthermore supports basic state transitions like "pause a listing", "make a listing visible"
and so on.
Whenever the content of a listing is affected, the listing is subject to validation, which ensures that the content
provided by the caller is sufficient and understandable. Validation errors result in a 400 response with errors
description(s) in the response body.
*/
public class ListingLifecycleApiClient {
private final String bearerToken;
private final String myPartnerId;
private final String moveBaseUrl;
public ListingLifecycleApiClient(
String bearerToken,
String myPartnerId,
String moveBaseUrl) {
this.bearerToken = bearerToken;
this.myPartnerId = myPartnerId;
this.moveBaseUrl = moveBaseUrl;
}
public Optional<Listing> getListing(String foreignId) {
WebTarget webTarget = createWebtarget()
.path("partners/{partnerId}/listings/{foreignId}")
.resolveTemplate("partnerId", myPartnerId)
.resolveTemplate("foreignId", foreignId);
Response response = webTarget
.request()
.header(HttpHeaders.AUTHORIZATION, "Bearer " + bearerToken)
.accept(MediaType.APPLICATION_JSON)
.get();
StatusType statusInfo = response.getStatusInfo();
if (!statusInfo.getFamily().equals(Family.SUCCESSFUL) && statusInfo.getStatusCode() != 404) {
throw new IllegalStateException(format("Unexpected response while GETting a listing: %s", statusInfo));
}
return Optional.ofNullable(response.readEntity(Listing.class));
}
public String publishListing(Listing listing, String myListingId) {
WebTarget webTarget = createWebtarget()
.path("partners/{partnerId}/listings/{myListingId}")
.resolveTemplate("partnerId", myPartnerId)
.resolveTemplate("myListingId", myListingId);
Response response = webTarget
.request()
.header(HttpHeaders.AUTHORIZATION, "Bearer " + bearerToken)
.accept(MediaType.APPLICATION_JSON)
.put(Entity.entity(listing, MediaType.APPLICATION_JSON));
StatusType statusInfo = response.getStatusInfo();
if (!statusInfo.getFamily().equals(Family.SUCCESSFUL)) {
throw new IllegalStateException(format("Unexpected response while PUTting a listing: '%s', response: '%s'", statusInfo, response.readEntity(String.class)));
}
return Optional.ofNullable(response.readEntity(CreatedAdResponse.class))
.map(CreatedAdResponse::getMoveListingId)
.orElseThrow();
}
private WebTarget createWebtarget() {
Client client = ClientBuilder.newClient();
return client.target(moveBaseUrl);
}
}