Skip to content

Commit

Permalink
new: serialize to json inside pydantic
Browse files Browse the repository at this point in the history
  • Loading branch information
joein authored and generall committed Jul 21, 2023
1 parent aacd291 commit fc4fc2b
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 67 deletions.
12 changes: 7 additions & 5 deletions qdrant_client/http/api/cluster_api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# flake8: noqa E501
from typing import TYPE_CHECKING, Any, Dict, Set, Union

from qdrant_client._pydantic_compat import to_dict
from qdrant_client._pydantic_compat import to_json
from qdrant_client.http.models import *
from qdrant_client.http.models import models as m

Expand All @@ -18,8 +18,8 @@ def jsonable_encoder(
skip_defaults: bool = None,
exclude_unset: bool = False,
):
if hasattr(obj, "dict") or hasattr(obj, "model_dump"):
return to_dict(
if hasattr(obj, "json") or hasattr(obj, "model_dump_json"):
return to_json(
obj,
include=include,
exclude=exclude,
Expand Down Expand Up @@ -115,15 +115,17 @@ def _build_for_update_collection_cluster(
if timeout is not None:
query_params["timeout"] = str(timeout)

headers = {}
body = jsonable_encoder(cluster_operations)

if "Content-Type" not in headers:
headers["Content-Type"] = "application/json"
return self.api_client.request(
type_=m.InlineResponse2003,
method="POST",
url="/collections/{collection_name}/cluster",
path_params=path_params,
params=query_params,
json=body,
data=body,
)


Expand Down
42 changes: 27 additions & 15 deletions qdrant_client/http/api/collections_api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# flake8: noqa E501
from typing import IO, TYPE_CHECKING, Any, Dict, Set, Union

from qdrant_client._pydantic_compat import to_dict
from qdrant_client._pydantic_compat import to_json
from qdrant_client.http.models import *
from qdrant_client.http.models import models as m

Expand All @@ -18,8 +18,8 @@ def jsonable_encoder(
skip_defaults: bool = None,
exclude_unset: bool = False,
):
if hasattr(obj, "dict") or hasattr(obj, "model_dump"):
return to_dict(
if hasattr(obj, "json") or hasattr(obj, "model_dump_json"):
return to_json(
obj,
include=include,
exclude=exclude,
Expand Down Expand Up @@ -73,15 +73,17 @@ def _build_for_create_collection(
if timeout is not None:
query_params["timeout"] = str(timeout)

headers = {}
body = jsonable_encoder(create_collection)

if "Content-Type" not in headers:
headers["Content-Type"] = "application/json"
return self.api_client.request(
type_=m.InlineResponse2003,
method="PUT",
url="/collections/{collection_name}",
path_params=path_params,
params=query_params,
json=body,
data=body,
)

def _build_for_create_field_index(
Expand All @@ -104,15 +106,17 @@ def _build_for_create_field_index(
if ordering is not None:
query_params["ordering"] = str(ordering)

headers = {}
body = jsonable_encoder(create_field_index)

if "Content-Type" not in headers:
headers["Content-Type"] = "application/json"
return self.api_client.request(
type_=m.InlineResponse2006,
method="PUT",
url="/collections/{collection_name}/index",
path_params=path_params,
params=query_params,
json=body,
data=body,
)

def _build_for_create_snapshot(
Expand Down Expand Up @@ -333,15 +337,17 @@ def _build_for_recover_from_snapshot(
if wait is not None:
query_params["wait"] = str(wait).lower()

headers = {}
body = jsonable_encoder(snapshot_recover)

if "Content-Type" not in headers:
headers["Content-Type"] = "application/json"
return self.api_client.request(
type_=m.InlineResponse2003,
method="PUT",
url="/collections/{collection_name}/snapshots/recover",
path_params=path_params,
params=query_params,
json=body,
data=body,
)

def _build_for_recover_from_uploaded_snapshot(
Expand Down Expand Up @@ -388,10 +394,12 @@ def _build_for_update_aliases(
if timeout is not None:
query_params["timeout"] = str(timeout)

headers = {}
body = jsonable_encoder(change_aliases_operation)

if "Content-Type" not in headers:
headers["Content-Type"] = "application/json"
return self.api_client.request(
type_=m.InlineResponse2003, method="POST", url="/collections/aliases", params=query_params, json=body
type_=m.InlineResponse2003, method="POST", url="/collections/aliases", params=query_params, data=body
)

def _build_for_update_collection(
Expand All @@ -411,15 +419,17 @@ def _build_for_update_collection(
if timeout is not None:
query_params["timeout"] = str(timeout)

headers = {}
body = jsonable_encoder(update_collection)

if "Content-Type" not in headers:
headers["Content-Type"] = "application/json"
return self.api_client.request(
type_=m.InlineResponse2003,
method="PATCH",
url="/collections/{collection_name}",
path_params=path_params,
params=query_params,
json=body,
data=body,
)

def _build_for_update_collection_cluster(
Expand All @@ -436,15 +446,17 @@ def _build_for_update_collection_cluster(
if timeout is not None:
query_params["timeout"] = str(timeout)

headers = {}
body = jsonable_encoder(cluster_operations)

if "Content-Type" not in headers:
headers["Content-Type"] = "application/json"
return self.api_client.request(
type_=m.InlineResponse2003,
method="POST",
url="/collections/{collection_name}/cluster",
path_params=path_params,
params=query_params,
json=body,
data=body,
)


Expand Down
Loading

0 comments on commit fc4fc2b

Please sign in to comment.