Skip to content

Commit

Permalink
feat: support /documents/ endpoints
Browse files Browse the repository at this point in the history
- See Haidra-Org/AI-Horde#416 for more info.
  • Loading branch information
tazlin committed Jun 6, 2024
1 parent 816f897 commit 8b16596
Show file tree
Hide file tree
Showing 12 changed files with 251 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/api_to_sdk_map.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ This is a mapping of the AI-Horde API models (defined at [https://stablehorde.ne
## Payloads
| API Endpoint | HTTP Method | SDK Request Type |
| ------------ | ----------- | ---------------- |
| /v2/documents/privacy | GET | [AIHordeGetPrivacyPolicyRequest][horde_sdk.ai_horde_api.apimodels._documents.AIHordeGetPrivacyPolicyRequest] |
| /v2/documents/sponsors | GET | [AIHordeGetSponsorsRequest][horde_sdk.ai_horde_api.apimodels._documents.AIHordeGetSponsorsRequest] |
| /v2/documents/terms | GET | [AIHordeGetTermsRequest][horde_sdk.ai_horde_api.apimodels._documents.AIHordeGetTermsRequest] |
| /v2/find_user | GET | [FindUserRequest][horde_sdk.ai_horde_api.apimodels._find_user.FindUserRequest] |
| /v2/generate/async | POST | [ImageGenerateAsyncRequest][horde_sdk.ai_horde_api.apimodels.generate._async.ImageGenerateAsyncRequest] |
| /v2/generate/check/{id} | GET | [ImageGenerateCheckRequest][horde_sdk.ai_horde_api.apimodels.generate._check.ImageGenerateCheckRequest] |
Expand Down Expand Up @@ -43,6 +46,9 @@ This is a mapping of the AI-Horde API models (defined at [https://stablehorde.ne
## Responses
| API Endpoint | HTTP Status Code | SDK Response Type |
| ------------ | ----------- | ----------------- |
| /v2/documents/privacy | 200 | [HordeDocument][horde_sdk.ai_horde_api.apimodels._documents.HordeDocument] |
| /v2/documents/sponsors | 200 | [HordeDocument][horde_sdk.ai_horde_api.apimodels._documents.HordeDocument] |
| /v2/documents/terms | 200 | [HordeDocument][horde_sdk.ai_horde_api.apimodels._documents.HordeDocument] |
| /v2/find_user | 200 | [UserDetailsResponse][horde_sdk.ai_horde_api.apimodels._users.UserDetailsResponse] |
| /v2/generate/async | 200 | [ImageGenerateAsyncDryRunResponse][horde_sdk.ai_horde_api.apimodels.generate._async.ImageGenerateAsyncDryRunResponse] |
| /v2/generate/async | 202 | [ImageGenerateAsyncResponse][horde_sdk.ai_horde_api.apimodels.generate._async.ImageGenerateAsyncResponse] |
Expand Down
9 changes: 9 additions & 0 deletions docs/api_to_sdk_payload_map.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
{
"/v2/documents/privacy": {
"GET": "horde_sdk.ai_horde_api.apimodels._documents.AIHordeGetPrivacyPolicyRequest"
},
"/v2/documents/sponsors": {
"GET": "horde_sdk.ai_horde_api.apimodels._documents.AIHordeGetSponsorsRequest"
},
"/v2/documents/terms": {
"GET": "horde_sdk.ai_horde_api.apimodels._documents.AIHordeGetTermsRequest"
},
"/v2/status/heartbeat": {
"GET": "horde_sdk.ai_horde_api.apimodels._status.AIHordeHeartbeatRequest"
},
Expand Down
9 changes: 9 additions & 0 deletions docs/api_to_sdk_response_map.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
{
"/v2/documents/privacy": {
"200": "horde_sdk.ai_horde_api.apimodels._documents.HordeDocument"
},
"/v2/documents/sponsors": {
"200": "horde_sdk.ai_horde_api.apimodels._documents.HordeDocument"
},
"/v2/documents/terms": {
"200": "horde_sdk.ai_horde_api.apimodels._documents.HordeDocument"
},
"/v2/status/heartbeat": {
"200": "horde_sdk.ai_horde_api.apimodels._status.AIHordeHeartbeatResponse"
},
Expand Down
2 changes: 2 additions & 0 deletions docs/horde_sdk/ai_horde_api/apimodels/_documents.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# _documents
::: horde_sdk.ai_horde_api.apimodels._documents
63 changes: 63 additions & 0 deletions docs/request_field_names_and_descriptions.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,67 @@
{
"AIHordeGetPrivacyPolicyRequest": {
"format": {
"description": "The format of the document to return. Default is markdown.",
"types": [
"horde_sdk.ai_horde_api.apimodels._documents.DocumentFormat",
"str"
]
},
"accept": {
"description": "The 'accept' header field.",
"types": [
"horde_sdk.generic_api.metadata.GenericAcceptTypes"
]
},
"client_agent": {
"description": "The requesting client's agent. You should set this to reflect the name, version and contact information\nfor your client.",
"types": [
"str"
]
}
},
"AIHordeGetSponsorsRequest": {
"format": {
"description": "The format of the document to return. Default is markdown.",
"types": [
"horde_sdk.ai_horde_api.apimodels._documents.DocumentFormat",
"str"
]
},
"accept": {
"description": "The 'accept' header field.",
"types": [
"horde_sdk.generic_api.metadata.GenericAcceptTypes"
]
},
"client_agent": {
"description": "The requesting client's agent. You should set this to reflect the name, version and contact information\nfor your client.",
"types": [
"str"
]
}
},
"AIHordeGetTermsRequest": {
"format": {
"description": "The format of the document to return. Default is markdown.",
"types": [
"horde_sdk.ai_horde_api.apimodels._documents.DocumentFormat",
"str"
]
},
"accept": {
"description": "The 'accept' header field.",
"types": [
"horde_sdk.generic_api.metadata.GenericAcceptTypes"
]
},
"client_agent": {
"description": "The requesting client's agent. You should set this to reflect the name, version and contact information\nfor your client.",
"types": [
"str"
]
}
},
"AIHordeHeartbeatRequest": {
"accept": {
"description": "The 'accept' header field.",
Expand Down
16 changes: 16 additions & 0 deletions docs/response_field_names_and_descriptions.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
{
"HordeDocument": {
"html": {
"description": "The HTML content of the document, if requested.",
"types": [
"str",
"None"
]
},
"markdown": {
"description": "The markdown content of the document, if requested.",
"types": [
"str",
"None"
]
}
},
"AIHordeHeartbeatResponse": {
"message": {
"description": "A message from the API. This is typically an error or warning message, but may also be informational.",
Expand Down
14 changes: 14 additions & 0 deletions horde_sdk/ai_horde_api/apimodels/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
"""All requests, responses and API models defined for the AI Horde API."""

from horde_sdk.ai_horde_api.apimodels._documents import (
AIHordeDocumentRequestMixin,
AIHordeGetPrivacyPolicyRequest,
AIHordeGetSponsorsRequest,
AIHordeGetTermsRequest,
DocumentFormat,
HordeDocument,
)
from horde_sdk.ai_horde_api.apimodels._find_user import (
FindUserRequest,
)
Expand Down Expand Up @@ -167,6 +175,12 @@
)

__all__ = [
"AIHordeDocumentRequestMixin",
"AIHordeGetPrivacyPolicyRequest",
"AIHordeGetSponsorsRequest",
"AIHordeGetTermsRequest",
"DocumentFormat",
"HordeDocument",
"ContributionsDetails",
"FindUserRequest",
"KudosTransferRequest",
Expand Down
116 changes: 116 additions & 0 deletions horde_sdk/ai_horde_api/apimodels/_documents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
from loguru import logger
from pydantic import field_validator
from strenum import StrEnum
from typing_extensions import override

from horde_sdk.ai_horde_api.apimodels.base import BaseAIHordeRequest
from horde_sdk.ai_horde_api.endpoints import AI_HORDE_API_ENDPOINT_SUBPATH
from horde_sdk.consts import HTTPMethod
from horde_sdk.generic_api.apimodels import (
HordeAPIObject,
HordeResponseBaseModel,
)


class DocumentFormat(StrEnum):
html = "html"
markdown = "markdown"


class HordeDocument(HordeResponseBaseModel):
html: str | None = None
"""The HTML content of the document, if requested."""
markdown: str | None = None
"""The markdown content of the document, if requested."""

@override
@classmethod
def get_api_model_name(cls) -> str:
return "HordeDocument"


class AIHordeDocumentRequestMixin(HordeAPIObject):
format: DocumentFormat | str = DocumentFormat.html

"""The format of the document to return. Default is markdown."""

@field_validator("format")
def validate_format(cls, value: DocumentFormat | str) -> DocumentFormat | str:
if isinstance(value, DocumentFormat):
return value

try:
DocumentFormat(value)
except ValueError:
logger.warning(f"Unknown document format: {value}. Is your SDK out of date or did the API change?")

return value


class AIHordeGetPrivacyPolicyRequest(BaseAIHordeRequest, AIHordeDocumentRequestMixin):

@override
@classmethod
def get_api_model_name(cls) -> str | None:
return None

@override
@classmethod
def get_http_method(cls) -> HTTPMethod:
return HTTPMethod.GET

@override
@classmethod
def get_api_endpoint_subpath(cls) -> AI_HORDE_API_ENDPOINT_SUBPATH:
return AI_HORDE_API_ENDPOINT_SUBPATH.v2_documents_privacy

@override
@classmethod
def get_default_success_response_type(cls) -> type[HordeDocument]:
return HordeDocument


class AIHordeGetSponsorsRequest(BaseAIHordeRequest, AIHordeDocumentRequestMixin):

@override
@classmethod
def get_api_model_name(cls) -> str | None:
return None

@override
@classmethod
def get_http_method(cls) -> HTTPMethod:
return HTTPMethod.GET

@override
@classmethod
def get_api_endpoint_subpath(cls) -> AI_HORDE_API_ENDPOINT_SUBPATH:
return AI_HORDE_API_ENDPOINT_SUBPATH.v2_documents_sponsors

@override
@classmethod
def get_default_success_response_type(cls) -> type[HordeDocument]:
return HordeDocument


class AIHordeGetTermsRequest(BaseAIHordeRequest, AIHordeDocumentRequestMixin):

@override
@classmethod
def get_api_model_name(cls) -> str | None:
return None

@override
@classmethod
def get_http_method(cls) -> HTTPMethod:
return HTTPMethod.GET

@override
@classmethod
def get_api_endpoint_subpath(cls) -> AI_HORDE_API_ENDPOINT_SUBPATH:
return AI_HORDE_API_ENDPOINT_SUBPATH.vs_documents_terms

@override
@classmethod
def get_default_success_response_type(cls) -> type[HordeDocument]:
return HordeDocument
4 changes: 4 additions & 0 deletions horde_sdk/ai_horde_api/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ class AI_HORDE_API_ENDPOINT_SUBPATH(GENERIC_API_ENDPOINT_SUBPATH):
v2_operations_ipaddr = "/v2/operations/ipaddr"
v2_operations_ipaddr_single = "/v2/operations/ipaddr/{ipaddr}"

v2_documents_privacy = "/v2/documents/privacy"
v2_documents_sponsors = "/v2/documents/sponsors"
vs_documents_terms = "/v2/documents/terms"


def get_ai_horde_swagger_url() -> str:
"""Get the URL for the AI Horde API swagger docs."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"html": "",
"markdown": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"html": "",
"markdown": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"html": "",
"markdown": ""
}

0 comments on commit 8b16596

Please sign in to comment.