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

Seperated Links from JSON API into its own file #30

Merged
merged 7 commits into from
Jun 14, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion optimade/server/models/errors.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Dict, List, Optional, Union

from .jsonapi import Link
from .links import Links
from .modified_jsonapi import Links
from pydantic import BaseModel, UrlStr, Schema

class ErrorSource(BaseModel):
Expand Down
21 changes: 17 additions & 4 deletions optimade/server/models/jsonapi.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
"""
This module should reproduce https://jsonapi.org/schema
"""

from typing import Optional, Set, Union, Dict, Any
from pydantic import BaseModel, UrlStr, constr

class Meta(Dict[str, Any]):
"""Non-standard meta-information that can not be represented as an attribute or relationship."""

class Link(BaseModel):
href: UrlStr
meta: Optional[dict]

# class Links(BaseModel):
# next: Optional[Union[UrlStr,Link]]
# base_url: Optional[Union[UrlStr,Link]]
class Links(BaseModel):
next: Optional[Union[UrlStr,Link]]
self: Optional[Union[UrlStr,Link]]
about: Optional[Union[UrlStr, Link]]

class JsonAPI(BaseModel):
version: str
Expand All @@ -35,6 +38,7 @@ class Error(BaseModel):
detail: Optional[str]
source: Optional[Source]
meta: Optional[dict]
links: Optional[Links]

class Failure(BaseModel):
errors: Set[Error]
Expand Down Expand Up @@ -65,6 +69,15 @@ class Relationship(BaseModel):
data: Optional[Union[Linkage, Set[Linkage]]]
meta: Optional[dict]

# class Empty(None):
# """Describes an empty to-one relationship."""

class RelationshipToOne(Linkage):
"""References to other resource objects in a to-one (\"relationship\"). Relationships can be specified by including a member in a resource's links object."""

class RelationshipToMany(Set[Linkage]):
"""An array of objects each containing \"type\" and \"id\" members for to-many relationships."""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a question: is it necessary to escape quotation marks here?
Does it otherwise break the JSON?
I would have thought pydantic could be a bit more clever...


rel_pat_prop = constr(regex=r"^(?!id$|type$)\\w[-\\w_]*$")
class Relationships(BaseModel):
items : Optional[Dict[rel_pat_prop, Relationship]]
Expand Down
26 changes: 26 additions & 0 deletions optimade/server/models/modified_jsonapi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from optimade.server.models.jsonapi import Link, Links, Source

from datetime import datetime
from pydantic import UrlStr, BaseModel
from typing import Optional, Union, Any

class Attributes(BaseModel):
local_id: UrlStr
lad_modified: datetime
immutable_id: Optional[UrlStr]

class ErrorLinks(BaseModel):
about: Union[Link, UrlStr]

class Error(BaseModel):
id: Optional[str]
status: Optional[str]
code: Optional[str]
title: Optional[str]
detail: Optional[str]
source: Optional[Source]
meta: Optional[dict]
links: Optional[ErrorLinks]

class Links(Links):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this very ugly, redefinition of the same name when importing something? Looks like a problem waiting to happen

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could simply from .jsonapi import Links as JSONLinks

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My preference here would be to just import the namespace and then use jsonapi.Links as the base class.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even better!

base_rul: Optional[Union[Link, UrlStr]]