{
  "info": {
    "name": "Oomplo API",
    "description": "Generating PDF and DOCX from published Oomplo templates (docs/customer/api.md). Set baseUrl, apiKey and template (the id or the API name of a published template) as collection variables.",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "variable": [
    {
      "key": "baseUrl",
      "value": "http://127.0.0.1:3001"
    },
    {
      "key": "apiKey",
      "value": "oomplo_…"
    },
    {
      "key": "template",
      "value": "faktura"
    }
  ],
  "item": [
    {
      "name": "Template schema",
      "request": {
        "method": "GET",
        "header": [
          {
            "key": "Authorization",
            "value": "Bearer {{apiKey}}"
          }
        ],
        "url": {
          "raw": "{{baseUrl}}/api/templates/{{template}}/schema",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "templates",
            "{{template}}",
            "schema"
          ]
        }
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test('200', () => pm.response.to.have.status(200));",
              "const schema = pm.response.json();",
              "pm.test('fields, example and JSON Schema', () => { pm.expect(schema.fields).to.be.an('array'); pm.expect(schema.example).to.be.an('object'); pm.expect(schema.jsonSchema.$schema).to.include('2020-12'); });",
              "pm.collectionVariables.set('example', JSON.stringify(schema.example));",
              "pm.collectionVariables.set('row', JSON.stringify((schema.example.json || {}).dane1 || {}));"
            ]
          }
        }
      ]
    },
    {
      "name": "Validate data",
      "request": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Authorization",
            "value": "Bearer {{apiKey}}"
          }
        ],
        "url": {
          "raw": "{{baseUrl}}/api/templates/{{template}}/validate",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "templates",
            "{{template}}",
            "validate"
          ]
        },
        "body": {
          "mode": "raw",
          "raw": "{ \"data\": {{example}} }",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        }
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test('200', () => pm.response.to.have.status(200));",
              "pm.test('the example is valid', () => pm.expect(pm.response.json().valid).to.equal(true));"
            ]
          }
        }
      ]
    },
    {
      "name": "Render PDF",
      "request": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Authorization",
            "value": "Bearer {{apiKey}}"
          }
        ],
        "url": {
          "raw": "{{baseUrl}}/api/templates/{{template}}/render",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "templates",
            "{{template}}",
            "render"
          ]
        },
        "body": {
          "mode": "raw",
          "raw": "{ \"data\": {{example}} }",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        }
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test('200 PDF', () => { pm.response.to.have.status(200); pm.expect(pm.response.headers.get('Content-Type')).to.equal('application/pdf'); });",
              "pm.test('a PDF file', () => pm.expect(pm.response.stream.slice(0, 5).toString()).to.equal('%PDF-'));",
              "pm.test('the version rendered', () => pm.expect(Number(pm.response.headers.get('X-Oomplo-Template-Version'))).to.be.above(0));"
            ]
          }
        }
      ]
    },
    {
      "name": "Render as JSON (base64)",
      "request": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Authorization",
            "value": "Bearer {{apiKey}}"
          }
        ],
        "url": {
          "raw": "{{baseUrl}}/api/templates/{{template}}/render",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "templates",
            "{{template}}",
            "render"
          ]
        },
        "body": {
          "mode": "raw",
          "raw": "{ \"data\": {{example}}, \"response\": \"json\" }",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        }
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test('200', () => pm.response.to.have.status(200));",
              "const answer = pm.response.json();",
              "pm.test('file in base64', () => { pm.expect(answer.contentType).to.equal('application/pdf'); pm.expect(atob(answer.file).slice(0, 5)).to.equal('%PDF-'); pm.expect(answer.pages).to.be.above(0); });"
            ]
          }
        }
      ]
    },
    {
      "name": "Render DOCX with kept fields",
      "request": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Authorization",
            "value": "Bearer {{apiKey}}"
          }
        ],
        "url": {
          "raw": "{{baseUrl}}/api/templates/{{template}}/render",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "templates",
            "{{template}}",
            "render"
          ]
        },
        "body": {
          "mode": "raw",
          "raw": "{ \"data\": {{example}}, \"format\": \"docx\", \"fields\": \"keep\" }",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        }
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test('200 DOCX', () => { pm.response.to.have.status(200); pm.expect(pm.response.headers.get('Content-Type')).to.include('wordprocessingml'); });",
              "pm.test('a ZIP package', () => pm.expect(pm.response.stream.slice(0, 2).toString()).to.equal('PK'));"
            ]
          }
        }
      ]
    },
    {
      "name": "Missing data is named",
      "request": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Authorization",
            "value": "Bearer {{apiKey}}"
          }
        ],
        "url": {
          "raw": "{{baseUrl}}/api/templates/{{template}}/render",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "templates",
            "{{template}}",
            "render"
          ]
        },
        "body": {
          "mode": "raw",
          "raw": "{\n  \"data\": {}\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        }
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test('422 with the missing values', () => { pm.response.to.have.status(422); pm.expect(pm.response.json().error).to.include('No data'); });"
            ]
          }
        }
      ]
    },
    {
      "name": "Series job from rows",
      "request": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Authorization",
            "value": "Bearer {{apiKey}}"
          }
        ],
        "url": {
          "raw": "{{baseUrl}}/api/templates/{{template}}/jobs",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "templates",
            "{{template}}",
            "jobs"
          ]
        },
        "body": {
          "mode": "raw",
          "raw": "{ \"series\": { \"name\": \"dane1\", \"rows\": [ {{row}}, {{row}} ] }, \"zip\": true }",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        }
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test('202', () => pm.response.to.have.status(202));",
              "pm.collectionVariables.set('job', pm.response.json().id);"
            ]
          }
        }
      ]
    },
    {
      "name": "Job state",
      "request": {
        "method": "GET",
        "header": [
          {
            "key": "Authorization",
            "value": "Bearer {{apiKey}}"
          }
        ],
        "url": {
          "raw": "{{baseUrl}}/api/jobs/{{job}}",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "jobs",
            "{{job}}"
          ]
        }
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test('200', () => pm.response.to.have.status(200));",
              "const job = pm.response.json();",
              "if (job.state === 'running') { setTimeout(() => {}, 500); pm.execution.setNextRequest('Job state'); }",
              "else pm.test('done', () => pm.expect(job.state).to.equal('done'));"
            ]
          }
        }
      ]
    },
    {
      "name": "Job result (ZIP)",
      "request": {
        "method": "GET",
        "header": [
          {
            "key": "Authorization",
            "value": "Bearer {{apiKey}}"
          }
        ],
        "url": {
          "raw": "{{baseUrl}}/api/jobs/{{job}}/result",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "jobs",
            "{{job}}",
            "result"
          ]
        }
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test('200 ZIP', () => { pm.response.to.have.status(200); pm.expect(pm.response.stream.slice(0, 2).toString()).to.equal('PK'); });"
            ]
          }
        }
      ]
    },
    {
      "name": "History",
      "request": {
        "method": "GET",
        "header": [
          {
            "key": "Authorization",
            "value": "Bearer {{apiKey}}"
          }
        ],
        "url": {
          "raw": "{{baseUrl}}/api/history?limit=5",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "history?limit=5"
          ]
        }
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test('200 with entries', () => { pm.response.to.have.status(200); pm.expect(pm.response.json().entries).to.be.an('array'); });"
            ]
          }
        }
      ]
    }
  ]
}
