{"id":135416,"date":"2024-09-06T12:30:51","date_gmt":"2024-09-06T07:00:51","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?page_id=135416"},"modified":"2024-09-06T12:30:51","modified_gmt":"2024-09-06T07:00:51","slug":"dependency-injection-fetching-the-user","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/dependency-injection-fetching-the-user\/","title":{"rendered":"Dependency Injection: Fetching the User"},"content":{"rendered":"\n<p>Dependency injection is a powerful technique in software development that promotes loose coupling and testability. In FastAPI, it&#8217;s commonly used to inject dependencies like database sessions, configuration settings, and services into your endpoints. In this section, we&#8217;ll demonstrate how to use dependency injection to fetch the current user in your FastAPI routes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Creating a Dependency Function<\/strong><\/h2>\n\n\n\n<p><strong>Import Necessary Modules:<\/strong><\/p>\n\n\n\n<p>Python<\/p>\n\n\n\n<p>from fastapi import Depends, HTTPException, Request<br>from app.utils import get_current_user<\/p>\n\n\n\n<p><strong>Create a Dependency Function:<\/strong><\/p>\n\n\n\n<p>Python<\/p>\n\n\n\n<p>def get_current_user(request: Request) -&gt; User:<br># \u2026 implementation<\/p>\n\n\n\n<p>This function will retrieve the current user based on the access token in the request headers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Using the Dependency in Endpoints<\/strong><\/h2>\n\n\n\n<p><strong>Inject the Dependency:<\/strong><\/p>\n\n\n\n<p>Python<\/p>\n\n\n\n<p>@app.get(&#8220;\/profile&#8221;)<br>async def get_profile(current_user: User = Depends(get_current_user)):<br>return current_user<\/p>\n\n\n\n<p><strong>Explanation<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code class=\"\">Depends<\/code> decorator automatically injects the <code class=\"\">current_user<\/code> object into the endpoint function.<\/li>\n\n\n\n<li>The <code class=\"\">get_current_user<\/code> function retrieves the current user based on the access token.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Additional Factors<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Error Handling:<\/strong> Implement appropriate error handling to deal with cases where the user cannot be retrieved or is unauthorized.<\/li>\n\n\n\n<li><strong>Caching:<\/strong> Consider caching the current user object to improve performance.<\/li>\n\n\n\n<li><strong>Security:<\/strong> Ensure that the access token is validated and protected against unauthorized access.<\/li>\n<\/ul>\n\n\n\n<p>By using dependency injection to fetch the current user in your FastAPI endpoints, you can promote code reusability, testability, and maintainability. This approach also makes it easier to manage dependencies and centralize configuration.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Dependency injection is a powerful technique in software development that promotes loose coupling and testability. In FastAPI, it&#8217;s commonly used to inject dependencies like database sessions, configuration settings, and services into your endpoints. In this section, we&#8217;ll demonstrate how to use dependency injection to fetch the current user in your FastAPI routes. Creating a Dependency&#8230;<\/p>\n","protected":false},"author":16,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-135416","page","type-page","status-publish","hentry"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Dependency Injection: Fetching the User - Tutorial<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/dependency-injection-fetching-the-user\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Dependency Injection: Fetching the User - Tutorial\" \/>\n<meta property=\"og:description\" content=\"Dependency injection is a powerful technique in software development that promotes loose coupling and testability. In FastAPI, it&#8217;s commonly used to inject dependencies like database sessions, configuration settings, and services into your endpoints. In this section, we&#8217;ll demonstrate how to use dependency injection to fetch the current user in your FastAPI routes. Creating a Dependency...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/dependency-injection-fetching-the-user\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/dependency-injection-fetching-the-user\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/dependency-injection-fetching-the-user\/\",\"name\":\"Dependency Injection: Fetching the User - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2024-09-06T07:00:51+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/dependency-injection-fetching-the-user\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/dependency-injection-fetching-the-user\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/dependency-injection-fetching-the-user\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Dependency Injection: Fetching the User\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Dependency Injection: Fetching the User - Tutorial","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/dependency-injection-fetching-the-user\/","og_locale":"en_US","og_type":"article","og_title":"Dependency Injection: Fetching the User - Tutorial","og_description":"Dependency injection is a powerful technique in software development that promotes loose coupling and testability. In FastAPI, it&#8217;s commonly used to inject dependencies like database sessions, configuration settings, and services into your endpoints. In this section, we&#8217;ll demonstrate how to use dependency injection to fetch the current user in your FastAPI routes. Creating a Dependency...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/dependency-injection-fetching-the-user\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/dependency-injection-fetching-the-user\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/dependency-injection-fetching-the-user\/","name":"Dependency Injection: Fetching the User - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2024-09-06T07:00:51+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/dependency-injection-fetching-the-user\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/dependency-injection-fetching-the-user\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/dependency-injection-fetching-the-user\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Dependency Injection: Fetching the User"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/135416","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=135416"}],"version-history":[{"count":2,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/135416\/revisions"}],"predecessor-version":[{"id":135420,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/135416\/revisions\/135420"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=135416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=135416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=135416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}