{"id":3258,"date":"2024-04-29T19:25:34","date_gmt":"2024-04-29T10:25:34","guid":{"rendered":"https:\/\/nobunobu1717.site\/?p=3258"},"modified":"2024-04-29T19:25:34","modified_gmt":"2024-04-29T10:25:34","slug":"fastapi%e3%81%a7%e9%96%a2%e6%95%b0%e3%81%a7%e3%81%af%e3%81%aa%e3%81%84%e3%82%af%e3%83%a9%e3%82%b9%e3%82%aa%e3%83%96%e3%82%b8%e3%82%a7%e3%82%af%e3%83%88%e3%82%92depends%e3%81%a7%e4%be%9d%e5%ad%98","status":"publish","type":"post","link":"https:\/\/nobunobu1717.site\/?p=3258","title":{"rendered":"FastAPI\u3067\u95a2\u6570\u3067\u306f\u306a\u3044\u30af\u30e9\u30b9\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092Depends\u3067\u4f9d\u5b58\u6027\u6ce8\u5165\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"<h2>\u6982\u8981<\/h2>\n<p>\u6700\u8fd1FastAPI\u3092\u89e6\u308a\u59cb\u3081\u305f\u306e\u3067\u3059\u304c\u3001Depends\u3067\u95a2\u6570\u3067\u306f\u306a\u304f\u30af\u30e9\u30b9\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u6ce8\u5165\u3059\u308b\u305f\u3081\u306e\u500b\u4eba\u7684\u306a\u30e1\u30e2\u3067\u3059\u3002<\/p>\n<h2><span style=\"color: #1a1a1a;\"><span style=\"caret-color: #1a1a1a; font-size: 20px; background-color: #f3f3f3;\"><b>\u8aac\u660e<\/b><\/span><\/span><\/h2>\n<h3>Depends\u3068\u306f<\/h3>\n<p>\u7c21\u5358\u306b\u8a00\u3063\u3066\u3057\u307e\u3046\u3068\u3001FastAPI\u5c02\u7528\u306eD\/I(\u4f9d\u5b58\u6027\u6ce8\u5165)\u306e\u4ed5\u7d44\u307f\u3067\u3059\u3002<\/p>\n<pre class=\"lang:python decode:true\">from typing import Union\r\n\r\nfrom fastapi import Depends, FastAPI\r\n\r\napp = FastAPI()\r\n\r\n# \u5171\u901a\u3067\u547c\u3070\u308c\u308b\u95a2\u6570\r\nasync def common_parameters(\r\n    q: Union[str, None] = None, skip: int = 0, limit: int = 100\r\n):\r\n    return {\"q\": q, \"skip\": skip, \"limit\": limit}\r\n\r\n\r\n@app.get(\"\/items\/\")\r\nasync def read_items(commons: dict = Depends(common_parameters)):\r\n    # \u95a2\u6570\u5b9f\u884c\u524d\u306bcommon_parameters\u304c\u547c\u3070\u308c\u3066\u3001dict\u304c\u4f5c\u3089\u308c\u308b\r\n    return commons\r\n\r\n\r\n@app.get(\"\/users\/\")\r\nasync def read_users(commons: dict = Depends(common_parameters)):\r\n    return commons<\/pre>\n<p>\u5b9f\u969b\u306b\u5404HTTP\u30e1\u30bd\u30c3\u30c9\u304c\u547c\u3073\u51fa\u3055\u308c\u308b\u524d\u306b\u3001Depends\u306b\u6307\u5b9a\u3057\u305f\u95a2\u6570\u3092\u5b9f\u884c\u3057\u3001\u305d\u306e\u623b\u308a\u5024\u3092\u5f15\u6570\u3068\u3057\u3066\u6e21\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u3088\u304f\u6319\u3052\u3089\u308c\u308b\u4f7f\u7528\u7528\u9014\u3068\u3057\u3066\u306f\u3001\u4e0b\u8a18\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<ol>\n<li>UnitTest\u3067\u30b9\u30bf\u30d6\u3084\u30e2\u30c3\u30af\u306a\u3069\u306e\u51e6\u7406\u306b\u7f6e\u304d\u63db\u3048\u304c\u53ef\u80fd<\/li>\n<li>\u524d\u51e6\u7406\u306e\u5171\u901a\u5316 (\u30d8\u30c3\u30c0\u306e\u7279\u5b9a\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u30c1\u30a7\u30c3\u30af\u3001\u30e6\u30fc\u30b6\u30fc\u306e\u8a8d\u8a3c\u30c1\u30a7\u30c3\u30af\u306a\u3069\uff09<\/li>\n<li>OpenAPI\u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306e\u30b9\u30ad\u30fc\u30de\u306a\u3069\u306b\u7d71\u5408<\/li>\n<\/ol>\n<p>2\u306b\u95a2\u3057\u3066\u306f\u3001\u4ed6\u306eWeb\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3067\u3082\u30df\u30c9\u30eb\u30a6\u30a7\u30a2\u6a5f\u80fd\u3068\u3057\u3066\u63d0\u4f9b\u3055\u308c\u3066\u3044\u3066\u3001\u30ea\u30af\u30a8\u30b9\u30c8\u306e\u524d\u5f8c\u306b\u51e6\u7406\u3092\u631f\u3080\u305f\u3081\u306b\u4f7f\u308f\u308c\u308b\u3053\u3068\u304c\u591a\u3044\u304b\u3068\u601d\u3044\u307e\u3059\u3002 (\u305f\u3060\u3057FastAPI\u306b\u3082\u30df\u30c9\u30eb\u30a6\u30a7\u30a2\u6a5f\u80fd\u306f\u3042\u308a\u3002\uff09<\/p>\n<p>\u3088\u308a\u8a73\u7d30\u306a\u60c5\u5831\u306f\u516c\u5f0f\u30da\u30fc\u30b8\u306e<a href=\"https:\/\/fastapi.tiangolo.com\/ja\/tutorial\/dependencies\/\" target=\"_blank\" rel=\"noopener\">\u8aac\u660e<\/a>\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p>&nbsp;<\/p>\n<h3>\u500b\u4eba\u7684\u306b\u611f\u3058\u305f\u70b9<\/h3>\n<p>1\u3064\u306e\u30b3\u30fc\u30c9\u3067\u8aac\u660e\u3059\u308b\u3088\u3046\u306a\u7c21\u5358\u306a\u30b5\u30f3\u30d7\u30eb\u3067\u306f\u3001\u5148\u306b\u6319\u3052\u305f\u95a2\u6570\u3092\u76f4\u63a5\u6e21\u3057\u3066\u3057\u307e\u3048\u3070\u3044\u3044\u3067\u3059\u304c\u3001\u4e0b\u8a18\u306e\u70b9\u3067\u5c11\u3057\u4e0d\u4fbf(\u4f7f\u3044\u306b\u304f\u3044)\u3068\u611f\u3058\u307e\u3057\u305f\u3002<\/p>\n<ul>\n<li>\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u306e\u5b9a\u7fa9\u304c\u66f8\u304b\u308c\u305f\u30b3\u30fc\u30c9\u5185\u306b\u3001\u7279\u5b9a\u306e\u30ed\u30b8\u30c3\u30af\u3092\u6301\u3064\u95a2\u6570\u3092\u66f8\u304f\u306e\u306f\u4f55\u304b\u6c17\u6301\u3061\u60aa\u3044<\/li>\n<li>\u56fa\u5b9a\u51e6\u7406\u3058\u3083\u7121\u3044\u5834\u5408\u306b\u306f\u3001\u95a2\u6570\u306e\u30b9\u30b3\u30fc\u30d7\u5916\u306b\u8a2d\u5b9a\u306a\u3069\u3092\u6301\u305f\u305b\u306a\u3044\u3068\u3044\u3051\u306a\u3044<\/li>\n<\/ul>\n<p>\u305d\u3053\u3067\u95a2\u6570\u3067\u306f\u306a\u304f\u3001\u30af\u30e9\u30b9\u81ea\u4f53\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u6e21\u305b\u306a\u3044\u306e\u304b\u3068\u601d\u3044\u307e\u3057\u305f\u3002\u305d\u3046\u3059\u308b\u3053\u3068\u3067\u3001\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u3067\u8a2d\u5b9a\u3092\u6e21\u3059\u3053\u3068\u3067\u30af\u30e9\u30b9\u5185\u306b\u5404\u7a2e\u8a2d\u5b9a\u306a\u3069\u3082\u6301\u305f\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h3>\u30af\u30e9\u30b9\u306b\u3088\u308b\u4f9d\u5b58\u6027\u6ce8\u5165<\/h3>\n<p><a href=\"https:\/\/fastapi.tiangolo.com\/ja\/tutorial\/dependencies\/classes-as-dependencies\/\" target=\"_blank\" rel=\"noopener\">\u516c\u5f0f\u306e\u89e3\u8aac<\/a>\u306b\u3082\u66f8\u3044\u3066\u3042\u308a\u307e\u3059\u304c\u3001\u30af\u30e9\u30b9\u3092\u4f7f\u3063\u305f\u4f9d\u5b58\u6027\u306e\u6ce8\u5165\u3082\u53ef\u80fd\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">from typing import Union\r\n\r\nfrom fastapi import Depends, FastAPI\r\n\r\napp = FastAPI()\r\n\r\n\r\nfake_items_db = [{\"item_name\": \"Foo\"}, {\"item_name\": \"Bar\"}, {\"item_name\": \"Baz\"}]\r\n\r\n\r\nclass CommonQueryParams:\r\n    def __init__(self, q: Union[str, None] = None, skip: int = 0, limit: int = 100):\r\n        self.q = q\r\n        self.skip = skip\r\n        self.limit = limit\r\n\r\n\r\n@app.get(\"\/items\/\")\r\nasync def read_items(commons=Depends(CommonQueryParams)):\r\n    response = {}\r\n    if commons.q:\r\n        response.update({\"q\": commons.q})\r\n    items = fake_items_db[commons.skip : commons.skip + commons.limit]\r\n    response.update({\"items\": items})\r\n    return response<\/pre>\n<p>&nbsp;<\/p>\n<p>\u305f\u3060\u3001\u3053\u308c\u3060\u3068\u5b9f\u969b\u306b\u306f\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u304c\u547c\u3070\u308c\u308b\u3060\u3051\u306a\u306e\u3067\u3001\u521d\u671f\u5316\u51e6\u7406\u3067\u4f55\u304b\u8a2d\u5b9a\u3092\u6301\u305f\u305b\u308b\u3068\u306a\u308b\u3068\u6bce\u56de\u51e6\u7406\u304c\u5b9f\u884c\u3055\u308c\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n<p>\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u81ea\u4f53\u306f\u30e2\u30b8\u30e5\u30fc\u30eb\u5185\u306e\u521d\u671f\u5316\u51e6\u7406\u306a\u3069\u3067\u3001\u5225\u9014\u547c\u3073\u51fa\u3057\u3092\u884c\u3044\u3001\u8a2d\u5b9a\u306a\u3069\u3092\u4fdd\u6301\u3055\u305b\u3066\u3001\u305d\u308c\u3089\u306e\u8a2d\u5b9a\u3092\u4f7f\u3063\u3066\u5225\u9014\u51e6\u7406\u3092\u3057\u305f\u3044\u306e\u3067\u76ee\u7684\u306b\u5408\u3044\u307e\u305b\u3093\u3002<\/p>\n<p><span>\u3082\u3046\u5c11\u3057\u63a2\u3063\u3066\u307f\u305f\u3068\u3053\u308d\u3001\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u306e\u9805\u76ee\u3067\u4f7f\u7528\u3055\u308c\u3066\u3044\u308b\u3001OAuth2PasswordBearer\u304c\u307e\u3055\u306b\u3084\u308a\u305f\u3044\u3053\u3068\u3092\u5b9f\u73fe\u3057\u3066\u3044\u308b\u306e\u3067<a href=\"https:\/\/github.com\/tiangolo\/fastapi\/blob\/efc12c5cdbede6922a033a5234c70cb22c4d204a\/fastapi\/security\/oauth2.py#L140\" target=\"_blank\" rel=\"noopener\">\u5b9f\u969b\u306e\u30b3\u30fc\u30c9<\/a>\u3092\u8997\u3044\u3066\u307f\u305f\u3068\u3053\u308d\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306b\u5b9f\u884c\u3057\u3066\u3044\u307e\u3057\u305f\u3002<\/span><\/p>\n<pre class=\"lang:default decode:true\">class OAuth2PasswordBearer(OAuth2):\r\n    def __init__(\r\n        self,\r\n        tokenUrl: str,\r\n        scheme_name: Optional[str] = None,\r\n        scopes: Optional[Dict[str, str]] = None,\r\n        description: Optional[str] = None,\r\n        auto_error: bool = True,\r\n    ):\r\n        if not scopes:\r\n            scopes = {}\r\n        flows = OAuthFlowsModel(password={\"tokenUrl\": tokenUrl, \"scopes\": scopes})\r\n        super().__init__(\r\n            flows=flows,\r\n            scheme_name=scheme_name,\r\n            description=description,\r\n            auto_error=auto_error,\r\n        )\r\n\r\n    async def __call__(self, request: Request) -&gt; Optional[str]:\r\n        authorization = request.headers.get(\"Authorization\")\r\n        scheme, param = get_authorization_scheme_param(authorization)\r\n        if not authorization or scheme.lower() != \"bearer\":\r\n            if self.auto_error:\r\n                raise HTTPException(\r\n                    status_code=HTTP_401_UNAUTHORIZED,\r\n                    detail=\"Not authenticated\",\r\n                    headers={\"WWW-Authenticate\": \"Bearer\"},\r\n                )\r\n            else:\r\n                return None\r\n        return param<\/pre>\n<p>&nbsp;<\/p>\n<p><span>\u3068\u3044\u3046\u3053\u3068\u3067\u3001__call__\u95a2\u6570\u3092\u5b9a\u7fa9\u3057\u3066\u3001\u547c\u3076\u3053\u3068\u304c\u3067\u304d\u308b\u3088\u3046\u3067\u3059\u3002<\/span><\/p>\n<p>\u500b\u4eba\u7684\u306b\u306f\u306a\u3058\u307f\u306e\u7121\u3044\u95a2\u6570\u306a\u306e\u3067\u3059\u304c\u3001\u30af\u30e9\u30b9\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u30e1\u30bd\u30c3\u30c9\u3068\u3057\u3066\u547c\u3073\u51fa\u3057\u305f\u969b\u306b\u547c\u3070\u308c\u308b\u6a5f\u80fd\u306e\u3088\u3046\u3067\u3059\u306d\u3002<\/p>\n<pre class=\"lang:default decode:true\">class Sample:\r\n\u3000\u3000def __call__(self):\r\n\u3000\u3000\u3000\u3000print(\"Hello\")\r\n\r\ns = Sample()\r\ns() # __call__\u304c\u547c\u3070\u308c\u308b<\/pre>\n<p>&nbsp;<\/p>\n<p>\u4e0b\u8a18\u306e\u3088\u3046\u306b__call__\u95a2\u6570\u3092\u8a18\u8f09\u3057\u3066\u3001\u5225\u306e\u30b9\u30b3\u30fc\u30d7\u3067\u4f5c\u6210\u3057\u305f\u30af\u30e9\u30b9\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u81ea\u4f53\u3092Depends\u306b\u6e21\u3059\u3068\u3001\u3053\u306e__call__\u304c\u30ea\u30af\u30a8\u30b9\u30c8\u306e\u5ea6\u306b\u5b9f\u884c\u3055\u308c\u3066\u3084\u308a\u305f\u3044\u3053\u3068\u304c\u5b9f\u73fe\u3067\u304d\u307e\u307e\u3057\u305f\u3002<\/p>\n<p>\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u3067\u56fa\u5b9a\u306e\u8a2d\u5b9a\u3092\u6301\u305f\u305b\u305f\u4e0a\u3067\u3001\u7279\u5b9a\u306e\u51e6\u7406\u3092\u5b9f\u884c\u3055\u305b\u305f\u3044\u3068\u3044\u3046\u3053\u3068\u304c\u5b9f\u73fe\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n<pre class=\"lang:default decode:true \">from typing import Union\r\n\r\nfrom fastapi import Depends, FastAPI\r\n\r\napp = FastAPI()\r\n\r\nclass CommonParam:\r\n  p1: str\r\n\r\n  def __init__(self, p1: str):\r\n     self.p1 = p1\r\n\r\n  async def __call__(self) -&gt; str:\r\n     return p1\r\n\r\n# \u30af\u30e9\u30b9\u3092\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3068\u3057\u3066\u4fdd\u6301 (\"test1\"\u3092\u8a2d\u5b9a)\r\ncommon_param = CommonParam(\"test1\")\r\n\r\n\r\n@app.get(\"\/items\/\")\r\nasync def read_items(param1: str = Depends(common_param)):\r\n    return param1 # param1\u306b\u306f\"test1\"\u304c\u5165\u3063\u3066\u3044\u308b\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8981 \u6700\u8fd1FastAPI\u3092\u89e6\u308a\u59cb\u3081\u305f\u306e\u3067\u3059\u304c\u3001Depends\u3067\u95a2\u6570\u3067\u306f\u306a\u304f\u30af\u30e9\u30b9\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u6ce8\u5165\u3059\u308b\u305f\u3081\u306e\u500b\u4eba\u7684\u306a\u30e1\u30e2\u3067\u3059\u3002 \u8aac\u660e Depends\u3068\u306f \u7c21\u5358\u306b\u8a00\u3063\u3066\u3057\u307e\u3046\u3068\u3001FastAPI\u5c02\u7528\u306eD\/ &#8230; <\/p>\n","protected":false},"author":1,"featured_media":3263,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[55,34],"tags":[],"_links":{"self":[{"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/posts\/3258"}],"collection":[{"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3258"}],"version-history":[{"count":3,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/posts\/3258\/revisions"}],"predecessor-version":[{"id":3270,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/posts\/3258\/revisions\/3270"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/media\/3263"}],"wp:attachment":[{"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}