{"id":226174,"date":"2018-12-19T09:57:31","date_gmt":"2018-12-19T01:57:31","guid":{"rendered":"http:\/\/magicalbits.net\/?p=226174"},"modified":"2018-12-19T09:57:31","modified_gmt":"2018-12-19T01:57:31","slug":"kitura-session-3-3-sharing-codable-objects-between-a-users-requests","status":"publish","type":"post","link":"https:\/\/magicalbits.net\/?p=226174","title":{"rendered":"Kitura-Session 3.3: Sharing Codable objects between a user&#8217;s requests"},"content":{"rendered":"<blockquote><p>In Kitura-Session 3.3, by defining the type we expect from the session, we get our Codable model straight out of the session:<\/p>\n<div>\n<div id=\"highlighter_301527\" class=\"syntaxhighlighter nogutter  swift\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code>router.<\/code><code>get<\/code><code>(<\/code><code>\"\/session\"<\/code><code>) { request, response, next <\/code><code>in<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>let<\/code> <code>books: [<\/code><code>Book<\/code><code>] = request.session?[<\/code><code>\"books\"<\/code><code>] ?? []<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>response.send(books)<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>next()<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code>}<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Even with a simple model, this has vastly reduced the amount of code required, added compile time safety and made our code easier to read!<\/p><\/blockquote>\n<p>Source: <em><a href=\"https:\/\/developer.ibm.com\/swift\/2018\/12\/18\/codable-kitura-session\/\">Kitura-Session 3.3: Sharing Codable objects between a user&#8217;s requests<\/a><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Kitura-Session 3.3, by defining the type we expect from the session, we get our Codable model straight out of the session: router.get(&#8220;\/session&#8221;) { request, response, next in \u00a0\u00a0\u00a0\u00a0let books: [Book] = request.session?[&#8220;books&#8221;] ?? [] \u00a0\u00a0\u00a0\u00a0response.send(books) \u00a0\u00a0\u00a0\u00a0next() } Even with a simple model, this has vastly reduced the amount of code required, added compile time [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ep_exclude_from_search":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-226174","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/magicalbits.net\/index.php?rest_route=\/wp\/v2\/posts\/226174","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/magicalbits.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/magicalbits.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/magicalbits.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/magicalbits.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=226174"}],"version-history":[{"count":1,"href":"https:\/\/magicalbits.net\/index.php?rest_route=\/wp\/v2\/posts\/226174\/revisions"}],"predecessor-version":[{"id":226175,"href":"https:\/\/magicalbits.net\/index.php?rest_route=\/wp\/v2\/posts\/226174\/revisions\/226175"}],"wp:attachment":[{"href":"https:\/\/magicalbits.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=226174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/magicalbits.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=226174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/magicalbits.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=226174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}