Görevleri toplu olarak oluşturma

Bu belgede, gRPC veya REST kullanarak sunucu ortamından nasıl görev grubu oluşturacağınız açıklanmaktadır. Görev oluşturma hakkında daha fazla bilgi için aşağıdaki makaleleri inceleyin:

Toplu görev oluşturma için görev alanları

Görevleri toplu olarak oluştururken requests içindeki her CreateTasksRequest öğesi, tek bir görev için CreateTask isteğiyle aynı doğrulama kurallarını geçmelidir. parent ve header alanları isteğe bağlıdır. Ayarlanmışsa üst düzeydeki ilgili alanlarıyla aynı olmalıdırlar BatchCreateTasksRequest.

Daha fazla bilgi için BatchCreateTasks gRPC veya REST API referans belgelerine bakın.

Zorunlu toplu alanlar

AlanDeğer
istek Array<CreateTasksRequest>

İsteğe bağlı toplu görev alanları

AlanDeğer
başlık DeliveryRequestHeader

Toplu görev oluşturma

Aşağıdaki örneklerde, Java gRPC kitaplığını kullanarak hem teslim alma hem de teslimat görevi oluşturma veya BatchCreateTask adresine HTTP REST isteği gönderme işlemleri gösterilmektedir. Doğru JWT söz dizimi için JWT öğeleri bölümüne bakın.

gRPC

static final String PROJECT_ID = "my-delivery-co-gcp-project";

DeliveryServiceBlockingStub deliveryService =
  DeliveryServiceGrpc.newBlockingStub(channel);

// Delivery Task settings
Task deliveryTask = Task.newBuilder()
  .setType(Task.Type.DELIVERY)
  .setState(Task.State.OPEN)
  .setTrackingId("delivery-tracking-id")
  .setPlannedLocation(               // Grand Indonesia East Mall
    LocationInfo.newBuilder().setPoint(
      LatLng.newBuilder().setLatitude(-6.195139).setLongitude(106.820826)))
  .setTaskDuration(
    Duration.newBuilder().setSeconds(2 * 60))
  .build();

// Delivery Task request
CreateTaskRequest createDeliveryTaskRequest =
  CreateTaskRequest.newBuilder()  // No need for the header or parent fields
      .setTaskId("task-8312508")  // Task ID assigned by the Provider
      .setTask(deliveryTask)      // Initial state
      .build();

// Pickup Task settings
Task pickupTask = Task.newBuilder()
  .setType(Task.Type.PICKUP)
  .setState(Task.State.OPEN)
  .setTrackingId("pickup-tracking-id")
  .setPlannedLocation(               // Grand Indonesia East Mall
    LocationInfo.newBuilder().setPoint(
      LatLng.newBuilder().setLatitude(-6.195139).setLongitude(106.820826)))
  .setTaskDuration(
    Duration.newBuilder().setSeconds(2 * 60))
  .build();

// Pickup Task request
CreateTaskRequest createPickupTaskRequest =
  CreateTaskRequest.newBuilder()  // No need for the header or parent fields
      .setTaskId("task-8241890")  // Task ID assigned by the Provider
      .setTask(pickupTask)        // Initial state
      .build();

// Batch Create Tasks settings
String parent = "providers/" + PROJECT_ID;

// Batch Create Tasks request
BatchCreateTasksRequest batchCreateTasksRequest =
  BatchCreateTasksRequest.newBuilder()
      .setParent(parent)
      .addRequests(createDeliveryTaskRequest)
      .addRequests(createPickupTaskRequest)
      .build();

// Error handling
// If Fleet Engine does not have any task(s) with these task ID(s) and the
// credentials of the requestor pass, the service creates the task(s)
// successfully.

try {
  BatchCreateTasksResponse createdTasks = deliveryService.batchCreateTasks(
    batchCreateTasksRequest);
} catch (StatusRuntimeException e) {
  Status s = e.getStatus();
  switch (s.getCode()) {
    case ALREADY_EXISTS:
      break;
    case PERMISSION_DENIED:
      break;
  }
  return;
}

REST

Sunucu ortamından teslimat ve teslim alma görevi oluşturmak için BatchCreateTasks adresine bir HTTP REST çağrısı yapın:

POST https://fleetengine.googleapis.com/v1/providers/<project_id>/batchCreate

<id>, görev için benzersiz bir tanımlayıcıdır.

İstek başlığında, Bearer <token> değerine sahip bir Authorization alanı bulunmalıdır. Burada <token>, Hizmet hesabı rolleri ve JSON Web jetonları bölümünde açıklanan yönergelere göre sunucunuz tarafından verilir.

İstek gövdesi bir BatchCreateTasksRequest varlığı içermelidir.

Örnek curl komutu:

# Set $JWT, $PROJECT_ID, $DELIVERY_TRACKING_ID, $DELIVERY_TASK_ID,
# $PICKUP_TRACKING_ID, and $PICKUP_TASK_ID in the local environment
curl -X POST "https://fleetengine.googleapis.com/v1/providers/${PROJECT_ID}/tasks:batchCreate" \
 -H "Content-type: application/json" \
 -H "Authorization: Bearer ${JWT}" \
 --data-binary @- << EOM
{
 "requests" : [
   {
     "taskId": "${DELIVERY_TASK_ID}",
     "task" : {
       "type": "DELIVERY",
       "state": "OPEN",
       "trackingId": "${DELIVERY_TRACKING_ID}",
       "plannedLocation": {
         "point": {
             "latitude": -6.195139,
             "longitude": 106.820826
         }
       },
       "taskDuration": "90s"
     }
   },
   {
     "taskId": "${PICKUP_TASK_ID}",
     "task" : {
       "type": "PICKUP",
       "state": "OPEN",
       "trackingId": "${PICKUP_TRACKING_ID}",
       "plannedLocation": {
         "point": {
             "latitude": -6.195139,
             "longitude": 106.820826
         }
       },
       "taskDuration": "90s"
     }
   }
 ]
}
EOM

Sırada ne var?