Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Добавил X-Headers #57

Merged
merged 2 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "gigachat"
version = "0.1.37post1"
version = "0.1.38"
description = "GigaChat. Python-library for GigaChain and LangChain"
authors = ["Konstantin Krestnikov <[email protected]>", "Sergey Malyshev <[email protected]>"]
license = "MIT"
Expand Down
17 changes: 3 additions & 14 deletions src/gigachat/api/assistants/get_assistants.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from http import HTTPStatus
from typing import Any, Dict, Optional

import httpx

from gigachat.api.utils import build_headers
from gigachat.exceptions import AuthenticationError, ResponseError
from gigachat.api.utils import build_headers, build_response
from gigachat.models.assistants import Assistants


Expand All @@ -24,15 +22,6 @@ def _get_kwargs(
return params


def _build_response(response: httpx.Response) -> Assistants:
if response.status_code == HTTPStatus.OK:
return Assistants(**response.json())
elif response.status_code == HTTPStatus.UNAUTHORIZED:
raise AuthenticationError(response.url, response.status_code, response.content, response.headers)
else:
raise ResponseError(response.url, response.status_code, response.content, response.headers)


def sync(
client: httpx.Client,
*,
Expand All @@ -42,7 +31,7 @@ def sync(
"""Возвращает массив объектов с данными доступных ассистентов"""
kwargs = _get_kwargs(assistant_id=assistant_id, access_token=access_token)
response = client.request(**kwargs)
return _build_response(response)
return build_response(response, Assistants)


async def asyncio(
Expand All @@ -54,4 +43,4 @@ async def asyncio(
"""Возвращает массив объектов с данными доступных ассистентов"""
kwargs = _get_kwargs(assistant_id=assistant_id, access_token=access_token)
response = await client.request(**kwargs)
return _build_response(response)
return build_response(response, Assistants)
17 changes: 3 additions & 14 deletions src/gigachat/api/assistants/post_assistant_delete.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from http import HTTPStatus
from typing import Any, Dict, Optional

import httpx

from gigachat.api.utils import build_headers
from gigachat.exceptions import AuthenticationError, ResponseError
from gigachat.api.utils import build_headers, build_response
from gigachat.models.assistants import AssistantDelete


Expand All @@ -25,15 +23,6 @@ def _get_kwargs(
}


def _build_response(response: httpx.Response) -> AssistantDelete:
if response.status_code == HTTPStatus.OK:
return AssistantDelete(**response.json())
elif response.status_code == HTTPStatus.UNAUTHORIZED:
raise AuthenticationError(response.url, response.status_code, response.content, response.headers)
else:
raise ResponseError(response.url, response.status_code, response.content, response.headers)


def sync(
client: httpx.Client,
*,
Expand All @@ -42,7 +31,7 @@ def sync(
) -> AssistantDelete:
kwargs = _get_kwargs(assistant_id=assistant_id, access_token=access_token)
response = client.request(**kwargs)
return _build_response(response)
return build_response(response, AssistantDelete)


async def asyncio(
Expand All @@ -53,4 +42,4 @@ async def asyncio(
) -> AssistantDelete:
kwargs = _get_kwargs(assistant_id=assistant_id, access_token=access_token)
response = await client.request(**kwargs)
return _build_response(response)
return build_response(response, AssistantDelete)
17 changes: 3 additions & 14 deletions src/gigachat/api/assistants/post_assistant_files_delete.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from http import HTTPStatus
from typing import Any, Dict, Optional

import httpx

from gigachat.api.utils import build_headers
from gigachat.exceptions import AuthenticationError, ResponseError
from gigachat.api.utils import build_headers, build_response
from gigachat.models.assistants import AssistantFileDelete


Expand All @@ -27,15 +25,6 @@ def _get_kwargs(
}


def _build_response(response: httpx.Response) -> AssistantFileDelete:
if response.status_code == HTTPStatus.OK:
return AssistantFileDelete(**response.json())
elif response.status_code == HTTPStatus.UNAUTHORIZED:
raise AuthenticationError(response.url, response.status_code, response.content, response.headers)
else:
raise ResponseError(response.url, response.status_code, response.content, response.headers)


def sync(
client: httpx.Client,
*,
Expand All @@ -45,7 +34,7 @@ def sync(
) -> AssistantFileDelete:
kwargs = _get_kwargs(assistant_id=assistant_id, file_id=file_id, access_token=access_token)
response = client.request(**kwargs)
return _build_response(response)
return build_response(response, AssistantFileDelete)


async def asyncio(
Expand All @@ -57,4 +46,4 @@ async def asyncio(
) -> AssistantFileDelete:
kwargs = _get_kwargs(assistant_id=assistant_id, file_id=file_id, access_token=access_token)
response = await client.request(**kwargs)
return _build_response(response)
return build_response(response, AssistantFileDelete)
17 changes: 3 additions & 14 deletions src/gigachat/api/assistants/post_assistant_modify.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from http import HTTPStatus
from typing import Any, Dict, List, Optional

import httpx

from gigachat.api.utils import build_headers
from gigachat.exceptions import AuthenticationError, ResponseError
from gigachat.api.utils import build_headers, build_response
from gigachat.models import Function
from gigachat.models.assistants import Assistant

Expand Down Expand Up @@ -41,15 +39,6 @@ def _get_kwargs(
}


def _build_response(response: httpx.Response) -> Assistant:
if response.status_code == HTTPStatus.OK:
return Assistant(**response.json())
elif response.status_code == HTTPStatus.UNAUTHORIZED:
raise AuthenticationError(response.url, response.status_code, response.content, response.headers)
else:
raise ResponseError(response.url, response.status_code, response.content, response.headers)


def sync(
client: httpx.Client,
*,
Expand All @@ -73,7 +62,7 @@ def sync(
access_token=access_token,
)
response = client.request(**kwargs)
return _build_response(response)
return build_response(response, Assistant)


async def asyncio(
Expand All @@ -99,4 +88,4 @@ async def asyncio(
access_token=access_token,
)
response = await client.request(**kwargs)
return _build_response(response)
return build_response(response, Assistant)
17 changes: 3 additions & 14 deletions src/gigachat/api/assistants/post_assistants.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from http import HTTPStatus
from typing import Any, Dict, List, Optional

import httpx

from gigachat.api.utils import build_headers
from gigachat.exceptions import AuthenticationError, ResponseError
from gigachat.api.utils import build_headers, build_response
from gigachat.models import Function
from gigachat.models.assistants import CreateAssistant

Expand Down Expand Up @@ -40,15 +38,6 @@ def _get_kwargs(
}


def _build_response(response: httpx.Response) -> CreateAssistant:
if response.status_code == HTTPStatus.OK:
return CreateAssistant(**response.json())
elif response.status_code == HTTPStatus.UNAUTHORIZED:
raise AuthenticationError(response.url, response.status_code, response.content, response.headers)
else:
raise ResponseError(response.url, response.status_code, response.content, response.headers)


def sync(
client: httpx.Client,
*,
Expand All @@ -73,7 +62,7 @@ def sync(
access_token=access_token,
)
response = client.request(**kwargs)
return _build_response(response)
return build_response(response, CreateAssistant)


async def asyncio(
Expand All @@ -100,4 +89,4 @@ async def asyncio(
access_token=access_token,
)
response = await client.request(**kwargs)
return _build_response(response)
return build_response(response, CreateAssistant)
17 changes: 3 additions & 14 deletions src/gigachat/api/get_balance.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from http import HTTPStatus
from typing import Any, Dict, Optional

import httpx

from gigachat.api.utils import build_headers
from gigachat.exceptions import AuthenticationError, ResponseError
from gigachat.api.utils import build_headers, build_response
from gigachat.models.balance import Balance


Expand All @@ -21,15 +19,6 @@ def _get_kwargs(
}


def _build_response(response: httpx.Response) -> Balance:
if response.status_code == HTTPStatus.OK:
return Balance(**response.json())
elif response.status_code == HTTPStatus.UNAUTHORIZED:
raise AuthenticationError(response.url, response.status_code, response.content, response.headers)
else:
raise ResponseError(response.url, response.status_code, response.content, response.headers)


def sync(
client: httpx.Client,
*,
Expand All @@ -39,7 +28,7 @@ def sync(
Только для клиентов с предоплатой иначе http 403"""
kwargs = _get_kwargs(access_token=access_token)
response = client.request(**kwargs)
return _build_response(response)
return build_response(response, Balance)


async def asyncio(
Expand All @@ -51,4 +40,4 @@ async def asyncio(
Только для клиентов с предоплатой иначе http 403"""
kwargs = _get_kwargs(access_token=access_token)
response = await client.request(**kwargs)
return _build_response(response)
return build_response(response, Balance)
5 changes: 3 additions & 2 deletions src/gigachat/api/get_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import httpx

from gigachat.api.utils import build_headers
from gigachat.api.utils import build_headers, build_x_headers
from gigachat.exceptions import AuthenticationError, ResponseError
from gigachat.models import Image

Expand All @@ -25,7 +25,8 @@ def _get_kwargs(

def _build_response(response: httpx.Response) -> Image:
if response.status_code == HTTPStatus.OK:
return Image(content=base64.b64encode(response.content).decode())
x_headers = build_x_headers(response)
return Image(x_headers=x_headers, content=base64.b64encode(response.content).decode())
elif response.status_code == HTTPStatus.UNAUTHORIZED:
raise AuthenticationError(response.url, response.status_code, response.content, response.headers)
else:
Expand Down
17 changes: 3 additions & 14 deletions src/gigachat/api/get_model.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from http import HTTPStatus
from typing import Any, Dict, Optional

import httpx

from gigachat.api.utils import build_headers
from gigachat.exceptions import AuthenticationError, ResponseError
from gigachat.api.utils import build_headers, build_response
from gigachat.models import Model


Expand All @@ -22,15 +20,6 @@ def _get_kwargs(
}


def _build_response(response: httpx.Response) -> Model:
if response.status_code == HTTPStatus.OK:
return Model(**response.json())
elif response.status_code == HTTPStatus.UNAUTHORIZED:
raise AuthenticationError(response.url, response.status_code, response.content, response.headers)
else:
raise ResponseError(response.url, response.status_code, response.content, response.headers)


def sync(
client: httpx.Client,
*,
Expand All @@ -40,7 +29,7 @@ def sync(
"""Возвращает объект с описанием указанной модели"""
kwargs = _get_kwargs(model=model, access_token=access_token)
response = client.request(**kwargs)
return _build_response(response)
return build_response(response, Model)


async def asyncio(
Expand All @@ -52,4 +41,4 @@ async def asyncio(
"""Возвращает объект с описанием указанной модели"""
kwargs = _get_kwargs(model=model, access_token=access_token)
response = await client.request(**kwargs)
return _build_response(response)
return build_response(response, Model)
17 changes: 3 additions & 14 deletions src/gigachat/api/get_models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from http import HTTPStatus
from typing import Any, Dict, Optional

import httpx

from gigachat.api.utils import build_headers
from gigachat.exceptions import AuthenticationError, ResponseError
from gigachat.api.utils import build_headers, build_response
from gigachat.models import Models


Expand All @@ -21,15 +19,6 @@ def _get_kwargs(
}


def _build_response(response: httpx.Response) -> Models:
if response.status_code == HTTPStatus.OK:
return Models(**response.json())
elif response.status_code == HTTPStatus.UNAUTHORIZED:
raise AuthenticationError(response.url, response.status_code, response.content, response.headers)
else:
raise ResponseError(response.url, response.status_code, response.content, response.headers)


def sync(
client: httpx.Client,
*,
Expand All @@ -38,7 +27,7 @@ def sync(
"""Возвращает массив объектов с данными доступных моделей"""
kwargs = _get_kwargs(access_token=access_token)
response = client.request(**kwargs)
return _build_response(response)
return build_response(response, Models)


async def asyncio(
Expand All @@ -49,4 +38,4 @@ async def asyncio(
"""Возвращает массив объектов с данными доступных моделей"""
kwargs = _get_kwargs(access_token=access_token)
response = await client.request(**kwargs)
return _build_response(response)
return build_response(response, Models)
Loading