Support > Wagby Developer Network(R7) > REST API usage guide > [Application example] Link Biz/Browser and Wagby with REST API
ja | en

Here is an example of linking Biz/Browser and Wagby with the REST API.

Biz/Browser is the platform for business application dedicated screen.You can design a business screen using Biz/Browser and link it with Wagby.

Fig. 1 Cooperation between Biz/Browser and Wagby

If it is difficult to realize on the HTML 5-based business screen automatically generated by Wagby, there is a possibility that it can be solved by linking with Biz/Browser.

It shows the cooperation image of Biz/Browser and Wagby.Figure 2 shows the logon screen developed by Biz/Browser.

Figure 2 Logon screen

Figure 3 shows the work screen developed by Biz/Browser.You can see that the layout and color tone are different from the standard screen of Wagby.

Figure 3 Application screen

In cooperation between Biz/Browser and Wagby, we will call Wagby using the REST API from the screen developed by Biz/Browser.

Fig. 4 Development screen (Biz/Designer)

The files prepared in this sample application are as follows.The extension crs is a script that Biz/Browser reads and executes.

Fig. 5 File list

Communication processing by REST API - Logon

The point is communication processing by the REST API.We will excerpt the code of G_HTTP_WAGBY.crs and introduce it.The code of the logon process is shown first.

import "G_SETTING.crs";
import "G_MSG.crs";
import "G_UTIL.crs";

Record G_HTTP_WAGBY {
    
    Function getSession(){
        return findHTTPSession( G_SETTING.APP_HOST_WAGBY.Value );
    }
    
    /**
     * ログオン
     * @param user
     * @param pass
     * @return なし
     * @throws 
     * @version 1.0
     */
    Function logon( user, pass ){
        /* httpSessionオブジェクトを検索 */
        var session = getSession();
        
        /* 新しいリクエストを作成 */
        var req = session.createRequest("/wagby/rest/session", HttpSession.UTF8_ENCODE);
        
        req.SetHeader( "Content-Type", "application/x-www-form-urlencoded" );
        
        req.addParam( "user", user );
        req.addParam( "pass", pass );
        
        /* レスポンス格納変数 */
        var res;
        var cookie;
        var jsondoc = new JSONDocument();
        var res_arr = new Array();

        try {
            /* リクエスト送信・レスポンス取得処理 */
            res = session.Put(req);
            
            /* HTTP ステータスエラー判別処理 */
            /* 401=ログオン失敗 */
            if( res.status == $HTTP_UNAUTHORIZED ){
                jsondoc.Parse( TextConverter.Convert( res.body, "UTF-8", "Shift_JIS" ) );
                res_arr["code"] = jsondoc.Value["errors"]["jfcerror"][0]["code"];
                res_arr["content"] = jsondoc.Value["errors"]["jfcerror"][0]["content"];
                res_arr["name"] = jsondoc.Value["errors"]["jfcerror"][0]["name"];
                res_arr["result"] = $False;
                return res_arr;
            } else if (res.status != $HTTP_OK) {
                throw new Exception("HTTP_ERROR", 1, res.status);
            }
            
            print( TextConverter.Convert( res, "UTF-8", "Shift_JIS" ) );
            jsondoc.Parse( TextConverter.Convert( res.body, "UTF-8", "Shift_JIS" ) );
            G_SETTING.r_user.userId.Value = jsondoc.Value["username"];
            
            res_arr["result"] = $True;
            
            /* JSESSIONIDを取得する */
            cookie = session.GetCookie();
            for (var i in cookie) {
                print("id=", cookie[i].cookieId, "\n");
                print("value=", cookie[i].cookieValue, "\n");
                if( cookie[i].cookieId == "JSESSIONID" ){
                    G_SETTING.JSESSIONID.Value = cookie[i].cookieValue;
                    break;
                }
            }
        } catch( ex ) {
            //.MessageBox( ex.message );
        }
        
        /* G_UTIL.showArray(jsondoc.Value); */
        /* G_UTIL.showArray(res_arr); */
        
        return res_arr;
    }
...
  • On line 24 we have a session with Wagby.Specify utf-8 as the character code.
  • Line 26 specifies application/x - www - form - urlencoded for Content - Type.
  • Logon ID and password are set in lines 28 and 29.
  • Logon processing is being executed on line 39.
  • Wagby's REST API returns the processing result in JSON format.You can receive this as a JSONDocument object in Biz/Browser.The character code is also converted to Shift_JIS.
  • On line 61, I handle cookies and get JSESSIONID.This is the session ID.

Communication processing by REST API - Log off

Similarly, log off code is shown.

    /**
     * ログオフ
     * @return なし
     * @throws 
     * @version 1.0
     */
    Function logoff(){
        /* httpSessionオブジェクトを検索 */
        var session = getSession();
        
        /* 新しいリクエストを作成 */
        var req = session.createRequest("/wagby/rest/session", HttpSession.UTF8_ENCODE);
        
        req.SetHeader( "Content-Type", "application/x-www-form-urlencoded" );
        req.SetHeader( "JSESSIONID", G_SETTING.JSESSIONID.Value );
        
        /* レスポンス格納変数 */
        var res;
        
        try {
            /* リクエスト送信・レスポンス取得処理 */
            res = session.Dele(req);
            
            /* HTTP ステータスエラー判別処理 */
            if (res.status != $HTTP_OK) {
                throw new Exception("HTTP_ERROR", 1, res.status);
            }
            
        } catch( ex ) {
            //.MessageBox( ex.message );
            return $False;
        }
        
        print( TextConverter.Convert( res, "UTF-8", "Shift_JIS" ) );
        
        return $True;
        
    }
  • We are calling Wagby's REST API specification logoff.
  • In line 15, JSESSIONID is set.The REST API should always pass JSESSIONID (obtained at logon).
  • We are doing a REST API call on line 22.

Communication processing by REST API - Information acquisition of logon user

Shows the code to retrieve logon user information.

    /**
     * ログオンユーザの情報取得
     * @return なし
     * @throws 
     * @version 1.0
     */
    Function getLogonUser(){
        /* httpSessionオブジェクトを検索 */
        var session = getSession();
        
        /* 新しいリクエストを作成 */
        var req = session.createRequest("/wagby/rest/session", HttpSession.UTF8_ENCODE);
        
        req.SetHeader( "Content-Type", "application/x-www-form-urlencoded" );
        req.SetHeader( "JSESSIONID", G_SETTING.JSESSIONID.Value );
        
        /* レスポンス格納変数 */
        var res;
        var cookie;
        var jsondoc = new JSONDocument();
        var res_arr = new Array();

        try {
            /* リクエスト送信・レスポンス取得処理 */
            res = session.Get(req);
            
            /* HTTP ステータスエラー判別処理 */
            /* 401=未認証時 */
            if( res.status == $HTTP_UNAUTHORIZED ){
                res_arr["result"] = $False;
                return res_arr;
            } else if (res.status != $HTTP_OK) {
                throw new Exception("HTTP_ERROR", 1, res.status);
            }
            
            print( TextConverter.Convert( res, "UTF-8", "Shift_JIS" ) );
            jsondoc.Parse( TextConverter.Convert( res.body, "UTF-8", "Shift_JIS" ) );
            G_SETTING.r_user.userId.Value = jsondoc.Value["username"];
            
            /* JSESSIONIDを取得する */
            cookie = session.GetCookie();
            for (var i in cookie) {
                print("id=", cookie[i].cookieId, "\n");
                print("value=", cookie[i].cookieValue, "\n");
                if( cookie[i].cookieId == "JSESSIONID" ){
                    G_SETTING.JSESSIONID.Value = cookie[i].cookieValue;
                    break;
                }
            }
        } catch( ex ) {
            //.MessageBox( ex.message );
        }
        
        /* G_UTIL.showArray(jsondoc.Value); */
        /* G_UTIL.showArray(res_arr); */
        
        return res_arr;
    }
  • In line 38 we are using jsondoc.Value to get the value from the return value JSON object.

Communication processing by REST API - Obtain list display data

When we pass the model ID as an argument, we will introduce the code to obtain the list display data of that model.

    /**
     * 一覧表示データを取得
     * @param modelId
     * @param mode 
     * @return なし
     * @throws 
     * @version 1.0
     */
    Function getList( modelId, param ){
        /* httpSessionオブジェクトを検索 */
        var session = getSession();
        
        /* 新しいリクエストを作成 */
        var req;
        if( param instanceof Array ){
            req = session.createRequest("/wagby/rest/" + modelId + "/list", HttpSession.UTF8_ENCODE);
            req.SetHeader( "Content-Type", "application/x-www-form-urlencoded" );
            for( var i in param ){
                req.AddParam( i , param[i] );
            }
        }else{
            switch( param ){
                case "first":   /* 最初のページの検索結果を取得 */
                case "last":    /* 最後のページの検索結果を取得 */
                case "next":    /* 次ページの検索結果を取得 */
                case "prev":    /* 前ページの検索結果を取得 */
                    req = session.createRequest("/wagby/rest/" + modelId + "/list/" + param, HttpSession.UTF8_ENCODE);
                    break;
                case "jump":    /* ページ番号を指定した検索結果を取得 */
                    req = session.createRequest("/wagby/rest/" + modelId + "/list/" + param + "/last", HttpSession.UTF8_ENCODE);
                    break;
                default:
                    req = session.createRequest("/wagby/rest/" + modelId + "/list", HttpSession.UTF8_ENCODE);
                    break;
            }
        }
        
        req.SetHeader( "JSESSIONID", G_SETTING.JSESSIONID.Value );
        
        /* レスポンス格納変数 */
        var res;
        var cookie;
        var jsondoc = new JSONDocument();
        var res_arr = new Array();

        try {
            /* リクエスト送信・レスポンス取得処理 */
            if( param instanceof Array ){
                res = session.Post(req);
            }else{
                res = session.Get(req);
            }
            
            /* HTTP ステータスエラー判別処理 */
            if (res.status != $HTTP_OK) {
                throw new Exception("HTTP_ERROR", 1, res.status);
            }
            
            print( TextConverter.Convert( res, "UTF-8", "Shift_JIS" ) );
            jsondoc.Parse( TextConverter.Convert( res.body, "UTF-8", "Shift_JIS" ) );
            
            res_arr["size"] = jsondoc.Value["result"]["size"];
            res_arr["next"] = jsondoc.Value["result"]["next"];
            res_arr["current"] = jsondoc.Value["result"]["current"];
            res_arr["nextPage"] = jsondoc.Value["result"]["nextPage"];
            res_arr["prevPage"] = jsondoc.Value["result"]["prevPage"];
            res_arr["entity"] = jsondoc.Value["entity"];
            res_arr["list"] = jsondoc.Value["listp"]["item_"];
            
        } catch( ex ) {
            //.MessageBox( ex.message );
        }
        
        /* G_UTIL.showArray(jsondoc.Value); */
        /* G_UTIL.showArray(res_arr); */
        
        return res_arr;
    }
  • In Wagby's REST API specification, list display data can be acquired with "/wagby/rest/model ID/list".
  • If the second argument param of getList is an array, it is used as the parameter of Wagby as it is.In the case of a single character string, it is treated as a comment.
  • Entities and lists contained in the returned JSON object are set in res_arr.This value will be used to draw the screen.(The description of the crs file for screen rendering will be omitted.Please read the manual of Biz/Browser.)

Communication processing by REST API - New registration

When we pass the model ID as an argument, we will introduce the code to perform the new registration process of that model.

    Function insert( modelId, param ){
        var rtnarr = new Array();
        
        /* httpSessionオブジェクトを検索 */
        var session = getSession();
        
        /* 新しいリクエストを作成 */
        var req = session.createRequest("/wagby/rest/" + modelId + "/new", HttpSession.UTF8_ENCODE);
        
        req.SetHeader( "Content-Type", "application/x-www-form-urlencoded" );
        req.SetHeader( "JSESSIONID", G_SETTING.JSESSIONID.Value );
        
        for( var i in param ){
            req.AddParam( i , param[i] );
        }
        
        /* レスポンス格納変数 */
        var res;
        var cookie;
        var jsondoc = new JSONDocument();
        var res_arr = new Array();
        
        try {
            /* リクエスト送信・レスポンス取得処理 */
            res = session.Post(req);
            
            print( TextConverter.Convert( res, "UTF-8", "Shift_JIS" ) );
            jsondoc.Parse( TextConverter.Convert( res.body, "UTF-8", "Shift_JIS" ) );
            /* HTTP ステータスエラー判別処理 */
            if (res.status != $HTTP_OK) {
                rtnarr["name"] = jsondoc.Value["errors"]["jfcerror"][0]["name"];
                rtnarr["content"] = jsondoc.Value["errors"]["jfcerror"][0]["content"];
                rtnarr["code"] = jsondoc.Value["errors"]["jfcerror"][0]["code"];
                rtnarr["result"] = $False;
            }else{
                rtnarr["name"] = jsondoc.Value["errors"]["jfcinfo"][0]["name"];
                rtnarr["content"] = jsondoc.Value["errors"]["jfcinfo"][0]["content"];
                rtnarr["code"] = jsondoc.Value["errors"]["jfcinfo"][0]["code"];
                rtnarr["entity"] = jsondoc.Value["entity"];
                rtnarr["result"] = $True;
            }        
        } catch( ex ) {
            //.MessageBox( ex.message );
        }
        
        return rtnarr;
    }
  • Wagby's REST API specification allows you to register data with "/wagby/rest/model ID/new".
  • Assume that the second argument param is passed data in a format that the REST API can accept.

Communication processing by REST API - Update

When we pass the model ID as an argument, we will introduce the code for updating the model.

    Function update( modelId, param, key ){
        var rtnarr = new Array();
        
        /* httpSessionオブジェクトを検索 */
        var session = getSession();
        
        if( key == null ){
            key = "";
        }else if( key != "" ){
            key = "/" + str( key );
        }
        
        /* 新しいリクエストを作成 */
        var req = session.createRequest("/wagby/rest/" + modelId + "/edit" + key, HttpSession.UTF8_ENCODE);
        
        req.SetHeader( "Content-Type", "application/x-www-form-urlencoded" );
        req.SetHeader( "JSESSIONID", G_SETTING.JSESSIONID.Value );
        
        for( var i in param ){
            req.AddParam( i , param[i] );
        }
        
        /* レスポンス格納変数 */
        var res;
        var cookie;
        var jsondoc = new JSONDocument();
        var res_arr = new Array();
        try {
            /* リクエスト送信・レスポンス取得処理 */
            res = session.Put(req);
            
            print( TextConverter.Convert( res, "UTF-8", "Shift_JIS" ) );
            jsondoc.Parse( TextConverter.Convert( res.body, "UTF-8", "Shift_JIS" ) );
            /* HTTP ステータスエラー判別処理 */
            if (res.status != $HTTP_OK) {
                rtnarr["name"] = jsondoc.Value["errors"]["jfcerror"][0]["name"];
                rtnarr["content"] = jsondoc.Value["errors"]["jfcerror"][0]["content"];
                rtnarr["code"] = jsondoc.Value["errors"]["jfcerror"][0]["code"];
                rtnarr["result"] = $False;
            }else{
                rtnarr["name"] = jsondoc.Value["errors"]["jfcinfo"][0]["name"];
                rtnarr["content"] = jsondoc.Value["errors"]["jfcinfo"][0]["content"];
                rtnarr["code"] = jsondoc.Value["errors"]["jfcinfo"][0]["code"];
                rtnarr["entity"] = jsondoc.Value["entity"];
                rtnarr["result"] = $True;
            }            
        } catch( ex ) {
            //.MessageBox( ex.message );
        }
        
        return rtnarr;
    }
  • In Wagby's REST API specification, data can be updated with "/wagby/rest/model ID/edit/primary key".Primary key is required.
  • Assume that the second argument param is passed data in a format that the REST API can accept.
  • The primary key is passed to the third argument key.

The sample application and a set of codes introduced here are prepared by Software Partner, Inc.

For demonstration and details on collaboration between Biz/Browser and Wagby, please contact the company.