Skip to content

Commit

Permalink
Added fate api
Browse files Browse the repository at this point in the history
  • Loading branch information
Digant C Kasundra committed May 23, 2015
1 parent 27b8217 commit c26c4b5
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 13 deletions.
201 changes: 188 additions & 13 deletions hermes/handlers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from .util import ApiHandler
from .. import exc
from ..models import Host, EventType, Event, Labor
from ..models import Host, EventType, Event, Labor, Fate
from ..util import qp_to_bool as qpbool, parse_set_query


Expand Down Expand Up @@ -34,13 +34,9 @@ def post(self):
Location: /api/v1/hosts/example
{
"status": "ok",
"data": {
"host": {
"id": 1,
"hostname": "example"
}
}
"status": "create",
"id": 1,
"hostname": "example"
}
"""

Expand All @@ -64,6 +60,7 @@ def post(self):

json = host.to_dict("/api/v1")
json['href'] = "/api/v1/hosts/{}".format(host.hostname)
json['status'] = 'created'

self.created("/api/v1/hosts/{}".format(host.hostname), json)

Expand Down Expand Up @@ -312,6 +309,7 @@ def post(self):

json = event_type.to_dict("/api/v1")
json['href'] = "/api/v1/eventtypes/{}".format(event_type.id)
json['status'] = 'created'

self.created("/api/v1/eventtypes/{}".format(event_type.id), json)

Expand Down Expand Up @@ -539,7 +537,7 @@ def post(self):
try:
hostname = self.jbody['hostname']
user = self.jbody['user']
event_type_id = self.jbody['event_type_id']
event_type_id = self.jbody['eventTypeId']
note = self.jbody['note']
except KeyError as err:
raise exc.BadRequest("Missing Required Argument: {}".format(err.message))
Expand All @@ -549,10 +547,7 @@ def post(self):
event_type = self.session.query(EventType).get(event_type_id)

if event_type is None:
self.error({
'message':
"No matching EventState {} found".format(event_type_id)
})
self.write_error(400, message="Bad event type")
return

host = Host.get_host(self.session, hostname)
Expand All @@ -573,6 +568,7 @@ def post(self):

json = host.to_dict("/api/v1")
json['href'] = "/api/v1/events/{}".format(event.id)
json['status'] = 'created'

self.created("/api/v1/events/{}".format(event.id), json)

Expand Down Expand Up @@ -676,6 +672,185 @@ def put(self, id):
def delete(self, id):
"""Delete an Event
Not supported
"""
self.not_supported()


class FatesHandler(ApiHandler):

def post(self):
""" Create a Fate entry
Example Request:
POST /api/v1/fates HTTP/1.1
Host: localhost
Content-Type: application/json
{
creationEventTypeId: 1,
completionEventTypeId: 2,
intermediate: false,
description: "This is a fate"
}
Example response:
HTTP/1.1 201 OK
Location: /api/v1/hosts/example
{
"status": "created",
creationEventTypeId: 1,
completionEventTypeId: 2,
intermediate: false,
description: "This is a fate"
}
"""

try:
creation_event_type_id = self.jbody['creationEventTypeId']
completion_event_type_id = self.jbody['completionEventTypeId']
intermediate = self.jbody['intermediate']
description = self.jbody['description']
except KeyError as err:
raise exc.BadRequest("Missing Required Argument: {}".format(err.message))
except ValueError as err:
raise exc.BadRequest(err.message)

creation_event_type = (
self.session.query(EventType).get(creation_event_type_id)
)

if creation_event_type is None:
self.write_error(400, message="Bad creation event type")
return

completion_event_type = (
self.session.query(EventType).get(completion_event_type_id)
)

if completion_event_type is None:
self.write_error(400, message="Bad event type")
return

try:
fate = Fate.create(
self.session, creation_event_type, completion_event_type,
intermediate=intermediate, description=description
)
except IntegrityError as err:
raise exc.Conflict(err.orig.message)
except exc.ValidationError as err:
raise exc.BadRequest(err.message)

self.session.commit()

json = fate.to_dict("/api/v1")
json['href'] = "/api/v1/fates/{}".format(fate.id)

self.created("/api/v1/fates/{}".format(fate.id), json)

def get(self):
""" Get all Fates
Example Request:
GET /api/v1/fates HTTP/1.1
Host: localhost
Example response:
HTTP/1.1 200 OK
Content-Type: application/json
{
limit: int,
page: int,
totalFates: int,
fates: [
{
id: int,
creationEventTypeId: int,
completionEventType: int,
intermediate: true|false,
description: string,
},
...
],
}
"""
fates = self.session.query(Fate)

offset, limit, expand = self.get_pagination_values()
hosts, total = self.paginate_query(fates, offset, limit)

json = {
"limit": limit,
"offset": offset,
"totalFates": total,
"fates": [fate.to_dict("/api/v1") for fate in fates.all()],
}

self.success(json)


class FateHandler(ApiHandler):
def get(self, id):
"""Get a specific Fate
Example Request:
GET /api/v1/fates/1/ HTTP/1.1
Host: localhost
Example response:
HTTP/1.1 200 OK
Content-Type: application/json
{
"status": "ok",
id: 1,
creationEventTypeId: 1,
completionEventTypeId: 2,
intermediate: false,
description: "This is a fate"
}
Args:
id: the id of the fate to get
"""
offset, limit, expand = self.get_pagination_values()
fate = self.session.query(Fate).filter_by(id=id).scalar()
if not fate:
raise exc.NotFound("No such Fate {} found".format(id))

json = fate.to_dict("/api/v1")
json['limit'] = limit
json['offset'] = offset

if "eventtypes" in expand:
json['creationEventType'] = (
fate.creation_event_type.to_dict('/api/v1')
)
json['competionEventType'] = (
fate.completion_event_type.to_dict('/api/v1')
)

self.success(json)

def put(self, id):
"""Update a Fate
Not supported
"""
self.not_supported()

def delete(self, id):
"""Delete a Fate
Not supported
"""
self.not_supported()
4 changes: 4 additions & 0 deletions hermes/handlers/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ def not_supported(self):
def write_error(self, status_code, **kwargs):

message = "An unknown problem has occured :("
if "message" in kwargs:
message = kwargs['message']

if "exc_info" in kwargs:
inst = kwargs["exc_info"][1]
if isinstance(inst, HTTPError):
Expand All @@ -139,6 +142,7 @@ def write_error(self, status_code, **kwargs):
"message": message,
},
})
self.set_status(status_code, message)

def success(self, data):
"""200 OK"""
Expand Down
4 changes: 4 additions & 0 deletions hermes/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
(r"/api/v1/events", api.EventsHandler),
(r"/api/v1/events/(?P<id>\d+)/", api.EventHandler),

# Fates
(r"/api/v1/fates", api.FatesHandler),
(r"/api/v1/fates/(?P<id>\d+)/", api.FateHandler),

# Frontend Handlers
(r"/.*", frontends.AppHandler),
]

0 comments on commit c26c4b5

Please sign in to comment.