יצירה של ספריות לקוח

אפשר ליצור קוד מקור של לקוח ולאמת את המבנה של נתוני JSON באמצעות גרסה קריאה למחשבים של פיד הנתונים מקצה לקצה ושל הגדרות ה-API של מילוי ההזמנה. כך תוכלו להשקיע יותר זמן בפיתוח היכולות של האפליקציות והלוגיקה העסקית שנדרשות לשילוב.

בדוגמה הזו אנחנו משתמשים ב-CLI quicktype כדי ליצור ספריית לקוח קלה לשימוש.

הורדת סכימות JSON

הגרסאות האלה, שניתנות לקריאה למחשבים, של פידים של נתונים וממשקי API, נדרשות ליצירת קוד ולאימות שלו.

יצירת קודים

אפשר להשתמש ב-Quicktype כדי ליצור מחדש קודים כשממשקי API משתנים, כך שתוכלו פשוט לעדכן את קוד האפליקציה המושפע. QuickType תומך ב-C++ , ב-Java, ב-JavaScript, ב-Python ובשפות תכנות אחרות.

כדי ליצור את ספריות הלקוח, תוכלו גם להשתמש בכלים זמינים אחרים ליצירת קוד שתומכים בהגדרות של סכימת JSON.

משתמשים במנהל חבילת הצמתים (npm) כדי להתקין את הטיפוס המהיר בספריית הפרויקטים בשביל השילוב של Ordering מקצה לקצה.

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. מעתיקים את הקבצים שנוצרו לסביבת העבודה ומטמיעים את הלוגיקה העסקית.

Usage

דוגמה ליצירת ישויות והמרתן ל-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))