Criar um veículo de entrega para tarefas programadas

Este documento descreve como criar um veículo em um ambiente de servidor usando gRPC ou REST. Você pode criar um veículo no SDK do Driver, desde que tenha provisionado o app como um ambiente confiável usando as credenciais adequadas.

Para entender como usar o SDK do Driver para criar veículos, consulte o seguinte:

Para criar um novo veículo em um ambiente de servidor, faça uma solicitação CreateDeliveryVehicle ao Fleet Engine. Use o objeto CreateDeliveryVehicleRequest para definir os atributos do novo veículo de entrega.

Campos para veículos de tarefas programadas

Ao criar um DeliveryVehicle, defina os seguintes campos opcionais:

  • attributes
  • last_location
  • type

Para criar um veículo sem definir nenhum campo opcional, deixe o DeliveryVehicle campo não definido no CreateDeliveryVehicleRequest.

Exemplo de criação de veículo

Para criar um veículo, use a biblioteca gRPC Java ou REST. Para conferir uma lista completa de idiomas com suporte, consulte Bibliotecas de cliente para serviços de tarefas programadas.

Java

  static final String PROJECT_ID = "my-delivery-co-gcp-project";
  static final String VEHICLE_ID = "vehicle-8241890"; // Avoid auto-incrementing IDs.

  DeliveryServiceBlockingStub deliveryService =
    DeliveryServiceGrpc.newBlockingStub(channel);

  // Vehicle settings
  String parent = "providers/" + PROJECT_ID;
  DeliveryVehicle vehicle = DeliveryVehicle.newBuilder()
    .addAttributes(DeliveryVehicleAttribute.newBuilder()
      .setKey("route_number").setValue("1"))  // Opaque to the Fleet Engine
    .build();

  // Vehicle request
  CreateDeliveryVehicleRequest createVehicleRequest =
    CreateDeliveryVehicleRequest.newBuilder()  // No need for the header
        .setParent(parent)
        .setDeliveryVehicleId(VEHICLE_ID)     // Vehicle ID assigned by the Provider
        .setDeliveryVehicle(vehicle)
        .build();

  // Error handling
  // If Fleet Engine does not have vehicle with that ID and the credentials of the
  // requestor pass, the service creates the vehicle successfully.

  try {
    DeliveryVehicle createdVehicle =
      deliveryService.createDeliveryVehicle(createVehicleRequest);
  } catch (StatusRuntimeException e) {
    Status s = e.getStatus();
    switch (s.getCode()) {
       case ALREADY_EXISTS:
         break;
       case PERMISSION_DENIED:
         break;
    }
    return;
  }

REST

Para criar um veículo em um ambiente de servidor, faça uma chamada HTTP REST para CreateDeliveryVehicle:

POST https://fleetengine.googleapis.com/v1/providers/<project_id>/deliveryVehicles?deliveryVehicleId=<id>

O corpo POST representa a entidade DeliveryVehicle a ser criada. É possível especificar estes campos opcionais:

  • attributes
  • lastLocation
  • type
  # Set $JWT, $PROJECT_ID, and $VEHICLE_ID in the local
  # environment
  curl -X POST "https://fleetengine.googleapis.com/v1/providers/${PROJECT_ID}/deliveryVehicles?deliveryVehicleId=${VEHICLE_ID}" \
    -H "Content-type: application/json" \
    -H "Authorization: Bearer ${JWT}" \
  --data-binary @- << EOM
  {
    "attributes": [{"key": "model", "value": "sedan"}],
    "lastLocation": {"location": {"latitude": 12.1, "longitude": 14.5}}
  }
  EOM

Para criar um veículo sem definir nenhum campo, deixe o corpo da solicitação POST vazio. O veículo recém-criado extrai um ID do veículo do parâmetro deliveryVehicleId no URL POST.

Exemplo:

  # Set $JWT, $PROJECT_ID, and $VEHICLE_ID in the local
  # environment
  curl -X POST "https://fleetengine.googleapis.com/v1/providers/${PROJECT_ID}/deliveryVehicles?deliveryVehicleId=${VEHICLE_ID}" \
    -H "Content-type: application/json" \
    -H "Authorization: Bearer ${JWT}"

A seguir