使用kbmmw 生成REST 服務OpenAPI函數原型


 

我們以前介紹了很多kbmmw 開發REST 的例子。一直有個問題困惑着大家。

我們提供REST 服務,如何讓客戶端快速的使用,當然可以寫文檔,但是一旦

后台改變了,又要再一次給調用者發新文檔,非常的麻煩。我們是否可以

自動生成調用函數原型,方便調用者使用JAVA,PHP,Delphi開發客戶端?

KBMMW 5.08 為我們完美的解決了這個問題。

我們還是以前面的REST 服務為例。稍微修改一下就支持openapi了。

當然了,這個需要借助一下開源的Swagger。網址http://editor.swagger.io/

我們直接到https://swagger.io/tools/swagger-ui/ 下載支持文件。

 

 

下載dist 目錄。 還要回到上一級目錄

繼續下載 swagger-editor.

同樣下載這個dist 目錄。

下載后的文件解壓,放到同一個目錄。

放到我們的服務器對應的目錄

在openapi 目錄下,新建一個index.html 文件。內容如下:

<!DOCTYPE html>
<!-- HTML for static distribution bundle build -->
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Swagger Editor</title>
  <style>
  * {
    box-sizing: border-box;
  }
  body {
    font-family: Roboto,sans-serif;
    font-size: 9px;
    line-height: 1.42857143;
    color: #444;
    margin: 0px;
  }
 
  #swagger-editor {
    font-size: 1.3em;
  }
 
  .container {
    height: 100%;
    max-width: 880px;
    margin-left: auto;
    margin-right: auto;
  }
 
  #editor-wrapper {
    height: 100%;
    border:1em solid #000;
    border:none;
  }
 
  .Pane2 {
    overflow-y: scroll;
  }
 
  </style>
  <link href="./dist/swagger-editor.css" rel="stylesheet">
  <link rel="icon" type="image/png" href="./dist/favicon-32x32.png" sizes="32x32" />
  <link rel="icon" type="image/png" href="./dist/favicon-16x16.png" sizes="16x16" />
</head>
 
<body>
  <div id="swagger-editor"></div>
  <script src="./dist/swagger-editor-bundle.js"> </script>
  <script src="./dist/swagger-editor-standalone-preset.js"> </script>
  <script>
  window.onload = function() {
    // Build a system
    const editor = SwaggerEditorBundle({
      dom_id: '#swagger-editor',
      layout: 'StandaloneLayout',
      presets: [
        SwaggerEditorStandalonePreset
      ]
    })
 
    window.editor = editor
  }
  </script>
 
  <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="position:absolute;width:0;height:0">
    <defs>
      <symbol viewBox="0 0 20 20" id="unlocked">
            <path d="M15.8 8H14V5.6C14 2.703 12.665 1 10 1 7.334 1 6 2.703 6 5.6V6h2v-.801C8 3.754 8.797 3 10 3c1.203 0 2 .754 2 2.199V8H4c-.553 0-1 .646-1 1.199V17c0 .549.428 1.139.951 1.307l1.197.387C5.672 18.861 6.55 19 7.1 19h5.8c.549 0 1.428-.139 1.951-.307l1.196-.387c.524-.167.953-.757.953-1.306V9.199C17 8.646 16.352 8 15.8 8z"></path>
      </symbol>
 
      <symbol viewBox="0 0 20 20" id="locked">
        <path d="M15.8 8H14V5.6C14 2.703 12.665 1 10 1 7.334 1 6 2.703 6 5.6V8H4c-.553 0-1 .646-1 1.199V17c0 .549.428 1.139.951 1.307l1.197.387C5.672 18.861 6.55 19 7.1 19h5.8c.549 0 1.428-.139 1.951-.307l1.196-.387c.524-.167.953-.757.953-1.306V9.199C17 8.646 16.352 8 15.8 8zM12 8H8V5.199C8 3.754 8.797 3 10 3c1.203 0 2 .754 2 2.199V8z"/>
      </symbol>
 
      <symbol viewBox="0 0 20 20" id="close">
        <path d="M14.348 14.849c-.469.469-1.229.469-1.697 0L10 11.819l-2.651 3.029c-.469.469-1.229.469-1.697 0-.469-.469-.469-1.229 0-1.697l2.758-3.15-2.759-3.152c-.469-.469-.469-1.228 0-1.697.469-.469 1.228-.469 1.697 0L10 8.183l2.651-3.031c.469-.469 1.228-.469 1.697 0 .469.469.469 1.229 0 1.697l-2.758 3.152 2.758 3.15c.469.469.469 1.229 0 1.698z"/>
      </symbol>
 
      <symbol viewBox="0 0 20 20" id="large-arrow">
        <path d="M13.25 10L6.109 2.58c-.268-.27-.268-.707 0-.979.268-.27.701-.27.969 0l7.83 7.908c.268.271.268.709 0 .979l-7.83 7.908c-.268.271-.701.27-.969 0-.268-.269-.268-.707 0-.979L13.25 10z"/>
      </symbol>
 
      <symbol viewBox="0 0 20 20" id="large-arrow-down">
        <path d="M17.418 6.109c.272-.268.709-.268.979 0s.271.701 0 .969l-7.908 7.83c-.27.268-.707.268-.979 0l-7.908-7.83c-.27-.268-.27-.701 0-.969.271-.268.709-.268.979 0L10 13.25l7.418-7.141z"/>
      </symbol>
 
 
      <symbol viewBox="0 0 24 24" id="jump-to">
        <path d="M19 7v4H5.83l3.58-3.59L8 6l-6 6 6 6 1.41-1.41L5.83 13H21V7z"/>
      </symbol>
 
      <symbol viewBox="0 0 24 24" id="expand">
        <path d="M10 18h4v-2h-4v2zM3 6v2h18V6H3zm3 7h12v-2H6v2z"/>
      </symbol>
 
    </defs>
  </svg>
 
</body>
 
</html>

好了,准備工作做好了。

我們開始修改kbmmw rest 服務器。

首先我們在smarthttpservice 里面引用 kbmMWSmartStubGenerator 單元。

新建一個過程

[kbmMW_Rest('method:get, path: "api", responseMimeType:"application/x-yaml"')]
     function OpenAPI:string;
function TkbmMWCustomHTTPSmartService1.OpenAPI: string;
begin
   // Return OpenAPI specification for all REST methods in this service
     // as YAML. Add the ASettings value: 'json:true' to return the specification
     // as JSON.
     // Add 'servers: [ "url1", "url2",.. "urln" ]' to ASettings if you want to
     // embed server location information in the specification.
     // Add 'inline:true' to inline object definitions instead of using $ref.
     // The example in the next line utilize the configuration framework to make
     // the setting easily configurable.
     Result:=TkbmMWSmartOpenAPIStubGenerator.GenerateOpenAPI('',self,'inline:$(OpenAPI.inline=false)');
end;

編譯運行。

我們在瀏覽器里面打開 連接 http://127.0.0.1/xalionrest/openapi/index.html?url=/xalionrest/api

 

 我的天哪。服務器上提供的函數都出來了。

我們甚至可以直接在瀏覽器里面測試服務器端提供的函數是否正常?

點開對應的函數

點 tryitout

點 Execute

 

圖像文件也沒問題

 

完美!

當然了,我們也可以通過這個直接生成java 和 php 的調用代碼。

一切都這么完美。

用飄柔就這么自信!

 附樣例函數聲明

 [kbmMW_Rest('method:$(service.xalionrest.helloworld.method=post), path:$(service.xalionrest.helloworld.path=helloworld)')]
    // [kbmMW_Method] 注釋了這個客戶端就不能訪問了
     function HelloWorld:string;

     [kbmMW_Rest('method:get, path: "echoheader", responseMimeType:"text/plain"')]
      function EchoHeader([kbmMW_Arg(mwatHeader,'Accept')] const AHeaderValue:string):string;
     [kbmMW_Rest('method:get, path: "echocookie", responseMimeType:"text/plain"')]
      function EchoCookie([kbmMW_Arg(mwatCookie,'MyCookie')] const ACookieValue:string):string;

     [kbmMW_Rest('method:get, path:version')]
     [kbmMW_Method]
     function version:string;

     [kbmMW_Method('EchoString')]       // 回應輸入的串
     [kbmMW_Rest('method:get, path: ["echostring/{AString}","myechostring/{AString}" ]')]
     [kbmMW_Auth('role:[SomeRole,SomeOtherRole], grant:true')]
     function EchoString([kbmMW_Rest('value: "{AString}"')] const AString:string):string;

     [kbmMW_Method]
     [kbmMW_Rest('method:get, path: "cal/addnumbers",summary:"兩個數相加",resultDescription:"返回相加和"')]
     function AddNumbers([kbmMW_Rest('value: "$arg1", required: true,description:"第一個數"')] const AValue1:integer;
                         [kbmMW_Rest('value: "$arg2", required: true,description:"第二個數"')] const AValue2:integer;
                         [kbmMW_Arg(mwatRemoteLocation)] const ARemoteLocation:string):string;



     [kbmMW_Method]
     [kbmMW_Rest('method:get, path: "gettoken",anonymousResult:False,freeResult:true')]
     function gettoken([kbmMW_Rest('value: "$appid", required: true')] const AValue1:string;
                         [kbmMW_Rest('value: "$acesskey", required: true')] const AValue2:string):Txalionresult;


     [kbmMW_Rest('method:post, path:postdata')]
     [kbmMW_Method]
     function postdata:string;

     [kbmMW_Rest('method:post, path:postfile')]
     [kbmMW_Method]
     function postfile:string;


     [kbmMW_Rest('method:get, path:getwithcheck,anonymousResult:False,freeResult:true')]
     [kbmMW_Method]
     function getwithcheck(

          [kbmMW_Rest('value: "$p1", required: true')] const p1:string;

          [kbmMW_Rest('value: "$p2", required: true')] const p2:string;

          [kbmMW_Rest('value: "$p3", required: true')] const p3:string;

          [kbmMW_Rest('value: "$AccessKeyId", required: true')] const AccessKeyId:string;

          [kbmMW_Rest('value: "$Timestamp", required: true')] const  Timestamp:string;
          [kbmMW_Rest('value: "$Action", required: true')] const  Action:string;

          [kbmMW_Rest('value: "$Signature", required: true')] const Signature:string):Txalionresult;

     [kbmMW_Rest('method:get, path:querydata')]
     [kbmMW_Method]
     function querydata(
          [kbmMW_Rest('value: "$tname", required: true')] const tblname:string;
          [kbmMW_Rest('value: "$id", required: False')] const id:string):string;
                   //,anonymousResult:False, freeResult:true

     [kbmMW_Rest('method:get, path:querytable,anonymousResult:true,freeResult:true')]
     [kbmMW_Method]
     [kbmMW_DataSet([mwdsfIncludeDefinitions])]
     function querytable(
          [kbmMW_Rest('value: "$tname", required: true')] const tblname:string;
          [kbmMW_Rest('value: "$id", required: False')] const id:string):Tkbmmemtable;

     [kbmMW_Rest('method:get, path:deletedata,anonymousResult:False,freeResult:true')]
     [kbmMW_Method]
     function deletedata(
          [kbmMW_Rest('value: "$tname",required: true')] const tblname:string;
          [kbmMW_Rest('value: "$id", required: true')] const id:string):Txalionresult;


    [kbmMW_Rest('method:post, path:poststring')]
     [kbmMW_Method]
     function poststring([kbmMW_Rest('value: "body", required: true')] const body:string):string;

    [kbmMW_Rest('method:get, path:"image/{no}", responseMimeType:"image/jpeg"')]
     function GetImage([kbmMW_Rest('value:"{no}"')] const Aname:string):TkbmMWBytes;

    [kbmMW_Rest('method:get, path:"image64/{no}",responseMimeType:"text/html"')]
     function GetImageB64([kbmMW_Rest('value:"{no}"')] const Aname:string):string;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM