|
| 1 | +/** |
| 2 | + * Copyright (c) 2016-2017 Oracle and/or its affiliates. All rights reserved. |
| 3 | + */ |
| 4 | +package com.oracle.bmc.audit; |
| 5 | + |
| 6 | +import com.oracle.bmc.ClientConfiguration; |
| 7 | +import com.oracle.bmc.auth.AuthenticationDetailsProvider; |
| 8 | +import com.oracle.bmc.http.ClientConfigurator; |
| 9 | +import com.oracle.bmc.http.internal.*; |
| 10 | +import com.oracle.bmc.responses.*; |
| 11 | +import com.oracle.bmc.util.internal.*; |
| 12 | + |
| 13 | +import com.google.common.base.Function; |
| 14 | +import com.google.common.base.Optional; |
| 15 | + |
| 16 | +import java.util.concurrent.Future; |
| 17 | + |
| 18 | +import javax.ws.rs.client.*; |
| 19 | +import javax.ws.rs.core.*; |
| 20 | + |
| 21 | +import com.oracle.bmc.audit.internal.http.*; |
| 22 | +import com.oracle.bmc.audit.requests.*; |
| 23 | +import com.oracle.bmc.audit.responses.*; |
| 24 | + |
| 25 | +import com.oracle.bmc.*; |
| 26 | + |
| 27 | +import lombok.AccessLevel; |
| 28 | +import lombok.Getter; |
| 29 | +import lombok.extern.slf4j.Slf4j; |
| 30 | + |
| 31 | +@javax.annotation.Generated(value = "OracleSDKGenerator", comments = "API Version: 20160918") |
| 32 | +@Slf4j |
| 33 | +public class AuditAsyncClient implements AuditAsync { |
| 34 | + /** |
| 35 | + * Service instance for Audit. |
| 36 | + */ |
| 37 | + public static final Service SERVICE = Services.create("AUDIT", "audit"); |
| 38 | + |
| 39 | + @Getter(value = AccessLevel.PACKAGE) |
| 40 | + private final RestClient client; |
| 41 | + |
| 42 | + /** |
| 43 | + * Creates a new service instance using the given authentication provider. |
| 44 | + * @param authenticationProvider The authentication details provider, required. |
| 45 | + */ |
| 46 | + public AuditAsyncClient(AuthenticationDetailsProvider authenticationProvider) { |
| 47 | + this.client = newClientBuilder().build().create(SERVICE, authenticationProvider); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Creates a new service instance using the given authentication provider and client configuration. |
| 52 | + * @param authenticationProvider The authentication details provider, required. |
| 53 | + * @param configuration The client configuration, optional. |
| 54 | + */ |
| 55 | + public AuditAsyncClient( |
| 56 | + AuthenticationDetailsProvider authenticationProvider, |
| 57 | + ClientConfiguration configuration) { |
| 58 | + this.client = |
| 59 | + newClientBuilder().build().create(SERVICE, authenticationProvider, configuration); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Creates a new service instance using the given authentication provider and client configuration. Additionally, |
| 64 | + * a Consumer can be provided that will be invoked whenever a REST Client is created to allow for additional configuration/customization. |
| 65 | + * @param authenticationProvider The authentication details provider, required. |
| 66 | + * @param configuration The client configuration, optional. |
| 67 | + * @param clientConfigurator ClientConfigurator that will be invoked for additional configuration of a REST client, optional. |
| 68 | + */ |
| 69 | + public AuditAsyncClient( |
| 70 | + AuthenticationDetailsProvider authenticationProvider, |
| 71 | + ClientConfiguration configuration, |
| 72 | + ClientConfigurator clientConfigurator) { |
| 73 | + this.client = |
| 74 | + newClientBuilder() |
| 75 | + .clientConfigurator(clientConfigurator) |
| 76 | + .build() |
| 77 | + .create(SERVICE, authenticationProvider, configuration); |
| 78 | + } |
| 79 | + |
| 80 | + private static RestClientFactoryBuilder newClientBuilder() { |
| 81 | + RestClientFactoryBuilder builder = RestClientFactoryBuilder.builder(); |
| 82 | + |
| 83 | + return builder; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void setEndpoint(String endpoint) { |
| 88 | + LOG.info("Setting endpoint to {}", endpoint); |
| 89 | + client.setEndpoint(endpoint); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void setRegion(Region region) { |
| 94 | + Optional<String> endpoint = region.getEndpoint(SERVICE); |
| 95 | + if (endpoint.isPresent()) { |
| 96 | + setEndpoint(endpoint.get()); |
| 97 | + } else { |
| 98 | + throw new IllegalArgumentException( |
| 99 | + "Endpoint for " + SERVICE + " is not known in region " + region); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void setRegion(String regionId) { |
| 105 | + regionId = regionId.toLowerCase(); |
| 106 | + try { |
| 107 | + Region region = Region.fromRegionId(regionId); |
| 108 | + setRegion(region); |
| 109 | + } catch (IllegalArgumentException e) { |
| 110 | + LOG.info("Unknown regionId '{}', falling back to default endpoint format", regionId); |
| 111 | + String endpoint = Region.formatDefaultRegionEndpoint(SERVICE, regionId); |
| 112 | + setEndpoint(endpoint); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public void close() { |
| 118 | + client.close(); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public Future<ListEventsResponse> listEvents( |
| 123 | + ListEventsRequest request, |
| 124 | + AsyncHandler<ListEventsRequest, ListEventsResponse> handler) { |
| 125 | + LOG.trace("Called async listEvents"); |
| 126 | + Invocation.Builder ib = ListEventsConverter.fromRequest(client, request); |
| 127 | + Function<Response, ListEventsResponse> transformer = ListEventsConverter.fromResponse(); |
| 128 | + |
| 129 | + Consumer<Response> onSuccess = new SuccessConsumer<>(handler, transformer, request); |
| 130 | + Consumer<Throwable> onError = new ErrorConsumer<>(handler, request); |
| 131 | + |
| 132 | + Future<Response> responseFuture = client.get(ib, request, onSuccess, onError); |
| 133 | + return new TransformingFuture<>(responseFuture, transformer); |
| 134 | + } |
| 135 | +} |
0 commit comments