Skip to content

Commit

Permalink
Remove compile dependency on openapi-generator
Browse files Browse the repository at this point in the history
- Replace usage of commons-lang's StringUtils in RetryUtil (add unit test)
  • Loading branch information
clementdenis committed Jun 26, 2023
1 parent f162c3d commit d507dce
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 44 deletions.
28 changes: 0 additions & 28 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,34 +137,6 @@
<artifactId>handlebars-jackson2</artifactId>
<version>${com.github.jknack.handlebars.version}</version>
</dependency>

<dependency>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator</artifactId>
<version>${openapi-generator.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
<exclusion>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
</exclusion>
<exclusion>
<groupId>com.github.jknack</groupId>
<artifactId>handlebars</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
Expand Down
11 changes: 0 additions & 11 deletions impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,6 @@
<groupId>com.okta.sdk</groupId>
<artifactId>okta-sdk-api</artifactId>
</dependency>
<dependency>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator</artifactId>
<version>6.4.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
16 changes: 11 additions & 5 deletions impl/src/main/java/com/okta/sdk/impl/retry/RetryUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.okta.sdk.impl.retry;

import org.apache.commons.lang3.StringUtils;
import com.google.common.primitives.Longs;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.ProtocolException;
Expand All @@ -32,6 +32,7 @@ public class RetryUtil {
* Maximum exponential back-off time before retrying a request
*/
private static final int DEFAULT_MAX_BACKOFF_IN_MILLISECONDS = 20 * 1000;
static final String X_RATE_LIMIT_RESET = "x-rate-limit-reset";

static long getDefaultDelayMillis(int retries) {
long scaleFactor = 300;
Expand All @@ -43,7 +44,7 @@ static long getDefaultDelayMillis(int retries) {

static long get429DelayMillis(HttpResponse response) {
// the time at which the rate limit will reset, specified in UTC epoch time.
long resetLimit = getRateLimitResetValue(response);
long resetLimit = getRateLimitResetValue(response.getFirstHeader(X_RATE_LIMIT_RESET));
if (resetLimit == -1L) {
return -1;
}
Expand Down Expand Up @@ -74,8 +75,13 @@ static Date dateFromHeader(HttpResponse response) {
return result;
}

static long getRateLimitResetValue(HttpResponse response) {
Header header = response.getFirstHeader("x-rate-limit-reset");
return header != null && StringUtils.isNotBlank(header.getValue()) ? Long.parseLong(header.getValue()) : -1L;
static long getRateLimitResetValue(Header resetHeader) {
if (resetHeader != null && resetHeader.getValue() != null) {
Long reset = Longs.tryParse(resetHeader.getValue());
if (reset != null) {
return reset;
}
}
return -1L;
}
}
52 changes: 52 additions & 0 deletions impl/src/test/groovy/com/okta/sdk/impl/retry/RetryUtilTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2020-Present Okta, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.okta.sdk.impl.retry

import org.apache.hc.core5.http.message.MessageSupport
import org.testng.annotations.Test

import static com.okta.sdk.impl.retry.RetryUtil.getRateLimitResetValue
import static org.testng.Assert.assertEquals

/**
* Test for {@link RetryUtil} class
*
* @since 12.0.0
*/
class RetryUtilTest {

@Test
void testGetRateLimitResetValue() {
def now = System.currentTimeMillis()
assertEquals(getRateLimitResetValue(MessageSupport.format(RetryUtil.X_RATE_LIMIT_RESET, now.toString())), now)
}

@Test
void testGetRateLimitResetValue_nullHeader() {
assertEquals(getRateLimitResetValue(null), -1L)
}

@Test
void testGetRateLimitResetValue_nullValue() {
assertEquals(getRateLimitResetValue(MessageSupport.format(RetryUtil.X_RATE_LIMIT_RESET, null)), -1L)
}

@Test
void testGetRateLimitResetValue_alphaValue() {
assertEquals(getRateLimitResetValue(MessageSupport.format(RetryUtil.X_RATE_LIMIT_RESET, "abc")), -1L)
}

}

0 comments on commit d507dce

Please sign in to comment.