Status RequestBuilder::FillAllocateQuotaRequest()

in src/api_proxy/service_control/request_builder.cc [994:1049]


Status RequestBuilder::FillAllocateQuotaRequest(
    const QuotaRequestInfo& info,
    ::google::api::servicecontrol::v1::AllocateQuotaRequest* request) const {
  ::google::api::servicecontrol::v1::QuotaOperation* operation =
      request->mutable_allocate_operation();

  // service_name
  request->set_service_name(service_name_);
  // service_config_id
  request->set_service_config_id(service_config_id_);

  // allocate_operation.operation_id
  if (!info.operation_id.empty()) {
    operation->set_operation_id(info.operation_id);
  }
  // allocate_operation.method_name
  if (!info.method_name.empty()) {
    operation->set_method_name(info.method_name);
  }
  // allocate_operation.consumer_id
  if (!info.api_key.empty()) {
    // For quota request, we send the API key as is.
    operation->set_consumer_id(std::string(kConsumerIdApiKey) +
                               std::string(info.api_key));
  } else if (!info.producer_project_id.empty()) {
    operation->set_consumer_id(std::string(kConsumerIdProject) +
                               std::string(info.producer_project_id));
  }

  // allocate_operation.quota_mode
  operation->set_quota_mode(
      ::google::api::servicecontrol::v1::QuotaOperation_QuotaMode::
          QuotaOperation_QuotaMode_NORMAL);

  // allocate_operation.labels
  auto* labels = operation->mutable_labels();
  if (!info.client_ip.empty()) {
    (*labels)[kServiceControlCallerIp] = info.client_ip;
  }

  if (!info.referer.empty()) {
    (*labels)[kServiceControlReferer] = info.referer;
  }
  (*labels)[kServiceControlUserAgent] = kUserAgent;
  (*labels)[kServiceControlServiceAgent] = get_service_agent();

  for (auto metric : info.metric_cost_vector) {
    MetricValueSet* value_set = operation->add_quota_metrics();
    value_set->set_metric_name(metric.first);
    MetricValue* value = value_set->add_metric_values();
    const auto& cost = metric.second;
    value->set_int64_value(cost <= 0 ? 1 : cost);
  }

  return OkStatus();
}