{
 "openapi": "3.0.3",
 "info": {
  "title": "Phylax API",
  "version": "1",
  "description": "Find shots and bullseyes in a photo of a shot target. See /docs."
 },
 "components": {
  "securitySchemes": {
   "bearer": {
    "type": "http",
    "scheme": "bearer"
   }
  },
  "schemas": {
   "Error": {
    "type": "object",
    "properties": {
     "error": {
      "type": "object",
      "properties": {
       "code": {
        "type": "string"
       },
       "message": {
        "type": "string"
       }
      }
     },
     "request_id": {
      "type": "string"
     }
    }
   },
   "Shot": {
    "type": "object",
    "properties": {
     "x": {
      "type": "number"
     },
     "y": {
      "type": "number"
     },
     "group": {
      "type": "integer",
      "nullable": true
     },
     "flag": {
      "type": "string",
      "enum": [
       "",
       "unsure",
       "added_for_count"
      ]
     },
     "confidence": {
      "type": "string",
      "enum": [
       "high",
       "medium",
       "low"
      ]
     }
    }
   },
   "Bullseye": {
    "type": "object",
    "properties": {
     "id": {
      "type": "integer"
     },
     "x": {
      "type": "number"
     },
     "y": {
      "type": "number"
     },
     "kind": {
      "type": "string",
      "enum": [
       "dot",
       "crosshair"
      ]
     },
     "r": {
      "type": "number"
     }
    }
   },
   "Analysis": {
    "type": "object",
    "properties": {
     "api_version": {
      "type": "string"
     },
     "request_id": {
      "type": "string"
     },
     "model": {
      "type": "string",
      "enum": [
       "medium"
      ]
     },
     "width": {
      "type": "integer"
     },
     "height": {
      "type": "integer"
     },
     "hole_width_px": {
      "type": "number"
     },
     "shots": {
      "type": "array",
      "items": {
       "$ref": "#/components/schemas/Shot"
      }
     },
     "bullseyes": {
      "type": "array",
      "items": {
       "$ref": "#/components/schemas/Bullseye"
      }
     },
     "groups": {
      "type": "array",
      "items": {
       "type": "object",
       "properties": {
        "id": {
         "type": "integer"
        },
        "shots": {
         "type": "integer"
        }
       }
      }
     },
     "outline": {
      "type": "array",
      "items": {
       "type": "number"
      },
      "nullable": true
     },
     "low_resolution": {
      "type": "boolean"
     },
     "quota": {
      "type": "object",
      "properties": {
       "month": {
        "type": "string"
       },
       "used": {
        "type": "integer"
       },
       "limit": {
        "type": "integer"
       }
      }
     }
    }
   },
   "Photo": {
    "type": "object",
    "properties": {
     "photo_id": {
      "type": "string"
     },
     "ref": {
      "type": "string"
     },
     "status": {
      "type": "string",
      "enum": [
       "queued",
       "processing",
       "done",
       "failed"
      ]
     },
     "result": {
      "allOf": [
       {
        "$ref": "#/components/schemas/Analysis"
       }
      ],
      "nullable": true
     },
     "error": {
      "type": "object",
      "nullable": true,
      "properties": {
       "code": {
        "type": "string"
       },
       "message": {
        "type": "string"
       }
      }
     },
     "duplicate": {
      "type": "boolean"
     }
    }
   },
   "Batch": {
    "type": "object",
    "properties": {
     "batch_id": {
      "type": "string"
     },
     "model": {
      "type": "string"
     },
     "shots_per_bullseye": {
      "type": "integer",
      "nullable": true
     },
     "created": {
      "type": "string",
      "format": "date-time"
     },
     "expires": {
      "type": "string",
      "format": "date-time"
     },
     "photo_count": {
      "type": "integer"
     },
     "counts": {
      "type": "object",
      "properties": {
       "queued": {
        "type": "integer"
       },
       "processing": {
        "type": "integer"
       },
       "done": {
        "type": "integer"
       },
       "failed": {
        "type": "integer"
       }
      }
     },
     "complete": {
      "type": "boolean"
     },
     "photos": {
      "type": "array",
      "items": {
       "$ref": "#/components/schemas/Photo"
      }
     }
    }
   }
  }
 },
 "security": [
  {
   "bearer": []
  }
 ],
 "paths": {
  "/v1/analyze": {
   "post": {
    "summary": "Analyse a target photo",
    "parameters": [
     {
      "name": "model",
      "in": "query",
      "schema": {
       "type": "string",
       "enum": [
        "medium"
       ],
       "default": "medium"
      }
     },
     {
      "name": "shots_per_bullseye",
      "in": "query",
      "schema": {
       "type": "integer",
       "minimum": 1,
       "maximum": 50
      }
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "image/jpeg": {
       "schema": {
        "type": "string",
        "format": "binary"
       }
      },
      "image/png": {
       "schema": {
        "type": "string",
        "format": "binary"
       }
      },
      "multipart/form-data": {
       "schema": {
        "type": "object",
        "properties": {
         "photo": {
          "type": "string",
          "format": "binary"
         }
        }
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "Result",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Analysis"
        }
       }
      }
     },
     "400": {
      "description": "Bad request",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "401": {
      "description": "Invalid key",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "413": {
      "description": "Photo too large"
     },
     "422": {
      "description": "Unreadable photo"
     },
     "429": {
      "description": "Rate limit or quota"
     },
     "503": {
      "description": "Busy; retry"
     }
    }
   }
  },
  "/v1/usage": {
   "get": {
    "summary": "This month's usage for the key",
    "responses": {
     "200": {
      "description": "Usage"
     }
    }
   }
  },
  "/v1/batches": {
   "post": {
    "summary": "Create a batch",
    "parameters": [
     {
      "name": "model",
      "in": "query",
      "schema": {
       "type": "string",
       "enum": [
        "medium"
       ],
       "default": "medium"
      }
     },
     {
      "name": "shots_per_bullseye",
      "in": "query",
      "schema": {
       "type": "integer",
       "minimum": 1,
       "maximum": 50
      }
     }
    ],
    "responses": {
     "201": {
      "description": "Batch created",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Batch"
        }
       }
      }
     },
     "400": {
      "description": "Bad request",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "401": {
      "description": "Invalid key",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "429": {
      "description": "Too many open batches",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     }
    }
   }
  },
  "/v1/batches/{batch_id}": {
   "get": {
    "summary": "Batch status and results",
    "parameters": [
     {
      "name": "batch_id",
      "in": "path",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "Batch",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Batch"
        }
       }
      }
     },
     "404": {
      "description": "Not found or expired",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     }
    }
   }
  },
  "/v1/batches/{batch_id}/photos": {
   "post": {
    "summary": "Add a photo to a batch (resumable by ref)",
    "parameters": [
     {
      "name": "batch_id",
      "in": "path",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "name": "ref",
      "in": "query",
      "required": true,
      "schema": {
       "type": "string",
       "pattern": "^[A-Za-z0-9._-]{1,64}$"
      }
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "image/jpeg": {
       "schema": {
        "type": "string",
        "format": "binary"
       }
      },
      "image/png": {
       "schema": {
        "type": "string",
        "format": "binary"
       }
      },
      "multipart/form-data": {
       "schema": {
        "type": "object",
        "properties": {
         "photo": {
          "type": "string",
          "format": "binary"
         }
        }
       }
      }
     }
    },
    "responses": {
     "202": {
      "description": "Queued",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Photo"
        }
       }
      }
     },
     "200": {
      "description": "Already received (duplicate ref)",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Photo"
        }
       }
      }
     },
     "400": {
      "description": "Bad request",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "404": {
      "description": "Batch not found",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "409": {
      "description": "Batch full",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "413": {
      "description": "Photo too large"
     },
     "422": {
      "description": "Not a JPEG or PNG"
     },
     "429": {
      "description": "Rate limit, queue full or quota",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     }
    }
   }
  },
  "/v1/batches/{batch_id}/photos/{photo_id}": {
   "get": {
    "summary": "One photo of a batch",
    "parameters": [
     {
      "name": "batch_id",
      "in": "path",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "name": "photo_id",
      "in": "path",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "Photo",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Photo"
        }
       }
      }
     },
     "404": {
      "description": "Not found",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     }
    }
   }
  }
 }
}