클라이언트 라이브러리 생성

머신이 읽을 수 있는 버전의 Ordering End-to-End 데이터 피드 및 fulfillment API 정의를 사용하여 클라이언트 소스 코드를 생성하고 JSON 데이터의 구조를 검증할 수 있습니다. 따라서 통합에 필요한 애플리케이션 기능과 비즈니스 로직을 개발하는 데 더 많은 시간을 할애할 수 있습니다.

이 예에서는 quicktype CLI를 사용하여 사용하기 쉬운 클라이언트 라이브러리를 생성합니다.

JSON 스키마 다운로드

코드 생성 및 유효성 검사에는 머신이 읽을 수 있는 데이터 피드 및 API 버전이 필요합니다.

코드 생성

API가 변경될 때 Quicktype을 사용하여 코드를 다시 생성할 수 있으므로 영향을 받는 애플리케이션 코드를 간단히 업데이트할 수 있습니다. QuickType은 C++, Java, JavaScript, Python, 기타 프로그래밍 언어를 지원합니다.

JSON 스키마 정의를 지원하는 다른 코드 생성기 도구를 사용하여 클라이언트 라이브러리를 생성할 수도 있습니다.

노드 패키지 관리자(npm)를 사용하여 순서 지정 엔드 투 엔드 통합을 위한 프로젝트 디렉터리에 빠른 유형을 설치합니다.

npm install quicktype

TypeScript

  1. 엔드 투 엔드 데이터 피드 주문을 위한 클라이언트 소스 코드를 생성합니다.
    quicktype --lang typescript --src-lang schema inventory-v2-json-schema.json#top_level_definitions/ -o ./owg-inventory.ts
            
  2. fulfillment 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. fulfillment 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. fulfillment 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. fulfillment 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))