คุณสามารถใช้บริการค้นหา Google APIs เพื่อสร้างเครื่องมือต่างๆ ที่จะใช้กับ Google APIs ได้
อย่างไรก็ตาม วัตถุประสงค์หลักของเอกสาร Discovery คือการอนุญาตให้ Google สร้างไลบรารีของไคลเอ็นต์
ในภาษาโปรแกรมต่างๆ เอกสารนี้อธิบายวิธีสร้างไลบรารีของไคลเอ็นต์ที่กำหนดเองสำหรับ Google APIs
ไลบรารีของไคลเอ็นต์ที่เสถียรและมีฟีเจอร์ครบถ้วนเป็นเครื่องมือที่ซับซ้อนซึ่งอาจใช้เวลาหลายเดือนในการ
พัฒนา อย่างไรก็ตาม วิธีการทั่วไปในการสร้างไลบรารีของไคลเอ็นต์อย่างง่ายสำหรับ Google
APIs สามารถแบ่งออกเป็น 3 ขั้นตอนง่ายๆ ดังนี้
การดึงข้อมูลเอกสารการค้นพบและการสร้างพื้นผิว API
การเขียนคำขอ
การโทรออกและดึงข้อมูลการตอบกลับ
เราจะอธิบายขั้นตอนเหล่านี้โดยละเอียดในส่วนต่อไปนี้ นอกจากนี้ คุณยังดูตัวอย่างไคลเอ็นต์ API อย่างง่ายในส่วนตัวอย่างเพื่อดูวิธี
จับคู่วิธีการเหล่านี้กับโค้ดได้ด้วย
คว้าพร็อพเพอร์ตี้ path ขยายเป็นเทมเพลต URI แล้วรวมผลลัพธ์
ของการขยายนั้นกับ URI จากขั้นตอนก่อนหน้า ตัวอย่างเช่น ในเมธอด serviceusage.services.enable ของ Service
Usage API เวอร์ชัน 1 ค่าของพร็อพเพอร์ตี้
path คือ v1/{+name}:enable ดังนั้น URI แบบเต็มสำหรับเมธอด
คือ
คุณไม่จำเป็นต้องมีคีย์ API
เพื่อเรียกใช้ Service Usage API อย่างไรก็ตาม หาก API ที่คุณเรียกใช้ต้องใช้คีย์ API คุณสามารถ
เพิ่มคีย์ API ลงในสตริงการค้นหาของ URI ได้
REQUEST_URI?key=API_KEY
โทรออกและจัดการการตอบกลับ
หลังจากส่งคำขอแล้ว คุณต้องยกเลิกการซีเรียลไลซ์การตอบกลับเป็นรูปแบบภาษาที่เหมาะสม
โดยระมัดระวังในการจัดการเงื่อนไขข้อผิดพลาดที่อาจเกิดขึ้น ทั้งใน
การรับส่ง HTTP พื้นฐานและข้อความแสดงข้อผิดพลาดที่สร้างจากบริการ API รูปแบบของข้อผิดพลาด
มีการบันทึกไว้เป็นส่วนหนึ่งของคู่มือรูปแบบ JSON ของ Google
ตัวอย่าง
ส่วนต่อไปนี้แสดงตัวอย่างไลบรารีของไคลเอ็นต์ API อย่างง่าย
ไคลเอ็นต์ Simple APIs
ด้านล่างนี้คือตัวอย่างไลบรารีของไคลเอ็นต์ที่เขียนด้วย Python3 อย่างง่าย ไคลเอ็นต์สร้างอินเทอร์เฟซสำหรับโต้ตอบกับ Service Usage API จากนั้น
ใช้อินเทอร์เฟซดังกล่าวเพื่อเปิดใช้ Compute Engine API (compute.googleapis.com) ใน
โปรเจ็กต์ my-project
importhttplib2importjsonimporturitemplateimporturllib# Step 1: Fetch Discovery documentDISCOVERY_URI="https://serviceusage.googleapis.com/$discovery/rest?version=v1"h=httplib2.Http()resp,content=h.request(DISCOVERY_URI)discovery=json.loads(content)location=None# Set this to your location if appropriateuse_global_endpoint=True# Set this to False if you want to target the endpoint for your location# Step 2.a: Construct base URIBASE_URL=Noneifnotuse_global_endpointandlocation:ifdiscovery['endpoints']:BASE_URL=next((item['endpointUrl']foritemindiscovery['endpoints']ifitem['location']==location),None)ifnotBASE_URL:BASE_URL=discovery['rootUrl']BASE_URL+=discovery['servicePath']classCollection(object):passdefcreateNewMethod(name,method):# Step 2.b Compose requestdefnewMethod(**kwargs):body=kwargs.pop('body',None)url=urllib.parse.urljoin(BASE_URL,uritemplate.expand(method['path'],kwargs))forpname,pconfiginmethod.get('parameters',{}).items():ifpconfig['location']=='path'andpnameinkwargs:delkwargs[pname]ifkwargs:url=url+'?'+urllib.parse.urlencode(kwargs)returnh.request(url,method=method['httpMethod'],body=body,headers={'content-type':'application/json'})returnnewMethod# Step 3.a: Build client surfacedefbuild(discovery,collection):forname,resourceindiscovery.get('resources',{}).items():setattr(collection,name,build(resource,Collection()))forname,methodindiscovery.get('methods',{}).items():setattr(collection,name,createNewMethod(name,method))returncollection# Step 3.b: Use the clientservice=build(discovery,Collection())print(serviceusage.services.enable(name='projects/my-project/services/compute.googleapis.com'))
องค์ประกอบสำคัญของไคลเอ็นต์มีดังนี้
ขั้นตอนที่ 1: ดึงข้อมูลเอกสารการค้นพบ ระบบจะดึงข้อมูลและแยกวิเคราะห์เอกสารการค้นพบสำหรับ Service Usage API เป็นโครงสร้างข้อมูล เนื่องจาก Python เป็นภาษาที่พิมพ์แบบไดนามิก คุณจึงดึงข้อมูลเอกสาร Discovery ได้ที่รันไทม์
ขั้นตอนที่ 3.b: ใช้ไคลเอ็นต์ ซึ่งแสดงให้เห็นว่ามีการใช้พื้นผิว API ที่สร้างขึ้นอย่างไร
ก่อนอื่นจะสร้างออบเจ็กต์บริการจากเอกสาร Discovery จากนั้นจะใช้ Service Usage
API เพื่อเปิดใช้ Compute Engine API ในโปรเจ็กต์ my-project
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-07-26 UTC"],[[["\u003cp\u003eThe Google APIs Discovery Service enables the creation of client libraries in various programming languages and tools for interacting with Google APIs.\u003c/p\u003e\n"],["\u003cp\u003eBuilding a client library involves fetching the Discovery document, composing requests (including body and URL construction), making API calls, and handling responses.\u003c/p\u003e\n"],["\u003cp\u003eRequest URLs are constructed using URI Templates from the Discovery document, incorporating parameters and API keys when necessary.\u003c/p\u003e\n"],["\u003cp\u003eThe provided Python example demonstrates a simplified client library implementation, showcasing key steps like fetching the Discovery document, constructing the base URI, composing requests, and building the client surface.\u003c/p\u003e\n"]]],[],null,["# Build a client library\n\nYou can use the Google APIs Discovery Service for building a variety of different tools to use with Google APIs.\nHowever, the primary purpose of the Discovery document is to allow Google to create client\nlibraries in various programming languages. This document describes how you could go about\nbuilding a custom client library for Google APIs.\n\n\nA stable and feature-complete client library is a complicated tool that can take months to\ndevelop. However, the general instructions for building a simple client library for Google\nAPIs can be broken down to three simple steps:\n\n1. Fetching the Discovery document and constructing API surface\n2. Composing a request\n3. Making a call and fetching the response\n\n\nThese steps are described in more detail in the following sections. You can also have a look\nat the [Simple APIs client](#simple_apis_client) sample in the Examples section to see how\nthese instructions map to the code.\n\nFetch the Discovery document\n----------------------------\n\n\nBefore you begin implementing a client library, there are some basic requirements that impact\nhow you will proceed down your development path. For example, your programming language of\nchoice may be either typed or untyped; if it is typed it could be either statically or\ndynamically typed. It may be compiled or interpreted. These requirements will guide your\napproach to consuming and using the Discovery document.\n\n\nThe first development task is to fetch the Discovery document. Your strategy for exactly when\nthe document is to be fetched is determined by the requirements you identified. For example,\nin a statically-typed language, you might fetch the Discovery document early in the process\nand then generate code to handle the specific API described by the Discovery document. For a\nstrongly-typed language, you might generate some code and build a compiled library. For a\ndynamically typed language, you can lazily construct the programming structures to interface\nto the API on the fly as the programming surface is used.\n\nCompose a request\n-----------------\n\nComposing a requests involves two separate steps:\n\n1. Composing the request body.\n2. Constructing the request URL.\n\n\nYou need to convert the request body, if any, from a language-appropriate\nrepresentation into the correct wire format. For example, in a Java client\nlibrary, there may be a class for each request type that allows type-safe\nmanipulation of the request data and is serializable into JSON.\n\n\nThe construction of the request URL is a slightly more complicated process.\n\n\nThe `path` property of each method in the API uses [URI Template v04](https://code.google.com/p/uri-templates/) syntax. This property may\ncontain variables, which are surrounded by curly braces. Here is an example of a\n`path` property with variables: \n\n```\n/example/path/var\n```\n\n\nIn the path above, `var` is a variable. The value for this variable comes from the\nDiscovery document's `parameters` section for that method. Each variable name has\na corresponding value in the `parameters` object. In the example above, there is a\nparameter named `var` in the `parameters` section (and its\n`location` property is `path`, to indicate that it is a path\nvariable).\n\n\nWhen making a request, you should substitute the value for `var` into the URL.\nFor example, if the user of the library makes a choice that sets `var` to the\nvalue `foo`, the new URL will be `/example/path/foo`.\n\n\nAlso note that the `path` property is a relative URI. In order to calculate the\nabsolute URI, follow these steps:\n\n1. If you know your location (region), and the Discovery document has the `endpoints` property, check if your location is present in the `endpoints` list. If so, grab the `endpointUrl` from the `endpoints` list whose `location` matches yours.\n2.\n If there is no `endpoints` property in the Discovery document or your location\n is not present in the `endpoints` list or you want to target the global\n endpoint, grab the `rootUrl` property from the top level of the\n Discovery document.\n\n\n For example, the `rootUrl` property in the\n [Discovery document](https://serviceusage.googleapis.com/$discovery/rest?version=v1) for\n [the Service Usage API](https://cloud.google.com/service-usage/docs/reference/rest) is: \n\n ```\n https://serviceusage.googleapis.com/\n ```\n3. Grab the `servicePath` from the top level of the Discovery document. For example, the `servicePath` property in the Discovery document for the Service Usage API is empty.\n4. Concatenate them together to get:\n\n ```\n https://serviceusage.googleapis.com/\n ```\n5.\n Grab the `path` property, expand it as a URI Template, and combine the results\n of that expansion with the URI from the previous step. For example, in the v1 Service\n Usage API's `serviceusage.services.enable` method, the value of the\n `path` property is `v1/{+name}:enable`. So, the full URI for the\n method is:\n\n ```\n https://serviceusage.googleapis.com/v1/{+name}:enable\n ```\n\n\nYou don't need an [API key](https://cloud.google.com/docs/authentication/api-keys)\nto call the Service Usage API. However, if the API you're calling requires an API key, you can\nadd the API key to the URI's query string: \n\n```\nREQUEST_URI?key=API_KEY\n```\n\nMake a call and handle the response\n-----------------------------------\n\n\nAfter you send the request, the you need to deserialize the response into the appropriate\nlanguage representation, taking care to handle error conditions that could occur---both in the\nunderlying HTTP transport and error messages generated from the API service. The format of the\nerrors is documented as part of the [Google JSON Style\nGuide](https://google.github.io/styleguide/jsoncstyleguide.xml#error).\n\nExamples\n--------\n\n\nThe following section gives a simple example of an APIs client library.\n\n### Simple APIs client\n\n\nBelow is an example of a very simple client library written in Python3. The client builds an\ninterface for interacting with the [Service Usage API](https://cloud.google.com/service-usage/docs/reference/rest), then\nuses that interface to enable the Compute Engine API (`compute.googleapis.com`) in\nthe project `my-project`.\n**Warning:** The code below is a significantly simplified version of a typical client library. It is an incomplete implementation that is provided to demonstrate some aspects of building a client library. It is not production-ready code. \n\n```python\nimport httplib2\nimport json\nimport uritemplate\nimport urllib\n\n# Step 1: Fetch Discovery document\nDISCOVERY_URI = \"https://serviceusage.googleapis.com/$discovery/rest?version=v1\"\nh = httplib2.Http()\nresp, content = h.request(DISCOVERY_URI)\ndiscovery = json.loads(content)\nlocation = None # Set this to your location if appropriate\nuse_global_endpoint = True # Set this to False if you want to target the endpoint for your location\n\n# Step 2.a: Construct base URI\nBASE_URL = None\nif not use_global_endpoint and location:\n if discovery['endpoints']:\n BASE_URL = next((item['endpointUrl'] for item in discovery['endpoints'] if item['location'] == location), None)\nif not BASE_URL:\n BASE_URL = discovery['rootUrl']\nBASE_URL += discovery['servicePath']\n\nclass Collection(object): pass\n\ndef createNewMethod(name, method):\n # Step 2.b Compose request\n def newMethod(**kwargs):\n body = kwargs.pop('body', None)\n url = urllib.parse.urljoin(BASE_URL, uritemplate.expand(method['path'], kwargs))\n for pname, pconfig in method.get('parameters', {}).items():\n if pconfig['location'] == 'path' and pname in kwargs:\n del kwargs[pname]\n if kwargs:\n url = url + '?' + urllib.parse.urlencode(kwargs)\n return h.request(url, method=method['httpMethod'], body=body,\n headers={'content-type': 'application/json'})\n\n return newMethod\n\n# Step 3.a: Build client surface\ndef build(discovery, collection):\n for name, resource in discovery.get('resources', {}).items():\n setattr(collection, name, build(resource, Collection()))\n for name, method in discovery.get('methods', {}).items():\n setattr(collection, name, createNewMethod(name, method))\n return collection\n\n# Step 3.b: Use the client\nservice = build(discovery, Collection())\nprint (serviceusage.services.enable(name='projects/my-project/services/compute.googleapis.com'))\n```\n\nThe critical components of the client are:\n\n- **Step 1: Fetch the Discovery document**. The Discovery document for the Service Usage API is retrieved and parsed into a data structure. Since Python is a dynamically typed language, the Discovery document can be fetched at runtime.\n- **Step 2.a: Construct the base URI**. The base URI is calculated.\n- **Step 2.b: Compose the request** . When a method is called on a collection the URI Template is expanded with the parameters passed into the method, and parameters with a location of `query` are put into the query parameters of the URL. Finally a request is sent to the composed URL using the HTTP method specified in the Discovery document.\n- **Step 3.a: Build the client surface** . The client surface is built by recursively descending over the parsed Discovery document. For each method in the `methods` section a new method is attached to the `Collection` object. Because collections can be nested we look for `resources` and recursively build a `Collection` object for all of its members if one is found. Each nested collection is also attached as an attribute to the `Collection` object.\n- **Step 3.b: Use the client** . This demonstrates how the built API surface is used. First a service object is built from the Discovery document, then the Service Usage API is used to enable the Compute Engine API in the project `my-project`."]]