category: Tutorial
name: SimpleTransaction
uri: /simpleTransactions
method: POST
commands:
  - command: script
    kwargs:
      code: |-
        r = await callout(path="/apis/systemAs",
                          method="POST",
                          body=dict(SystemA=dict(name=context.request.body.systemA.name,
                                                 description=context.request.body.systemA.description)))
        if r.error:
            raise Error(r.code, reason=r.error.__str__())
        a = json.loads(r.body)
      cancellation:
        cancellable: true
        actions:
          - action_type: script
            code: |-
              if not a:
                  r = await callout(path="/apis/systemAs?name={}".format(context.request.body.systemA.name))
                  if r.error:
                      await context.qmonus.abort()
                      return
                  a = json.loads(r.body)

              r = await callout(path="/apis/systemAs/{}".format(a["SystemA"]["instance"]), method="DELETE")
              if r.error:
                  await context.qmonus.abort()
    label: SystemA
  - command: script
    kwargs:
      code: |-
        r = await callout(path="/apis/systemBs",
                          method="POST",
                          body=dict(SystemB=dict(name=context.request.body.systemB.name,
                                                 description=context.request.body.systemB.description)))
        if r.error:
            raise Error(r.code, reason=r.error.__str__())
        b = json.loads(r.body)
      cancellation:
        cancellable: true
        actions:
          - action_type: script
            code: |-
              if not b:
                  r = await callout(path="/apis/systemBs?name={}".format(context.request.body.systemB.name))
                  if r.error:
                      await context.qmonus.abort()
                      return
                  b = json.loads(r.body)

              r = await callout(path="/apis/systemBs/{}".format(b["SystemB"]["instance"]), method="DELETE")
              if r.error:
                  await context.qmonus.abort()
    label: SystemB
  - command: breakpoint
    kwargs:
      abort: true
      immediate_cancel: true
request_timeout: 60
connect_timeout: 60
spec:
  response:
    normal:
      codes:
        - 200
  request:
    body:
      type: object
      required:
        - systemA
        - systemB
      properties:
        systemA:
          type: object
          properties:
            name:
              type: string
            description:
              type: string
          required:
            - name
        systemB:
          type: object
          properties:
            name:
              type: string
            description:
              type: string
          required:
            - name
transaction:
  enable: true
  async: true
  xname_use_counter: false
  auto_begin: true
  auto_response: true
  xname: ''
  lock:
    lock_keys:
      - context.request.body.systemA.name
      - context.request.body.systemB.name
    retry_count: 3
    retry_interval: 1
  callback_options:
    url: ''
  xdomain: Tutorial
  xtype: Two-Phase Commit
  auto_rollback: true
routing_auto_generation_mode: true
global_variables:
  a:
    initial: null
    description: SystemA Instance variable
  b:
    initial: null
    description: SystemB Instance variable