API는 외부 클라이언트에 액세스할 수 있는 원격 메소드를 제공하는 RPC(원격 절차 호출) 서비스입니다. 각 백엔드 API는 ProtoRPC remote.Service 클래스를 서브클래싱하는 RPC 서비스 클래스와 하나 이상의 메서드로 구성됩니다. 메서드를 정의할 때는 해당 메서드로 들어오는 요청과 해당 메서드에서 반환되는 응답에 사용되는 Message 클래스도 정의해야 합니다.
Message 클래스는 매핑 기능을 수행하므로 들어오는 데이터를 추출하여 서비스 메서드나 나가는 응답에 적절하게 제공할 수 있습니다.
요청에 경로 또는 쿼리 문자열 인수가 있으면 단순 Message 클래스 대신 ResourceContainer 클래스를 매핑에 사용합니다.
마지막으로, API 서비스 클래스와 클래스 메서드를 데코레이션하고 요청 및 응답에 사용되는 Message 클래스를 정의해야 합니다.
API 만들기
다음 절차에서는 코드를 데코레이션하여 단일 클래스로 구현된 API를 생성하는 방법을 보여줍니다. 다중 클래스가 있는 경우 여러 클래스로 구현된 API 만들기를 참조하세요.
사용 가능한 모든 데코레이터에 대한 자세한 내용은 데코레이터를 참조하세요.
API 이름과 서비스 클래스 이름이 동일할 필요는 없습니다. 버전 번호는 API 버전에 적용됩니다. 입력하는 값이 API로 연결되는 URL 경로의 일부가 됩니다. 버전에 대한 자세한 내용은 API 버전 관리를 참조하세요.
메서드가 요청에서 받아야 하는 데이터와 반환할 데이터를 결정하고 요청 본문과 응답 본문에 사용되는 Message 클래스를 만듭니다.
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))
GET 요청에서처럼 요청 본문에 인수가 나타나지 않는 경우에는 요청에 사용되는 Message 클래스를 생략하고 단순히 message_types.VoidMessage 값을 사용해도 됩니다.
Message 클래스 작성 및 사용에 대한 자세한 내용은 Google 프로토콜 RPC 응답 및 요청 Message 클래스에 관한 문서를 참조하세요.
API에 대한 메서드를 만들고 @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):
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["이해하기 어려움","hardToUnderstand","thumb-down"],["잘못된 정보 또는 샘플 코드","incorrectInformationOrSampleCode","thumb-down"],["필요한 정보/샘플이 없음","missingTheInformationSamplesINeed","thumb-down"],["번역 문제","translationIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-03-25(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)"]]