این سند نحوه ایجاد دستهای از وظایف از یک محیط سرور با استفاده از gRPC یا REST را نشان میدهد. برای جزئیات بیشتر در مورد ایجاد وظایف، به موارد زیر مراجعه کنید:
فیلدهای وظیفه برای ایجاد وظایف به صورت دستهای
هنگام ایجاد وظایف به صورت دستهای، هر عنصر CreateTasksRequest در requests باید همان قوانین اعتبارسنجی درخواست CreateTask برای یک وظیفه واحد را تصویب کند، با این تفاوت که فیلدهای parent و header اختیاری هستند. در صورت تنظیم، باید با فیلدهای مربوطه خود در سطح بالای BatchCreateTasksRequest یکسان باشند.
برای اطلاعات بیشتر، به مستندات مرجع API برای BatchCreateTasks برای gRPC یا REST مراجعه کنید.
فیلدهای دستهای مورد نیاز
| میدان | ارزش |
|---|---|
| درخواستها | Array<CreateTasksRequest> |
فیلدهای وظیفه دستهای اختیاری
| میدان | ارزش |
|---|---|
| سربرگ | DeliveryRequestHeader |
ایجاد دستهای از وظایف
The following examples shows how to create both a pickup and a delivery task using the Java gRPC library or how to make an HTTP REST request to BatchCreateTask . See JWT elements for the correct JWT syntax.
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;
}
استراحت
برای ایجاد یک وظیفه تحویل و برداشت از محیط سرور، یک فراخوانی HTTP REST به BatchCreateTasks انجام دهید:
POST https://fleetengine.googleapis.com/v1/providers/<project_id>/batchCreate<id> یک شناسه منحصر به فرد برای وظیفه است.
The request header must contain a field Authorization with the value Bearer <token> , where <token> is issued by your server according to the guidelines described in Service account roles and JSON Web tokens .
بدنه درخواست باید حاوی یک موجودیت 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