<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;
        mso-fareast-language:EN-US;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:#0563C1;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:#954F72;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri",sans-serif;
        mso-fareast-language:EN-US;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-GB" link="#0563C1" vlink="#954F72">
<div class="WordSection1">
<p class="MsoNormal">Hi all – I know it’s Kohacon this week so a lot of people will be busy there, but just in case ……… (also sorry for the length of the email!):<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I testing creating a custom API endpoint using the Koha plugin system. I’ve been using the code in the Kitchen sink plugin as a template, and it seems to be working well apart from my lack of Swagger/OpenAPI knowledge – I think it’s this
 I struggling with. I have three files:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">koha-dev/var/lib/plugins/Koha/Plugin/Uk/Ac/Herts/Notifications.pm
<o:p></o:p></p>
<p class="MsoNormal">koha-dev/var/lib/plugins/Koha/Plugin/Uk/Ac/Herts/Notifications/NotificationController.pm<o:p></o:p></p>
<p class="MsoNormal">koha-dev/var/lib/plugins/Koha/Plugin/Uk/Ac/Herts/Notifications/openapi.json<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">The work is done in NotificationController.pm where I have a DBI call to get the following fields:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Borrower.userid<o:p></o:p></p>
<p class="MsoNormal">Message_queue.message_id<o:p></o:p></p>
<p class="MsoNormal">Message_queue.subject<o:p></o:p></p>
<p class="MsoNormal">Message_queue.content<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I fetch the data, and convert into JSON (encode_json is a method from Mojo::JSON) like:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">my $data = $sth->fetchall_arrayref({}); # return both fieldnames and values<o:p></o:p></p>
<p class="MsoNormal">my $json = encode_json($data);<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">if I then do (i.e. use text):<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">return $c->render( status => 200, text => $json)<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">it works fine. If I call <a href="https://herttest-staff.koha-ptfs.co.uk/api/v1/contrib/UH/notifications">
https://herttest-staff.koha-ptfs.co.uk/api/v1/contrib/UH/notifications</a> I get json like:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">[ {<o:p></o:p></p>
<p class="MsoNormal">               “body”: “blah blah”,<o:p></o:p></p>
<p class="MsoNormal">               “messageid”: “1234567”,<o:p></o:p></p>
<p class="MsoNormal">               “subject”: “Important…”,<o:p></o:p></p>
<p class="MsoNormal">               “userid”:  99999999”<o:p></o:p></p>
<p class="MsoNormal">} {<o:p></o:p></p>
<p class="MsoNormal">               Etc etc<o:p></o:p></p>
<p class="MsoNormal">}]<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">So in Perl terms it’s an array of hashes. Which is what I want. However, if I try and use the openapi way that all the current “official” Koha endpoints use e.g.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">return $c->render( status => 200, openapi => { notices => $json } );
<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">it just doesn’t work. I have tried to map the field names with the appropriate types in openapi.json. I’ve tried object type with properties e.g.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">"responses": {<o:p></o:p></p>
<p class="MsoNormal">        "200": {<o:p></o:p></p>
<p class="MsoNormal">          "description": "A list of pending notices",<o:p></o:p></p>
<p class="MsoNormal">          "schema": {<o:p></o:p></p>
<p class="MsoNormal">                "type": "object",<o:p></o:p></p>
<p class="MsoNormal">                "properties": {<o:p></o:p></p>
<p class="MsoNormal">                "userid": {<o:p></o:p></p>
<p class="MsoNormal">                        "type": "string",<o:p></o:p></p>
<p class="MsoNormal">                        "description": "Unique User ID"<o:p></o:p></p>
<p class="MsoNormal">                }, etc<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">, and I’ve tried array type with items:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">"responses": {<o:p></o:p></p>
<p class="MsoNormal">        "200": {<o:p></o:p></p>
<p class="MsoNormal">          "description": "A list of pending notices",<o:p></o:p></p>
<p class="MsoNormal">          "schema": {<o:p></o:p></p>
<p class="MsoNormal">                "type": "array",<o:p></o:p></p>
<p class="MsoNormal">                "items": {<o:p></o:p></p>
<p class="MsoNormal">                "userid": {<o:p></o:p></p>
<p class="MsoNormal">                        "type": "string",<o:p></o:p></p>
<p class="MsoNormal">                        "description": "Unique User ID"<o:p></o:p></p>
<p class="MsoNormal">                }, etc<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">But I get a page not found error. When I pass my $json variable to the render method using openapi how do I get it to work? I’m happy to use the text way, but I want to understand where I’m going wrong. Any ideas, hints, tips would be most
 welcome!<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Cheers, Stephen<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-GB">--------------------------------------<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-GB">Stephen Graham<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-GB">Library Technology Consultant<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-GB">Content and Collections Team<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-GB">Library and Computing Services<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-GB">University of Hertfordshire<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-GB">Tel: 01707 286111<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-GB">Ext: 77751<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-GB">Email: <a href="mailto:s.graham4@herts.ac.uk">
<span style="color:#0563C1">s.graham4@herts.ac.uk</span></a><o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</body>
</html>