openapi: '3.0.3'

info:
  version: 1.0.0
  title: i3M Wallet API

paths:
  /resources:
    get:
      summary:
        Lists the resources that match the filter specified in the query parameters.
      operationId: resourceList
      x-eov-operation-handler: resources
      tags:
        - resources
      parameters:
        - in: query
          name: type
          example: Contract
          required: false
          schema:
            $ref:  "#/components/schemas/ResourceType"
          description: Filter the resources by resource type.
        - in: query
          name: identity
          example: did:ethr:i3m:0x031bee96cfae8bad99ea0dd3d08d1a3296084f894e9ddfe1ffe141133e81ac5863
          allowEmptyValue: true
          required: false
          schema:
            $ref: "../schema/identity.yaml#/components/schemas/did"
          description: 
            Filter the resource associated to an identity DID. Send empty value to get all the resources that are not associated to any identity.
        - in: query
          name: parentResource
          required: false
          schema:
            type: string
          description: 
            Get only resources with the given parent resource id.
      responses:
        "200":
          description: A paged array of resources. Only the props requested will be returned. Security policies may prevent some props from being returned.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ResourceListOutput"
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "../schema/error.yaml#/components/schemas/ApiError"

    post:
      summary: Create a resource
      operationId: resourceCreate
      x-eov-operation-handler: resources
      tags:
        - resources
      requestBody:
        description: Create a resource. Nowadays it only supports storage of verifiable credentials.
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/Resource"
      responses:
        "201":
          description: the ID and type of the created resource
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ResourceId"
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "../schema/error.yaml#/components/schemas/ApiError"

components:
  schemas:
    ResourceListOutput:
      title: ResourceListOutput
      description: A list of resources
      type: array
      items:
        $ref: "#/components/schemas/Resource"
        # $ref: "#/components/schemas/ResourceId"
        # allOf:
        #   - $ref: "#/components/schemas/ResourceId"
        #   - $ref: "#/components/schemas/Resource"

    Resource:
      title: Resource
      anyOf:
        - $ref: "#/components/schemas/VerifiableCredential"
        - $ref: "#/components/schemas/ObjectResource"
        - $ref: "#/components/schemas/KeyPair"
        - $ref: "#/components/schemas/Contract"
        - $ref: "#/components/schemas/NonRepudiationProof"
        - $ref: "#/components/schemas/DataExchangeResource"

    VerifiableCredential:
      title: VerifiableCredential
      type: object
      properties:
        type:
          $ref:  "#/components/schemas/ResourceType"
          example: VerifiableCredential
          enum:
            - VerifiableCredential
        name:
          type: string
          example: 'Resource name'
        resource:
          type: object
          properties:
            "@context":
              type: array
              items:
                type: string
              example: ['https://www.w3.org/2018/credentials/v1']
            id:
              type: string
              example: http://example.edu/credentials/1872
            type: 
              type: array
              items:
                type: string
              example: ['VerifiableCredential']
            issuer:
              type: object
              properties:
                id:
                  $ref: "../schema/identity.yaml#/components/schemas/did"
              additionalProperties: true
              required:
                - id
            issuanceDate:
              type: string
              format: date-time
              example: "2021-06-10T19:07:28.000Z"
            credentialSubject:
              type: object
              properties:
                id:
                  $ref: "../schema/identity.yaml#/components/schemas/did"
              required:
                - id
              additionalProperties: true
            proof:
              type: object
              properties:
                type:
                  type: string
                  enum:
                    - JwtProof2020
              required:
                - type
              additionalProperties: true
          additionalProperties: true
          required:
            - "@context"
            - type
            - issuer
            - issuanceDate
            - credentialSubject
            - proof
      required:
        - type
        - resource

    ObjectResource:
      title: ObjectResource
      type: object
      properties:
        type:
          $ref:  "#/components/schemas/ResourceType"
          example: Object
          enum:
            - Object
        name:
          type: string
          example: 'Resource name'
        parentResource:
          type: string
        identity:
          $ref: "../schema/identity.yaml#/components/schemas/did"
        resource:
          type: object
          additionalProperties: true
      required:
        - type
        - resource

    KeyPair:
      title: JWK pair
      type: object
      properties:
        type:
          $ref:  "#/components/schemas/ResourceType"
          example: KeyPair
          enum:
            - KeyPair
        name:
          type: string
          example: 'Resource name'
        identity:
          $ref: "../schema/identity.yaml#/components/schemas/did"
        resource:
          type: object
          properties:
            keyPair:
              $ref: "../schema/jwkPair.yaml#/components/schemas/JwkPair"
          required:
            - keyPair
      required:
        - type
        - resource

    Contract:
      title: Contract
      type: object
      properties:
        type:
          $ref:  "#/components/schemas/ResourceType"
          example: Contract
          enum:
            - Contract
        name:
          type: string
          example: 'Resource name'
        identity:
          $ref: "../schema/identity.yaml#/components/schemas/did"
        resource:
          type: object
          properties:
            dataSharingAgreement:
              $ref: "../schema/dataSharingAgreement.yaml#/components/schemas/DataSharingAgreement"
            keyPair:
              $ref: "../schema/jwkPair.yaml#/components/schemas/JwkPair"
          required:
            - dataSharingAgreement
      required:
        - type
        - resource

    DataExchangeResource:
      title: DataExchangeResource
      type: object
      properties:
        type:
          $ref:  "#/components/schemas/ResourceType"
          example: DataExchange
          enum:
            - DataExchange
        name:
          type: string
          example: 'Resource name'
        resource:
          $ref: '../schema/dataExchange.yaml#/components/schemas/DataExchange'
      required:
        - type
        - resource
      
    NonRepudiationProof:
      title: NonRepudiationProof
      type: object
      properties:
        type:
          $ref:  "#/components/schemas/ResourceType"
          example: NonRepudiationProof
          enum:
            - NonRepudiationProof
        name:
          type: string
          example: 'Resource name'
        resource:
          $ref: "../schema/jws.yaml#/components/schemas/CompactJWS"
          description: a non-repudiation proof (either a PoO, a PoR or a PoP) as a compact JWS
      required:
        - type
        - resource

    ResourceId:
      type: object
      properties:
        id:
          type: string
      required:
        - id

    ResourceType:
      type: string
      enum:
        - VerifiableCredential
        - Object
        - KeyPair
        - Contract
        - DataExchange
        - NonRepudiationProof
