Commit c3ca0e25 authored by Muskan Srivastava's avatar Muskan Srivastava
Browse files

added http client, retry configs and entitlements Factory

parent c398b41a
Pipeline #42931 failed with stage
in 11 seconds
......@@ -37,7 +37,7 @@
<azure.appinsights.log4j.version>2.6.3</azure.appinsights.log4j.version>
<azure.spring.boot.version>3.4.0</azure.spring.boot.version>
<lombok.version>1.18.16</lombok.version>
<osdu.oscorecommon.version>0.9.0-rc13</osdu.oscorecommon.version>
<osdu.oscorecommon.version>0.9.0-SNAPSHOT</osdu.oscorecommon.version>
<azure-mgmt-eventgrid.version>1.0.0-beta-3</azure-mgmt-eventgrid.version>
<azure-security-keyvault-keys.version>4.2.3</azure-security-keyvault-keys.version>
<documentdb-bulkexecutor.version>2.12.0</documentdb-bulkexecutor.version>
......
package org.opengroup.osdu.azure.di;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Configuration bean for setting up retry and timeouts variables.
*/
@Data
@ConfigurationProperties(prefix = "azure.retryAndTimeout")
public class RetryAndTimeoutsConfiguration {
private int retryCountForServiceUnavailableStrategy = 3;
private int connectTimeoutInMillis = 60000;
private int connectionRequestTimeout = 60000;
private int socketTimeout = 60000;
}
package org.opengroup.osdu.azure.entitlements;
import org.opengroup.osdu.core.common.entitlements.EntitlementsAPIConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.AbstractFactoryBean;
import org.springframework.stereotype.Component;
/**
* Creates bean of EntitlementsAPIConfig
*/
@Component
public class EntilementsAPIConfigBean extends AbstractFactoryBean<EntitlementsAPIConfig> {
@Value("${AUTHORIZE_API}")
private String AUTHORIZE_API;
@Value("${AUTHORIZE_API_KEY:}")
private String AUTHORIZE_API_KEY;
/**
* Abstract method of AbstractBeanFactory<T>.
*
* @return class type
*/
@Override
public Class<?> getObjectType() {
return EntitlementsAPIConfig.class;
}
/**
* Abstract method of AbstractBeanFactory<T> type.
*
* @return EntitlementsAPIConfig
* @throws Exception
*/
@Override
protected EntitlementsAPIConfig createInstance() throws Exception {
return EntitlementsAPIConfig
.builder()
.rootUrl(AUTHORIZE_API)
.apiKey(AUTHORIZE_API_KEY)
.build();
}
}
package org.opengroup.osdu.azure.entitlements;
import org.opengroup.osdu.core.common.entitlements.EntitlementsAPIConfig;
import org.opengroup.osdu.core.common.entitlements.EntitlementsService;
import org.opengroup.osdu.core.common.entitlements.IEntitlementsFactory;
import org.opengroup.osdu.core.common.entitlements.IEntitlementsService;
import org.opengroup.osdu.core.common.http.IHttpClient;
import org.opengroup.osdu.core.common.http.json.HttpResponseBodyMapper;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import java.util.Objects;
/**
* Implements IEntitlementsFactory.
*/
@Component
@Primary
@ConditionalOnProperty(value = "entitlements.factory.azure.enabled", havingValue = "true", matchIfMissing = true)
public class EntitlementsFactoryAzure implements IEntitlementsFactory {
private final EntitlementsAPIConfig config;
private final HttpResponseBodyMapper mapper;
private final IHttpClient client;
/**
* Constructor Injection for above 3 fields.
*
* @param config EntitlementsAPIConfig
* @param mapper HttpResponseBodyMapper
* @param client IHttpClient
*/
@Autowired
public EntitlementsFactoryAzure(EntitlementsAPIConfig config, HttpResponseBodyMapper mapper, IHttpClient client) {
this.config = config;
this.mapper = mapper;
this.client = client;
}
/**
* returns instance of EntitlementsService.
*
* @param headers DpsHeaders
* @return IEntitlementsService
*/
@Override
public IEntitlementsService create(DpsHeaders headers) {
Objects.requireNonNull(headers, "headers cannot be null");
return new EntitlementsService(this.config,
this.client,
headers,
this.mapper);
}
}
package org.opengroup.osdu.azure.httpconfig;
import org.apache.http.HttpStatus;
import org.opengroup.osdu.core.common.http.FetchServiceHttpRequest;
import org.opengroup.osdu.core.common.http.HttpRequest;
import org.opengroup.osdu.core.common.http.IHttpClient;
import org.opengroup.osdu.core.common.http.HttpResponse;
import org.opengroup.osdu.core.common.http.UrlFetchServiceImpl;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import java.net.URISyntaxException;
/**
* Extends URlFetchService and implements IHttpClient to send requests.
*/
@Primary
@Component
public class HttpClientAzure extends UrlFetchServiceImpl implements IHttpClient {
/**
* calls urlfetchservice's send request.
*
* @param httpRequest made by user class
* @return HttpResponse
*/
@Override
public HttpResponse send(HttpRequest httpRequest) {
org.opengroup.osdu.core.common.model.http.HttpResponse response = null;
try {
response = super.sendRequest(FetchServiceHttpRequest.builder()
.body(httpRequest.getBody())
.httpMethod(httpRequest.getHttpMethod())
.queryParams(httpRequest.getQueryParams())
.url(httpRequest.getUrl())
.headers(httpRequest.getHeaders())
.build());
} catch (URISyntaxException e) {
new AppException(HttpStatus.SC_BAD_REQUEST, e.getReason(), "URI Syntax is not correct");
}
HttpResponse httpResponse = new HttpResponse();
httpResponse.setBody(response.getBody());
httpResponse.setResponseCode(response.getResponseCode());
httpResponse.setContentType(response.getContentType());
httpResponse.setRequest(httpRequest);
return httpResponse;
}
}
package org.opengroup.osdu.azure.httpconfig;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpRequestBase;
import org.opengroup.osdu.azure.di.RetryAndTimeoutsConfiguration;
import org.opengroup.osdu.core.common.http.HttpClientHandler;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.model.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
/**
* Extends HttpClientHandler.
*/
@Primary
@Component
public class HttpClientHandlerAzure extends HttpClientHandler {
@Autowired
private RetryAndTimeoutsConfiguration configuration;
/**
* Constructor to set request and timeout configuration of HttpClientHandler.
*/
public HttpClientHandlerAzure() {
super.REQUEST_CONFIG = RequestConfig.custom()
.setConnectTimeout(configuration.getConnectTimeoutInMillis())
.setConnectionRequestTimeout(configuration.getConnectTimeoutInMillis())
.setSocketTimeout(configuration.getSocketTimeout()).build();
super.RETRY_COUNT = configuration.getRetryCountForServiceUnavailableStrategy();
}
/**
* same as the HttpClientHandler's send request.
*
* @param request HttpRequestBase
* @param requestHeaders DpsHeaders
* @return HttpResponse
*/
public HttpResponse sendRequest(HttpRequestBase request, DpsHeaders requestHeaders) {
return super.sendRequest(request, requestHeaders);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment