Membuat API menggunakan Framework Cloud Endpoints untuk Python
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
API adalah layanan remote procedure call (RPC) yang menyediakan metode jarak jauh yang dapat diakses oleh klien eksternal. Setiap backend API terdiri dari class layanan RPC yang merupakan subclass dari class ProtoRPC remote.Service, dan satu atau beberapa metode. Saat menentukan metode, Anda juga harus menentukan
class Message
untuk permintaan yang masuk ke metode tersebut dan respons yang dikembalikan olehnya.
Class Message melakukan fungsi pemetaan sehingga data yang masuk dapat diekstrak dan diberikan ke metode layanan dengan benar, atau diberikan dengan benar ke respons keluar.
Jika permintaan memiliki argumen jalur atau string kueri, Anda menggunakan
class ResourceContainer
untuk pemetaan, bukan class Message sederhana.
Terakhir, Anda perlu menghias class layanan API dan metode class, serta Anda
perlu menentukan class Message untuk permintaan dan respons.
Membuat API
Prosedur berikut menunjukkan cara menghias kode untuk membuat API yang diterapkan dalam satu class. Jika Anda memiliki API multi-class, lihat
Membuat API yang diimplementasikan dengan beberapa class.
Lihat
Decorator
untuk mengetahui informasi mendetail tentang semua decorator yang tersedia.
Perhatikan bahwa nama API dan nama class layanan Anda tidak harus sama. Nomor versi berlaku untuk versi API. Nilai yang Anda masukkan akan menjadi bagian dari jalur di URL ke API Anda. Untuk mengetahui informasi selengkapnya tentang versi, lihat Menangani pembuatan versi API.
Tentukan data apa yang diharapkan metode Anda dari permintaan dan data apa yang
dikembalikan, lalu buat
class Message
untuk isi permintaan dan isi respons:
classEchoRequest(messages.Message):message=messages.StringField(1)classEchoResponse(messages.Message):"""A proto Message that contains a simple string field."""message=messages.StringField(1)ECHO_RESOURCE=endpoints.ResourceContainer(EchoRequest,n=messages.IntegerField(2,default=1))
Perhatikan bahwa jika tidak ada argumen yang muncul di isi permintaan, seperti dalam permintaan GET, Anda dapat menghapus class Message untuk permintaan dan cukup menggunakan nilai message_types.VoidMessage.
Jika permintaan Anda memiliki argumen string kueri atau jalur, ganti
class Message Anda dengan
ResourceContainer yang sesuai.
Untuk mengetahui informasi lengkap tentang pembentukan dan penggunaan class Message, lihat dokumentasi untuk class Message permintaan dan respons Google Protocol RPC.
Buat metode untuk API Anda, dan hiasi dengan
@endpoints.method:
@endpoints.method(# This method takes a ResourceContainer defined above.ECHO_RESOURCE,# This method returns an Echo message.EchoResponse,path="echo",http_method="POST",name="echo",)defecho(self,request):
Jika permintaan Anda memiliki data jalur atau string kueri, ganti jenis pesan permintaan dengan
ResourceContainer yang sesuai.
Tambahkan kode untuk menayangkan API Anda, seperti yang dijelaskan dalam
Membuat server web.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-09-04 UTC."],[[["\u003cp\u003eAn API is an RPC service with remote methods accessible to external clients, requiring a service class that subclasses \u003ccode\u003eremote.Service\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eEach API method needs \u003ccode\u003eMessage\u003c/code\u003e classes defined for both incoming requests and outgoing responses to properly map data, though these \u003ccode\u003eMessage\u003c/code\u003e classes are not part of the exposed API.\u003c/p\u003e\n"],["\u003cp\u003eIf requests have path or query string arguments, a \u003ccode\u003eResourceContainer\u003c/code\u003e class should be used instead of a simple \u003ccode\u003eMessage\u003c/code\u003e class for data mapping.\u003c/p\u003e\n"],["\u003cp\u003eTo create an API, you must decorate a subclass of \u003ccode\u003eremote.Service\u003c/code\u003e with \u003ccode\u003e@endpoints.api\u003c/code\u003e, define \u003ccode\u003eMessage\u003c/code\u003e or \u003ccode\u003eResourceContainer\u003c/code\u003e classes for requests and responses, and decorate the method with \u003ccode\u003e@endpoints.method\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe API's name and version, specified in the \u003ccode\u003e@endpoints.api\u003c/code\u003e decorator, are incorporated into the API's URL path, allowing for version management.\u003c/p\u003e\n"]]],[],null,["# Creating the API using Cloud Endpoints Frameworks for Python\n\nAn API is a remote procedure call (RPC) service that provides remote\nmethods accessible to external clients. Each backend API consists of an RPC\nservice class that subclasses the\n[`ProtoRPC remote.Service`](/appengine/docs/python/tools/protorpc) class, and\none or more methods. When you define a method, you must also define\n[`Message` classes](/appengine/docs/python/tools/protorpc#Working_with_Messages)\nfor the requests coming into that method and the responses returned by it.\nA `Message` class performs a mapping function so the incoming data can be\nextracted and supplied to the service method properly, or supplied properly to\nthe outgoing response.\n| **Note:** The `Message` classes aren't part of the exposed API, so clients don't need to know about them.\n\nIf a request has path or query string arguments, you use a\n[`ResourceContainer`](/endpoints/docs/frameworks/python/decorators-reference#using_resourcecontainer_for_path_or_query_string_arguments)\nclass for the mapping, instead of a simple `Message` class.\n\nFinally, you need to decorate the API service class and class methods, and you\nneed to define `Message` classes for the requests and responses.\n\nCreating the API\n----------------\n\nThe following procedure shows how to decorate your code to create an API\nimplemented in a single class. If you have a multi-class API, see\n[Creating an API implemented with multiple classes](/endpoints/docs/frameworks/python/create-multi-class-api).\nSee\n[Decorators](/endpoints/docs/frameworks/python/decorators-reference)\nfor detailed information about all the available decorators.\n\nTo create an API:\n\n1. Add the following required imports:\n\n import endpoints\n from endpoints import message_types\n from endpoints import messages\n from endpoints import remote\n\n2. Define a subclass of `remote.Service` and decorate it with `@endpoints.api`:\n\n @endpoints.api(name=\"echo\", version=\"v1\")\n class EchoApi(remote.Service):\n\n Notice that your API name and the name of your service class don't need to\n be the same. The version number applies to the version of the API. The value\n that you enter becomes part of the path in the URL to your API. For more\n information on versions, see\n [Handling API versioning](/endpoints/docs/frameworks/python/handling-api-versioning).\n3. Determine what data your method expects from the request and what data is\n returned, and create a\n [`Message` class](/appengine/docs/python/tools/protorpc#Working_with_Messages)\n for the request body and response body:\n\n class EchoRequest(messages.Message):\n message = messages.StringField(1)\n\n\n class EchoResponse(messages.Message):\n \"\"\"A proto Message that contains a simple string field.\"\"\"\n\n message = messages.StringField(1)\n\n\n ECHO_RESOURCE = endpoints.ResourceContainer(\n EchoRequest, n=messages.IntegerField(2, default=1)\n )\n\n Note that if no arguments appear in the request body, such as in a `GET`\n request, you can omit the `Message` class for the request and simply use the\n value `message_types.VoidMessage`.\n\n If your request has path or query string arguments, replace\n your `Message` class with an appropriate\n [`ResourceContainer`](#using_resourcecontainer_for_path_or_query_string_arguments).\n\n For complete information on forming and using `Message` classes, see the\n documentation for the\n [Google Protocol RPC](/appengine/docs/python/tools/protorpc)\n response and request `Message` classes.\n4. Create the method for your API, and decorate it with\n `@endpoints.method`:\n\n @endpoints.method(\n # This method takes a ResourceContainer defined above.\n ECHO_RESOURCE,\n # This method returns an Echo message.\n EchoResponse,\n path=\"echo\",\n http_method=\"POST\",\n name=\"echo\",\n )\n def echo(self, request):\n\n If your request has path or query string data, replace the request message\n type with an appropriate\n [`ResourceContainer`](/endpoints/docs/frameworks/python/decorators-reference#using_resourcecontainer_for_path_or_query_string_arguments).\n5. Add the code to serve your API, as described in\n [Creating a web server](/endpoints/docs/frameworks/python/api_server).\n\nWhat's next\n-----------\n\n- [Creating an API implemented with multiple classes](/endpoints/docs/frameworks/python/create-multi-class-api)\n- [Decorators](/endpoints/docs/frameworks/python/decorators-reference)"]]