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

make serialization api a touch easier to use #837

Merged
merged 1 commit into from
Sep 22, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 3 additions & 5 deletions subiquity/common/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import inspect
import json

import aiohttp

Expand Down Expand Up @@ -42,9 +41,8 @@ async def impl(*args, **kw):
if arg_name == payload_arg:
data = serializer.serialize(payload_ann, value)
else:
query_args[arg_name] = json.dumps(
serializer.serialize(
meth_params[arg_name].annotation, value))
query_args[arg_name] = serializer.to_json(
meth_params[arg_name].annotation, value)
async with make_request(
meth.__name__, path, json=data, params=query_args) as resp:
resp.raise_for_status()
Expand Down Expand Up @@ -77,7 +75,7 @@ async def make_request(method, path, *, params, json):
# session.request needs a full URL with scheme and host
# even though that's in some ways a bit silly with a unix
# socket, so we just hardcode something here (I guess the
# "a" gets sent a long to the server inthe Host: header
# "a" gets sent along to the server in the Host: header
# and the server could in principle do something like
# virtual host based selection but well....)
url = 'http://a' + path
Expand Down
9 changes: 3 additions & 6 deletions subiquity/common/api/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import inspect
import json

from aiohttp import web

Expand Down Expand Up @@ -104,13 +103,11 @@ async def handler(request):
args = {}
try:
if data_annotation is not None:
payload = json.loads(await request.text())
args[data_arg] = serializer.deserialize(
data_annotation, payload)
args[data_arg] = serializer.from_json(
data_annotation, await request.text())
for arg, ann, default in query_args_anns:
if arg in request.query:
v = serializer.deserialize(
ann, json.loads(request.query[arg]))
v = serializer.from_json(ann, request.query[arg])
elif default != inspect._empty:
v = default
else:
Expand Down
12 changes: 12 additions & 0 deletions subiquity/common/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import datetime
import enum
import json
import inspect
import typing

Expand Down Expand Up @@ -124,3 +125,14 @@ def deserialize(self, annotation, value, metadata=None):
if isinstance(annotation, type) and issubclass(annotation, enum.Enum):
return getattr(annotation, value)
return self.type_deserializers[annotation](annotation, value, metadata)

def to_json(self, annotation, value):
return json.dumps(self.serialize(annotation, value))

def from_json(self, annotation, value):
return self.deserialize(annotation, json.loads(value))


_serializer = Serializer()
to_json = _serializer.to_json
from_json = _serializer.from_json