สร้างงานเป็นกลุ่ม

เอกสารนี้แสดงวิธีสร้างงานเป็นชุดจากสภาพแวดล้อมเซิร์ฟเวอร์โดยใช้ gRPC หรือ REST ดูรายละเอียดเพิ่มเติมเกี่ยวกับการสร้างงานได้ที่

ฟิลด์งานสำหรับการสร้างงานเป็นชุด

เมื่อสร้างงานเป็นชุด องค์ประกอบ CreateTasksRequest แต่ละรายการใน requests ต้องผ่านกฎการตรวจสอบความถูกต้องเดียวกันกับคำขอ CreateTask สำหรับงานเดียว ยกเว้นฟิลด์ parent และ header ที่ไม่บังคับ หากตั้งค่าไว้ ฟิลด์ดังกล่าวต้องเหมือนกับฟิลด์ที่เกี่ยวข้องใน BatchCreateTasksRequest ระดับบนสุด

ดูข้อมูลเพิ่มเติมได้ในเอกสารประกอบข้อมูลอ้างอิง API สำหรับ BatchCreateTasks สำหรับ gRPC หรือ REST

ฟิลด์ชุดที่ต้องกรอก

ช่องค่า
requests Array<CreateTasksRequest>

ฟิลด์งานชุดที่ไม่บังคับ

ช่องค่า
header DeliveryRequestHeader

สร้างงานเป็นชุด

ตัวอย่างต่อไปนี้แสดงวิธีสร้างทั้งงานรับและงานจัดส่ง โดยใช้ไลบรารี Java gRPC หรือวิธีส่งคำขอ HTTP REST ไปยัง BatchCreateTask ดูไวยากรณ์ JWT ที่ถูกต้องได้ที่ องค์ประกอบ JWT

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

หากต้องการสร้างงานจัดส่งและงานรับจากสภาพแวดล้อมเซิร์ฟเวอร์ ให้ส่งการเรียก HTTP REST ไปยัง BatchCreateTasks

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

<id> คือตัวระบุที่ไม่ซ้ำกันสำหรับงาน

ส่วนหัวของคำขอต้องมีฟิลด์ Authorization ที่มีค่า Bearer <token> โดย <token> ออกโดยเซิร์ฟเวอร์ของคุณ ตามหลักเกณฑ์ที่อธิบายไว้ใน บทบาทบัญชีบริการ และ JSON Web Token

เนื้อความของคำขอต้องมีเอนทิตี BatchCreateTasksRequest

ตัวอย่างคำสั่ง curl

# 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

ขั้นตอนถัดไป