/usr/share/doc/python-flask-restful-doc/html/intermediate-usage.html is in python-flask-restful-doc 0.3.6-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Intermediate Usage — Flask-RESTful 0.3.6 documentation</title>
<link rel="stylesheet" href="_static/flasky.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '0.3.6',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="API Docs" href="api.html" />
<link rel="prev" title="Extending Flask-RESTful" href="extending.html" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9">
</head>
<body role="document">
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="api.html" title="API Docs"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="extending.html" title="Extending Flask-RESTful"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">Flask-RESTful 0.3.6 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="intermediate-usage">
<span id="intermediate"></span><h1>Intermediate Usage<a class="headerlink" href="#intermediate-usage" title="Permalink to this headline">¶</a></h1>
<p>This page covers building a slightly more complex Flask-RESTful app that will
cover out some best practices when setting up a real-world Flask-RESTful-based
API. The <a class="reference internal" href="quickstart.html#quickstart"><span class="std std-ref">Quickstart</span></a> section is great for getting started with your first
Flask-RESTful app, so if you’re new to Flask-RESTful you’d be better off
checking that out first.</p>
<div class="section" id="project-structure">
<h2>Project Structure<a class="headerlink" href="#project-structure" title="Permalink to this headline">¶</a></h2>
<p>There are many different ways to organize your Flask-RESTful app, but here
we’ll describe one that scales pretty well with larger apps and maintains
a nice level organization.</p>
<p>The basic idea is to split your app into three main parts: the routes, the
resources, and any common infrastructure.</p>
<p>Here’s an example directory structure:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">myapi</span><span class="o">/</span>
<span class="fm">__init__</span><span class="o">.</span><span class="n">py</span>
<span class="n">app</span><span class="o">.</span><span class="n">py</span> <span class="c1"># this file contains your app and routes</span>
<span class="n">resources</span><span class="o">/</span>
<span class="fm">__init__</span><span class="o">.</span><span class="n">py</span>
<span class="n">foo</span><span class="o">.</span><span class="n">py</span> <span class="c1"># contains logic for /Foo</span>
<span class="n">bar</span><span class="o">.</span><span class="n">py</span> <span class="c1"># contains logic for /Bar</span>
<span class="n">common</span><span class="o">/</span>
<span class="fm">__init__</span><span class="o">.</span><span class="n">py</span>
<span class="n">util</span><span class="o">.</span><span class="n">py</span> <span class="c1"># just some common infrastructure</span>
</pre></div>
</div>
<p>The common directory would probably just contain a set of helper functions
to fulfill common needs across your application. It could also contain, for
example, any custom input/output types your resources need to get the job done.</p>
<p>In the resource files, you just have your resource objects. So here’s what
<code class="docutils literal"><span class="pre">foo.py</span></code> might look like:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">flask_restful</span> <span class="k">import</span> <span class="n">Resource</span>
<span class="k">class</span> <span class="nc">Foo</span><span class="p">(</span><span class="n">Resource</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">pass</span>
<span class="k">def</span> <span class="nf">post</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">pass</span>
</pre></div>
</div>
<p>The key to this setup lies in <code class="docutils literal"><span class="pre">app.py</span></code>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">flask</span> <span class="k">import</span> <span class="n">Flask</span>
<span class="kn">from</span> <span class="nn">flask_restful</span> <span class="k">import</span> <span class="n">Api</span>
<span class="kn">from</span> <span class="nn">myapi.resources.foo</span> <span class="k">import</span> <span class="n">Foo</span>
<span class="kn">from</span> <span class="nn">myapi.resources.bar</span> <span class="k">import</span> <span class="n">Bar</span>
<span class="kn">from</span> <span class="nn">myapi.resources.baz</span> <span class="k">import</span> <span class="n">Baz</span>
<span class="n">app</span> <span class="o">=</span> <span class="n">Flask</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>
<span class="n">api</span> <span class="o">=</span> <span class="n">Api</span><span class="p">(</span><span class="n">app</span><span class="p">)</span>
<span class="n">api</span><span class="o">.</span><span class="n">add_resource</span><span class="p">(</span><span class="n">Foo</span><span class="p">,</span> <span class="s1">'/Foo'</span><span class="p">,</span> <span class="s1">'/Foo/<str:id>'</span><span class="p">)</span>
<span class="n">api</span><span class="o">.</span><span class="n">add_resource</span><span class="p">(</span><span class="n">Bar</span><span class="p">,</span> <span class="s1">'/Bar'</span><span class="p">,</span> <span class="s1">'/Bar/<str:id>'</span><span class="p">)</span>
<span class="n">api</span><span class="o">.</span><span class="n">add_resource</span><span class="p">(</span><span class="n">Baz</span><span class="p">,</span> <span class="s1">'/Baz'</span><span class="p">,</span> <span class="s1">'/Baz/<str:id>'</span><span class="p">)</span>
</pre></div>
</div>
<p>As you can imagine with a particularly large or complex API, this file ends up
being very valuable as a comprehensive list of all the routes and resources in
your API. You would also use this file to set up any config values
(<a class="reference external" href="/usr/share/doc/python-flask-doc/html/api.html#flask.Flask.before_request" title="(in Flask v0.12)"><code class="xref py py-meth docutils literal"><span class="pre">before_request()</span></code></a>, <a class="reference external" href="/usr/share/doc/python-flask-doc/html/api.html#flask.Flask.after_request" title="(in Flask v0.12)"><code class="xref py py-meth docutils literal"><span class="pre">after_request()</span></code></a>).
Basically, this file configures your entire API.</p>
<p>The things in the common directory are just things you’d want to support your
resource modules.</p>
</div>
<div class="section" id="use-with-blueprints">
<h2>Use With Blueprints<a class="headerlink" href="#use-with-blueprints" title="Permalink to this headline">¶</a></h2>
<p>See <a class="reference external" href="/usr/share/doc/python-flask-doc/html/blueprints.html#blueprints" title="(in Flask v0.12)"><span>Modular Applications with Blueprints</span></a> in the Flask documentation for what blueprints are and
why you should use them. Here’s an example of how to link an <a class="reference internal" href="api.html#flask_restful.Api" title="flask_restful.Api"><code class="xref py py-class docutils literal"><span class="pre">Api</span></code></a>
up to a <a class="reference external" href="/usr/share/doc/python-flask-doc/html/api.html#flask.Blueprint" title="(in Flask v0.12)"><code class="xref py py-class docutils literal"><span class="pre">Blueprint</span></code></a>.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">flask</span> <span class="k">import</span> <span class="n">Flask</span><span class="p">,</span> <span class="n">Blueprint</span>
<span class="kn">from</span> <span class="nn">flask_restful</span> <span class="k">import</span> <span class="n">Api</span><span class="p">,</span> <span class="n">Resource</span><span class="p">,</span> <span class="n">url_for</span>
<span class="n">app</span> <span class="o">=</span> <span class="n">Flask</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>
<span class="n">api_bp</span> <span class="o">=</span> <span class="n">Blueprint</span><span class="p">(</span><span class="s1">'api'</span><span class="p">,</span> <span class="vm">__name__</span><span class="p">)</span>
<span class="n">api</span> <span class="o">=</span> <span class="n">Api</span><span class="p">(</span><span class="n">api_bp</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">TodoItem</span><span class="p">(</span><span class="n">Resource</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">id</span><span class="p">):</span>
<span class="k">return</span> <span class="p">{</span><span class="s1">'task'</span><span class="p">:</span> <span class="s1">'Say "Hello, World!"'</span><span class="p">}</span>
<span class="n">api</span><span class="o">.</span><span class="n">add_resource</span><span class="p">(</span><span class="n">TodoItem</span><span class="p">,</span> <span class="s1">'/todos/<int:id>'</span><span class="p">)</span>
<span class="n">app</span><span class="o">.</span><span class="n">register_blueprint</span><span class="p">(</span><span class="n">api_bp</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Calling <a class="reference internal" href="api.html#flask_restful.Api.init_app" title="flask_restful.Api.init_app"><code class="xref py py-meth docutils literal"><span class="pre">Api.init_app()</span></code></a> is not required here because registering the
blueprint with the app takes care of setting up the routing for the
application.</p>
</div>
</div>
<div class="section" id="full-parameter-parsing-example">
<h2>Full Parameter Parsing Example<a class="headerlink" href="#full-parameter-parsing-example" title="Permalink to this headline">¶</a></h2>
<p>Elsewhere in the documentation, we’ve described how to use the reqparse example
in detail. Here we’ll set up a resource with multiple input parameters that
exercise a larger amount of options. We’ll define a resource named “User”.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">flask_restful</span> <span class="k">import</span> <span class="n">fields</span><span class="p">,</span> <span class="n">marshal_with</span><span class="p">,</span> <span class="n">reqparse</span><span class="p">,</span> <span class="n">Resource</span>
<span class="k">def</span> <span class="nf">email</span><span class="p">(</span><span class="n">email_str</span><span class="p">):</span>
<span class="sd">"""Return email_str if valid, raise an exception in other case."""</span>
<span class="k">if</span> <span class="n">valid_email</span><span class="p">(</span><span class="n">email_str</span><span class="p">):</span>
<span class="k">return</span> <span class="n">email_str</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">'</span><span class="si">{}</span><span class="s1"> is not a valid email'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">email_str</span><span class="p">))</span>
<span class="n">post_parser</span> <span class="o">=</span> <span class="n">reqparse</span><span class="o">.</span><span class="n">RequestParser</span><span class="p">()</span>
<span class="n">post_parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span>
<span class="s1">'username'</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s1">'username'</span><span class="p">,</span>
<span class="n">location</span><span class="o">=</span><span class="s1">'form'</span><span class="p">,</span> <span class="n">required</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
<span class="n">help</span><span class="o">=</span><span class="s1">'The user</span><span class="se">\'</span><span class="s1">s username'</span><span class="p">,</span>
<span class="p">)</span>
<span class="n">post_parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span>
<span class="s1">'email'</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s1">'email'</span><span class="p">,</span>
<span class="nb">type</span><span class="o">=</span><span class="n">email</span><span class="p">,</span> <span class="n">location</span><span class="o">=</span><span class="s1">'form'</span><span class="p">,</span>
<span class="n">required</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s1">'The user</span><span class="se">\'</span><span class="s1">s email'</span><span class="p">,</span>
<span class="p">)</span>
<span class="n">post_parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span>
<span class="s1">'user_priority'</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s1">'user_priority'</span><span class="p">,</span>
<span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">location</span><span class="o">=</span><span class="s1">'form'</span><span class="p">,</span>
<span class="n">default</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">choices</span><span class="o">=</span><span class="nb">range</span><span class="p">(</span><span class="mi">5</span><span class="p">),</span> <span class="n">help</span><span class="o">=</span><span class="s1">'The user</span><span class="se">\'</span><span class="s1">s priority'</span><span class="p">,</span>
<span class="p">)</span>
<span class="n">user_fields</span> <span class="o">=</span> <span class="p">{</span>
<span class="s1">'id'</span><span class="p">:</span> <span class="n">fields</span><span class="o">.</span><span class="n">Integer</span><span class="p">,</span>
<span class="s1">'username'</span><span class="p">:</span> <span class="n">fields</span><span class="o">.</span><span class="n">String</span><span class="p">,</span>
<span class="s1">'email'</span><span class="p">:</span> <span class="n">fields</span><span class="o">.</span><span class="n">String</span><span class="p">,</span>
<span class="s1">'user_priority'</span><span class="p">:</span> <span class="n">fields</span><span class="o">.</span><span class="n">Integer</span><span class="p">,</span>
<span class="s1">'custom_greeting'</span><span class="p">:</span> <span class="n">fields</span><span class="o">.</span><span class="n">FormattedString</span><span class="p">(</span><span class="s1">'Hey there </span><span class="si">{username}</span><span class="s1">!'</span><span class="p">),</span>
<span class="s1">'date_created'</span><span class="p">:</span> <span class="n">fields</span><span class="o">.</span><span class="n">DateTime</span><span class="p">,</span>
<span class="s1">'date_updated'</span><span class="p">:</span> <span class="n">fields</span><span class="o">.</span><span class="n">DateTime</span><span class="p">,</span>
<span class="s1">'links'</span><span class="p">:</span> <span class="n">fields</span><span class="o">.</span><span class="n">Nested</span><span class="p">({</span>
<span class="s1">'friends'</span><span class="p">:</span> <span class="n">fields</span><span class="o">.</span><span class="n">Url</span><span class="p">(</span><span class="s1">'user_friends'</span><span class="p">),</span>
<span class="s1">'posts'</span><span class="p">:</span> <span class="n">fields</span><span class="o">.</span><span class="n">Url</span><span class="p">(</span><span class="s1">'user_posts'</span><span class="p">),</span>
<span class="p">}),</span>
<span class="p">}</span>
<span class="k">class</span> <span class="nc">User</span><span class="p">(</span><span class="n">Resource</span><span class="p">):</span>
<span class="nd">@marshal_with</span><span class="p">(</span><span class="n">user_fields</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">post</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">args</span> <span class="o">=</span> <span class="n">post_parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span>
<span class="n">user</span> <span class="o">=</span> <span class="n">create_user</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">username</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">email</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">user_priority</span><span class="p">)</span>
<span class="k">return</span> <span class="n">user</span>
<span class="nd">@marshal_with</span><span class="p">(</span><span class="n">user_fields</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">id</span><span class="p">):</span>
<span class="n">args</span> <span class="o">=</span> <span class="n">post_parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span>
<span class="n">user</span> <span class="o">=</span> <span class="n">fetch_user</span><span class="p">(</span><span class="nb">id</span><span class="p">)</span>
<span class="k">return</span> <span class="n">user</span>
</pre></div>
</div>
<p>As you can see, we create a <code class="docutils literal"><span class="pre">post_parser</span></code> specifically to handle the parsing
of arguments provided on POST. Let’s step through the definition of each
argument.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">post_parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span>
<span class="s1">'username'</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s1">'username'</span><span class="p">,</span>
<span class="n">location</span><span class="o">=</span><span class="s1">'form'</span><span class="p">,</span> <span class="n">required</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
<span class="n">help</span><span class="o">=</span><span class="s1">'The user</span><span class="se">\'</span><span class="s1">s username'</span><span class="p">,</span>
<span class="p">)</span>
</pre></div>
</div>
<p>The <code class="docutils literal"><span class="pre">username</span></code> field is the most normal out of all of them. It takes
a string from the POST body and converts it to a string type. This argument
is required (<code class="docutils literal"><span class="pre">required=True</span></code>), which means that if it isn’t provided,
Flask-RESTful will automatically return a 400 with a message along the lines
of ‘the username field is required’.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">post_parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span>
<span class="s1">'email'</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s1">'email'</span><span class="p">,</span>
<span class="nb">type</span><span class="o">=</span><span class="n">email</span><span class="p">,</span> <span class="n">location</span><span class="o">=</span><span class="s1">'form'</span><span class="p">,</span>
<span class="n">required</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s1">'The user</span><span class="se">\'</span><span class="s1">s email'</span><span class="p">,</span>
<span class="p">)</span>
</pre></div>
</div>
<p>The <code class="docutils literal"><span class="pre">email</span></code> field has a custom type of <code class="docutils literal"><span class="pre">email</span></code>. A few lines earlier we
defined an <code class="docutils literal"><span class="pre">email</span></code> function that takes a string and returns it if the type is
valid, else it raises an exception, exclaiming that the email type was
invalid.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">post_parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span>
<span class="s1">'user_priority'</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s1">'user_priority'</span><span class="p">,</span>
<span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">location</span><span class="o">=</span><span class="s1">'form'</span><span class="p">,</span>
<span class="n">default</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">choices</span><span class="o">=</span><span class="nb">range</span><span class="p">(</span><span class="mi">5</span><span class="p">),</span> <span class="n">help</span><span class="o">=</span><span class="s1">'The user</span><span class="se">\'</span><span class="s1">s priority'</span><span class="p">,</span>
<span class="p">)</span>
</pre></div>
</div>
<p>The <code class="docutils literal"><span class="pre">user_priority</span></code> type takes advantage of the <code class="docutils literal"><span class="pre">choices</span></code> argument. This
means that if the provided <cite>user_priority</cite> value doesn’t fall in the range
specified by the <code class="docutils literal"><span class="pre">choices</span></code> argument (in this case <code class="docutils literal"><span class="pre">[0,</span> <span class="pre">1,</span> <span class="pre">2,</span> <span class="pre">3,</span> <span class="pre">4]</span></code>),
Flask-RESTful will automatically respond with a 400 and a descriptive error
message.</p>
<p>That covers the inputs. We also defined some interesting field types in the
<code class="docutils literal"><span class="pre">user_fields</span></code> dictionary to showcase a couple of the more exotic types.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">user_fields</span> <span class="o">=</span> <span class="p">{</span>
<span class="s1">'id'</span><span class="p">:</span> <span class="n">fields</span><span class="o">.</span><span class="n">Integer</span><span class="p">,</span>
<span class="s1">'username'</span><span class="p">:</span> <span class="n">fields</span><span class="o">.</span><span class="n">String</span><span class="p">,</span>
<span class="s1">'email'</span><span class="p">:</span> <span class="n">fields</span><span class="o">.</span><span class="n">String</span><span class="p">,</span>
<span class="s1">'user_priority'</span><span class="p">:</span> <span class="n">fields</span><span class="o">.</span><span class="n">Integer</span><span class="p">,</span>
<span class="s1">'custom_greeting'</span><span class="p">:</span> <span class="n">fields</span><span class="o">.</span><span class="n">FormattedString</span><span class="p">(</span><span class="s1">'Hey there </span><span class="si">{username}</span><span class="s1">!'</span><span class="p">),</span>
<span class="s1">'date_created'</span><span class="p">:</span> <span class="n">fields</span><span class="o">.</span><span class="n">DateTime</span><span class="p">,</span>
<span class="s1">'date_updated'</span><span class="p">:</span> <span class="n">fields</span><span class="o">.</span><span class="n">DateTime</span><span class="p">,</span>
<span class="s1">'links'</span><span class="p">:</span> <span class="n">fields</span><span class="o">.</span><span class="n">Nested</span><span class="p">({</span>
<span class="s1">'friends'</span><span class="p">:</span> <span class="n">fields</span><span class="o">.</span><span class="n">Url</span><span class="p">(</span><span class="s1">'user_friends'</span><span class="p">,</span> <span class="n">absolute</span><span class="o">=</span><span class="kc">True</span><span class="p">),</span>
<span class="s1">'posts'</span><span class="p">:</span> <span class="n">fields</span><span class="o">.</span><span class="n">Url</span><span class="p">(</span><span class="s1">'user_friends'</span><span class="p">,</span> <span class="n">absolute</span><span class="o">=</span><span class="kc">True</span><span class="p">),</span>
<span class="p">}),</span>
<span class="p">}</span>
</pre></div>
</div>
<p>First up, there’s <a class="reference internal" href="api.html#fields.FormattedString" title="fields.FormattedString"><code class="xref py py-class docutils literal"><span class="pre">fields.FormattedString</span></code></a>.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="s1">'custom_greeting'</span><span class="p">:</span> <span class="n">fields</span><span class="o">.</span><span class="n">FormattedString</span><span class="p">(</span><span class="s1">'Hey there </span><span class="si">{username}</span><span class="s1">!'</span><span class="p">),</span>
</pre></div>
</div>
<p>This field is primarily used to interpolate values from the response into
other values. In this instance, <code class="docutils literal"><span class="pre">custom_greeting</span></code> will always contain the
value returned from the <code class="docutils literal"><span class="pre">username</span></code> field.</p>
<p>Next up, check out <a class="reference internal" href="api.html#fields.Nested" title="fields.Nested"><code class="xref py py-class docutils literal"><span class="pre">fields.Nested</span></code></a>.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="s1">'links'</span><span class="p">:</span> <span class="n">fields</span><span class="o">.</span><span class="n">Nested</span><span class="p">({</span>
<span class="s1">'friends'</span><span class="p">:</span> <span class="n">fields</span><span class="o">.</span><span class="n">Url</span><span class="p">(</span><span class="s1">'user_friends'</span><span class="p">,</span> <span class="n">absolute</span><span class="o">=</span><span class="kc">True</span><span class="p">),</span>
<span class="s1">'posts'</span><span class="p">:</span> <span class="n">fields</span><span class="o">.</span><span class="n">Url</span><span class="p">(</span><span class="s1">'user_posts'</span><span class="p">,</span> <span class="n">absolute</span><span class="o">=</span><span class="kc">True</span><span class="p">),</span>
<span class="p">}),</span>
</pre></div>
</div>
<p>This field is used to create a sub-object in the response. In this case,
we want to create a <code class="docutils literal"><span class="pre">links</span></code> sub-object to contain urls of related objects.
Note that we passed <cite>fields.Nested</cite> another dict which is built in such a
way that it would be an acceptable argument to <a class="reference internal" href="api.html#flask_restful.marshal" title="flask_restful.marshal"><code class="xref py py-func docutils literal"><span class="pre">marshal()</span></code></a> by itself.</p>
<p>Finally, we used the <a class="reference internal" href="api.html#fields.Url" title="fields.Url"><code class="xref py py-class docutils literal"><span class="pre">fields.Url</span></code></a> field type.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="s1">'friends'</span><span class="p">:</span> <span class="n">fields</span><span class="o">.</span><span class="n">Url</span><span class="p">(</span><span class="s1">'user_friends'</span><span class="p">,</span> <span class="n">absolute</span><span class="o">=</span><span class="kc">True</span><span class="p">),</span>
<span class="s1">'posts'</span><span class="p">:</span> <span class="n">fields</span><span class="o">.</span><span class="n">Url</span><span class="p">(</span><span class="s1">'user_friends'</span><span class="p">,</span> <span class="n">absolute</span><span class="o">=</span><span class="kc">True</span><span class="p">),</span>
</pre></div>
</div>
<p>It takes as its first parameter the name of the endpoint associated with the
urls of the objects in the <code class="docutils literal"><span class="pre">links</span></code> sub-object. Passing <code class="docutils literal"><span class="pre">absolute=True</span></code>
ensures that the generated urls will have the hostname included.</p>
</div>
<div class="section" id="passing-constructor-parameters-into-resources">
<h2>Passing Constructor Parameters Into Resources<a class="headerlink" href="#passing-constructor-parameters-into-resources" title="Permalink to this headline">¶</a></h2>
<p>Your <a class="reference internal" href="api.html#flask_restful.Resource" title="flask_restful.Resource"><code class="xref py py-class docutils literal"><span class="pre">Resource</span></code></a> implementation may require outside dependencies. Those
dependencies are best passed-in through the constructor to loosely couple each
other. The <a class="reference internal" href="api.html#flask_restful.Api.add_resource" title="flask_restful.Api.add_resource"><code class="xref py py-meth docutils literal"><span class="pre">Api.add_resource()</span></code></a> method has two keyword arguments:
<code class="docutils literal"><span class="pre">resource_class_args</span></code> and <code class="docutils literal"><span class="pre">resource_class_kwargs</span></code>. Their values will be forwarded
and passed into your Resource implementation’s constructor.</p>
<p>So you could have a <a class="reference internal" href="api.html#flask_restful.Resource" title="flask_restful.Resource"><code class="xref py py-class docutils literal"><span class="pre">Resource</span></code></a>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">flask_restful</span> <span class="k">import</span> <span class="n">Resource</span>
<span class="k">class</span> <span class="nc">TodoNext</span><span class="p">(</span><span class="n">Resource</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="c1"># smart_engine is a black box dependency</span>
<span class="bp">self</span><span class="o">.</span><span class="n">smart_engine</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">'smart_engine'</span><span class="p">]</span>
<span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">smart_engine</span><span class="o">.</span><span class="n">next_todo</span><span class="p">()</span>
</pre></div>
</div>
<p>You can inject the required dependency into TodoNext like so:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">smart_engine</span> <span class="o">=</span> <span class="n">SmartEngine</span><span class="p">()</span>
<span class="n">api</span><span class="o">.</span><span class="n">add_resource</span><span class="p">(</span><span class="n">TodoNext</span><span class="p">,</span> <span class="s1">'/next'</span><span class="p">,</span>
<span class="n">resource_class_kwargs</span><span class="o">=</span><span class="p">{</span> <span class="s1">'smart_engine'</span><span class="p">:</span> <span class="n">smart_engine</span> <span class="p">})</span>
</pre></div>
</div>
<p>Same idea applies for forwarding <cite>args</cite>.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper"><p class="logo"><a href="index.html">
<img class="logo" src="_static/flask-restful-small.png" alt="Logo"
style="margin-left: -10px"/>
</a></p>
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Intermediate Usage</a><ul>
<li><a class="reference internal" href="#project-structure">Project Structure</a></li>
<li><a class="reference internal" href="#use-with-blueprints">Use With Blueprints</a></li>
<li><a class="reference internal" href="#full-parameter-parsing-example">Full Parameter Parsing Example</a></li>
<li><a class="reference internal" href="#passing-constructor-parameters-into-resources">Passing Constructor Parameters Into Resources</a></li>
</ul>
</li>
</ul>
<h3>Related Topics</h3>
<ul>
<li><a href="index.html">Documentation overview</a><ul>
<li>Previous: <a href="extending.html" title="previous chapter">Extending Flask-RESTful</a></li>
<li>Next: <a href="api.html" title="next chapter">API Docs</a></li>
</ul></li>
</ul>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/intermediate-usage.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
© Copyright 2017, Kevin Burke, Kyle Conroy, Ryan Horn, Frank Stratton, Guillaume Binet.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>.
</div>
</body>
</html>
|