All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gobblin.util.limiter.RestClientRequestSender Maven / Gradle / Ivy

There is a newer version: 0.11.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 gobblin.util.limiter;

import com.linkedin.common.callback.Callback;
import com.linkedin.restli.client.Request;
import com.linkedin.restli.client.Response;
import com.linkedin.restli.client.RestClient;
import com.linkedin.restli.common.ComplexResourceKey;
import com.linkedin.restli.common.EmptyRecord;

import gobblin.restli.throttling.PermitAllocation;
import gobblin.restli.throttling.PermitRequest;
import gobblin.restli.throttling.PermitsGetRequestBuilder;
import gobblin.restli.throttling.PermitsRequestBuilders;

import lombok.AllArgsConstructor;


/**
 * Sends requests to a server using a {@link RestClient}. Subclasses can decorate the callback to intercept
 * certain response statuses.
 */
@AllArgsConstructor
public abstract class RestClientRequestSender implements RequestSender {
  @Override
  public void sendRequest(PermitRequest request, Callback> callback) {
    PermitsGetRequestBuilder getBuilder = new PermitsRequestBuilders().get();
    Request fullRequest = getBuilder.id(new ComplexResourceKey<>(request, new EmptyRecord())).build();
    getRestClient().sendRequest(fullRequest, decorateCallback(request, callback));
  }

  /**
   * Decorate the callback to intercept some responses.
   */
  protected Callback> decorateCallback(PermitRequest request,
      Callback> callback) {
    return callback;
  }

  /**
   * @return The {@link RestClient} to use to send the request.
   */
  protected abstract RestClient getRestClient();
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy