1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Postman Visualizerを試してみます(REST API Version)

Last updated at Posted at 2024-01-05

REST APIのVersionを実行すると以下のようなレスポンスが返ってきます。

[{"label":"Summer '14","url":"/services/data/v31.0","version":"31.0"},{"label":"Winter '15","url":"/services/data/v32.0","version":"32.0"},{"label":"Spring '15","url":"/services/data/v33.0","version":"33.0"},{"label":"Summer '15","url":"/services/data/v34.0","version":"34.0"},{"label":"Winter '16","url":"/services/data/v35.0","version":"35.0"},{"label":"Spring '16","url":"/services/data/v36.0","version":"36.0"},{"label":"Summer '16","url":"/services/data/v37.0","version":"37.0"},{"label":"Winter '17","url":"/services/data/v38.0","version":"38.0"},{"label":"Spring '17","url":"/services/data/v39.0","version":"39.0"},{"label":"Summer '17","url":"/services/data/v40.0","version":"40.0"},{"label":"Winter '18","url":"/services/data/v41.0","version":"41.0"},{"label":"Spring ’18","url":"/services/data/v42.0","version":"42.0"},{"label":"Summer '18","url":"/services/data/v43.0","version":"43.0"},{"label":"Winter '19","url":"/services/data/v44.0","version":"44.0"},{"label":"Spring '19","url":"/services/data/v45.0","version":"45.0"},{"label":"Summer '19","url":"/services/data/v46.0","version":"46.0"},{"label":"Winter '20","url":"/services/data/v47.0","version":"47.0"},{"label":"Spring '20","url":"/services/data/v48.0","version":"48.0"},{"label":"Summer '20","url":"/services/data/v49.0","version":"49.0"},{"label":"Winter '21","url":"/services/data/v50.0","version":"50.0"},{"label":"Spring '21","url":"/services/data/v51.0","version":"51.0"},{"label":"Summer '21","url":"/services/data/v52.0","version":"52.0"},{"label":"Winter '22","url":"/services/data/v53.0","version":"53.0"},{"label":"Spring '22","url":"/services/data/v54.0","version":"54.0"},{"label":"Summer '22","url":"/services/data/v55.0","version":"55.0"},{"label":"Winter '23","url":"/services/data/v56.0","version":"56.0"},{"label":"Spring '23","url":"/services/data/v57.0","version":"57.0"},{"label":"Summer '23","url":"/services/data/v58.0","version":"58.0"},{"label":"Winter '24","url":"/services/data/v59.0","version":"59.0"}]

ラベルとバージョンの表をつくってみたいと思います。

var template = `
    <table bgcolor="#FFFFFF">
        <tr>
            <th>Label</th>
            <th>Version</th>
        </tr>

        {{#each response}}
            <tr>
                <td>{{label}}</td>
                <td>{{version}}</td>
            </tr>
        {{/each}}
    </table>
`;
// Set visualizer
pm.visualizer.set(template, {
    // Pass the response body parsed as JSON as `data`
    response: pm.response.json()
});

少し見やすくなった感じですね。

image.png

cssを使って少し整形します。

var template = `
<style>
  .myTable {
    background-color : #dddbda;
  }
  .myTable th{
    width: 150px;
  }
  .myTable td{
    width: 150px;
  }
  </style>
    <table class="myTable">
        <tr>
            <th>Label</th>
            <th>Version</th>
        </tr>

        {{#each response}}
            <tr>
                <td>{{label}}</td>
                <td>{{version}}</td>
            </tr>
        {{/each}}
    </table>
`;

// Set visualizer
pm.visualizer.set(template, {
    // Pass the response body parsed as JSON as `data`
    response: pm.response.json()
});

image.png

最新のバージョンが確認できるように降順で並べてみます。

この回答をみるとjsonの結果をソートできそうですね。

You can make use of lodash which is built right into Postman.

var _ = require('lodash')
var expectedSortedOrder = _.orderBy(pm.response.json(), ['version'],['desc']);
// Set visualizer
pm.visualizer.set(template, {
    // Pass the response body parsed as JSON as `data`
    response: expectedSortedOrder
});

おおお、素晴らしい降順になりました。

image.png

もう少し複雑なレスポンスの場合 Tooling API

{
    "size": 5,
    "totalSize": 5,
    "done": true,
    "queryLocator": null,
    "entityTypeName": "FormulaFunctionAllowedType",
    "records": [
        {
            "attributes": {
                "type": "FormulaFunctionAllowedType",
                "url": "/services/data/v59.0/tooling/sobjects/FormulaFunctionAllowedType/VALIDATION-SUBSTITUTE"
            },
            "Id": "000000000000000AAA",
            "DurableId": "VALIDATION-SUBSTITUTE",
            "FunctionId": "SUBSTITUTE",
            "Type": "VALIDATION"
        },
        {
            "attributes": {
                "type": "FormulaFunctionAllowedType",
                "url": "/services/data/v59.0/tooling/sobjects/FormulaFunctionAllowedType/VISUALFORCE-SUBSTITUTE"
            },
            "Id": "000000000000000AAA",
            "DurableId": "VISUALFORCE-SUBSTITUTE",
            "FunctionId": "SUBSTITUTE",
            "Type": "VISUALFORCE"
        },
        {
            "attributes": {
                "type": "FormulaFunctionAllowedType",
                "url": "/services/data/v59.0/tooling/sobjects/FormulaFunctionAllowedType/FLOW-SUBSTITUTE"
            },
            "Id": "000000000000000AAA",
            "DurableId": "FLOW-SUBSTITUTE",
            "FunctionId": "SUBSTITUTE",
            "Type": "FLOW"
        },
        {
            "attributes": {
                "type": "FormulaFunctionAllowedType",
                "url": "/services/data/v59.0/tooling/sobjects/FormulaFunctionAllowedType/CONVERSATION_MESSAGE-SUBSTITUTE"
            },
            "Id": "000000000000000AAA",
            "DurableId": "CONVERSATION_MESSAGE-SUBSTITUTE",
            "FunctionId": "SUBSTITUTE",
            "Type": "CONVERSATION_MESSAGE"
        },
        {
            "attributes": {
                "type": "FormulaFunctionAllowedType",
                "url": "/services/data/v59.0/tooling/sobjects/FormulaFunctionAllowedType/LOYALTYFORMULA-SUBSTITUTE"
            },
            "Id": "000000000000000AAA",
            "DurableId": "LOYALTYFORMULA-SUBSTITUTE",
            "FunctionId": "SUBSTITUTE",
            "Type": "LOYALTYFORMULA"
        }
    ]
}

今回は繰り返し部分のrecordsを使います。そのため一旦jsonを変数にして抽出します。

const response = JSON.parse(pm.response.text())

const response = JSON.parse(pm.response.text())
var _ = require('lodash')
var expectedSortedOrder = _.orderBy(response.records, ['Type'],['asc']);
// Set visualizer
pm.visualizer.set(template, {
    // Pass the response body parsed as JSON as `data`
    response: expectedSortedOrder
});

実行結果は見やすくなりました。

image.png

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?