產生用戶端程式庫

提供機器可讀取的端對端資料動態饋給和執行要求 API 定義版本,可用於產生用戶端原始碼及驗證 JSON 資料結構。如此一來,您就能投入更多時間開發整合所需的應用程式功能和商業邏輯。

在此範例中,我們使用 Quicktype CLI 產生簡單易用的用戶端程式庫。

下載 JSON 結構定義

必須使用這些機器可讀取的資料動態饋給和 API,才能產生及驗證程式碼。

產生存取碼

變更 API 時,Quicktype 可用於重新產生程式碼,您只需更新受影響的應用程式程式碼即可。QuickType 支援 C++、Java、JavaScript、Python 和其他程式設計語言。

您也可以使用其他支援 JSON 結構定義定義的可用程式碼產生器,產生用戶端程式庫。

使用 Node Package Manager (npm) 在專案目錄中安裝 Quicktype,以便進行訂購端對端整合作業。

npm install quicktype

TypeScript

  1. 產生訂購端對端資料動態饋給的用戶端原始碼。
    quicktype --lang typescript --src-lang schema inventory-v2-json-schema.json#top_level_definitions/ -o ./owg-inventory.ts
            
  2. 為執行要求 API 產生用戶端原始碼。
    quicktype --lang typescript --src-lang schema fulfillment-actions-json-schema.json#top_level_definitions/ -o ./owg-fulfillment.ts
            
  3. 產生即時更新 API 的用戶端原始碼。
    quicktype --lang typescript --src-lang schema realtime-updates-json-schema.json#top_level_definitions/ -o ./owg-realtime-updates.ts
            
  4. 將產生的檔案複製到工作區,然後實作商業邏輯。

使用與驗證

建立實體並轉換為 JSON 的範例:

import { Convert, Fee, OperationHours, Restaurant, Service, ServiceArea,
    ServiceHours, Menu, MenuSection, Availability, MenuItem, MenuItemOption,
    MenuItemOffer, FeeType, FeeTypeEnum, RestaurantType } from './owg-inventory';

const restaurant: Restaurant = {
    "@id": "McDonalds",
    "@type": RestaurantType.Restaurant,
    "addressCountry": "US",
    "addressLocality": "123 Local",
    "addressRegion": "Region",
    "name": "MacDonald's",
    "postalCode": "1234",
    "streetAddress": "123",
    "telephone": "+15552999983",
    "url": "https://example.com",
    "dateModified": new Date()
}

const fee: Fee = {
    "@id": "123",
    "@type": FeeTypeEnum.Fee,
    "priceCurrency": "US",
    "serviceId": "123",
    "feeType": FeeType.Delivery,
    "dateModified": new Date()
}
const restaurantJson: string = Convert.restaurantToJson(restaurant);
const feeJson: string = Convert.feeToJson(fee);
    

Java

  1. 產生訂購端對端資料動態饋給的用戶端原始碼。
    quicktype --lang java --src-lang schema inventory-v2-json-schema.json#top_level_definitions/ -o ./java/ --no-date-times --package com.example.inventory
            
  2. 為執行要求 API 產生用戶端原始碼。
    quicktype --lang java --src-lang schema fulfillment-actions-json-schema.json#top_level_definitions/ -o ./java/ --no-date-times --package com.example.fulfillment
            
  3. 產生即時更新 API 的用戶端原始碼。
    quicktype --lang java --src-lang schema realtime-updates-json-schema.json#top_level_definitions/ -o ./java/ --no-date-times --package com.example.realtime
            
  4. 將產生的檔案複製到工作區,然後實作商業邏輯。

使用與驗證

建立實體並轉換為 JSON 的範例:

package com.example;
import com.example.inventory.Converter;
import com.example.inventory.Fee;
import com.example.inventory.FeeType;
import com.example.inventory.Restaurant;
import com.example.inventory.RestaurantType;
public class FoodOrderingResponse {
    public static void main(String[] args) {
        Restaurant restaurant = new Restaurant();
        restaurant.setId("MacDonalds");
        restaurant.setType(RestaurantType.RESTAURANT);
        restaurant.setAddressCountry("US");
        restaurant.setAddressLocality("123 Local");
        restaurant.setAddressRegion("Region");
        restaurant.setName("MacDonald's");
        restaurant.setPostalCode("1234");
        restaurant.setStreetAddress("123");
        restaurant.setTelephone("+15552999983");
        restaurant.setUrl("https://example.com");
        restaurant.setDateModified("2022-09-19T13:10:00.000Z");

        Fee fee = new Fee();
        fee.setId("123");
        fee.setType(FeeTypeEnum.FEE);
        fee.setPriceCurrency("US");
        fee.setServiceId("123");
        fee.setFeeType(FeeType.DELIVERY);
        fee.setDateModified("2022-09-19T13:13:10.000Z");

        String restaurantJson = Converter.RestaurantToJsonString(restaurant);
        String feeJson = Converter.FeeToJsonString(fee);
    }
}
    

JavaScript

  1. 產生訂購端對端資料動態饋給的用戶端原始碼。
    quicktype --lang javascript --src-lang schema inventory-v2-json-schema.json#top_level_definitions/ -o owg-inventory.js
            
  2. 為執行要求 API 產生用戶端原始碼。
    quicktype --lang javascript --src-lang schema fulfillment-actions-json-schema.json#top_level_definitions/ -o owg-fulfillment.js
            
  3. 產生即時更新 API 的用戶端原始碼。
    quicktype --lang javascript --src-lang schema realtime-updates-json-schema.json#top_level_definitions/ -o owg-realtime-updates.js
            
  4. 將產生的檔案複製到工作區,然後實作商業邏輯。

使用與驗證

建立實體並轉換為 JSON 的範例:

// Converts JSON strings to/from your types
// and asserts the results of JSON.parse at runtime
const Convert = require("./owg-inventory");

const restaurantJson = Convert.restaurantToJson({
    "@id": "McDonalds",
    "@type": 'Restaurant',
    "addressCountry": "US",
    "addressLocality": "123 Local",
    "addressRegion": "Region",
    "name": "MacDonald's",
    "postalCode": "1234",
    "streetAddress": "123",
    "telephone": "+15552999983",
    "url": "https://example.com",
    "dateModified": new Date()
}));

const restaurant = Convert.toRestaurant(restaurantJson);

    

Python

  1. 產生訂購端對端資料動態饋給的用戶端原始碼。
    quicktype --lang python --src-lang schema inventory-v2-json-schema.json#top_level_definitions/ -o owg_inventory.py
            
  2. 為執行要求 API 產生用戶端原始碼。
    quicktype --lang python --src-lang schema fulfillment-actions-json-schema.json#top_level_definitions/ -o owg_fulfillment.py
            
  3. 產生即時更新 API 的用戶端原始碼。
    quicktype --lang python --src-lang schema realtime-updates-json-schema.json#top_level_definitions/ -o owg_realtime_updates.py
            
  4. 將產生的檔案複製到工作區,然後實作商業邏輯。

使用方法

建立實體並轉換為 JSON 的範例:

import json
import owg_inventory

restaurant: owg_inventory.Restaurant = owg_inventory.restaurant_from_dict({
    "@id": "McDonalds",
    "@type": "Restaurant",
    "addressCountry": "US",
    "addressLocality": "123 Local",
    "addressRegion": "Region",
    "name": "MacDonald's",
    "postalCode": "1234",
    "streetAddress": "123",
    "telephone": "+15552999983",
    "url": "https://example.com",
    "dateModified": "2022-09-19T13:10:00.000Z"
})
restaurant_json: str = json.dumps(owg_inventory.restaurant_to_dict(restaurant))