This repository has been archived by the owner on Sep 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from duffelhq/FLIN-2981-ratelimit
FLIN-2981 Parse rate limit exceptions and return the reset time
- Loading branch information
Showing
8 changed files
with
91 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/java/com/duffel/exception/RateLimitException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.duffel.exception; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
/** | ||
* HTTP 429 rate limited | ||
*/ | ||
@EqualsAndHashCode(callSuper = true) | ||
@Getter | ||
@Setter | ||
@ToString(callSuper = true) | ||
public class RateLimitException extends DuffelException { | ||
|
||
/** | ||
* When will the rate limit reset again. | ||
*/ | ||
private LocalDateTime rateLimitReset; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
import com.duffel.model.request.Payment; | ||
import com.duffel.model.request.ServiceRequest; | ||
import com.duffel.model.response.*; | ||
import com.duffel.model.response.order.metadata.CancelForAnyReasonMetadata; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.junit.jupiter.api.Test; | ||
|
@@ -31,14 +32,14 @@ void bookWithCancelForAnyReason() { | |
|
||
// Create an offer request | ||
OfferRequest.Slice slice = new OfferRequest.Slice(); | ||
slice.setDepartureDate(LocalDate.now().plusDays(60).format(DateTimeFormatter.ISO_DATE)); | ||
slice.setOrigin("LGW"); | ||
slice.setDestination("CDG"); | ||
slice.setDepartureDate(LocalDate.now().plusDays(100).format(DateTimeFormatter.ISO_DATE)); | ||
slice.setOrigin("LHR"); | ||
slice.setDestination("FRA"); | ||
|
||
Passenger passenger = new Passenger(); | ||
passenger.setType(PassengerType.adult); | ||
passenger.setGivenName("Test"); | ||
passenger.setFamilyName("User"); | ||
passenger.setFamilyName("Cancel"); | ||
|
||
OfferRequest request = new OfferRequest(); | ||
request.setMaxConnections(0); | ||
|
@@ -60,6 +61,8 @@ void bookWithCancelForAnyReason() { | |
|
||
Service cfar = offer.getAvailableServices().stream().filter(s -> ServiceType.Type.cancel_for_any_reason == s.getServiceType()).findFirst().orElseThrow(); | ||
LOG.info("🧨 Cancel For Any Reason service available with cost {}{}", cfar.getTotalCurrency(), cfar.getTotalAmount()); | ||
LOG.info("🔐 T&Cs {}", ((CancelForAnyReasonMetadata) cfar.getMetadata()).getTermsAndConditionsUrl()); | ||
LOG.info("📝 Copy {}", ((CancelForAnyReasonMetadata) cfar.getMetadata()).getMerchantCopy()); | ||
|
||
BigDecimal newOrderTotalCost = offer.getTotalAmount().add(cfar.getTotalAmount()); | ||
LOG.info("💳 Cost of flight offer plus CFAR is {}{}", offer.getTotalCurrency(), newOrderTotalCost); | ||
|
@@ -68,7 +71,7 @@ void bookWithCancelForAnyReason() { | |
OrderPassenger orderPassenger = new OrderPassenger(); | ||
orderPassenger.setEmail("[email protected]"); | ||
orderPassenger.setGivenName("Test"); | ||
orderPassenger.setFamilyName("User"); | ||
orderPassenger.setFamilyName("Cancel"); | ||
orderPassenger.setTitle("Ms"); | ||
orderPassenger.setBornOn("1990-01-01"); | ||
orderPassenger.setPassengerType(PassengerType.adult); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/test/resources/fixtures/exceptions/429_rate_limit.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"meta": { | ||
"status": 429, | ||
"request_id": "F0J5ZCaJVEn580QADT4B" | ||
}, | ||
"errors": [ | ||
{ | ||
"type": "rate_limit_error", | ||
"title": "Rate limit exceeded", | ||
"message": "Too many requests hit the API too quickly. Please retry your request after the time specified in the `ratelimit-reset` header.", | ||
"documentation_url": "https://duffel.com/docs/api/overview/errors", | ||
"code": "rate_limit_exceeded" | ||
} | ||
] | ||
} |