-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumer.py
43 lines (34 loc) · 1.42 KB
/
consumer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from rdflib import Graph, URIRef
import requests
import json
from .base import BaseLDN
class Consumer(BaseLDN):
def __init__(self, **kwargs):
super(self.__class__, self).__init__(**kwargs)
def notifications(self, inbox, **kwargs):
"""
Retrieve all of the notification IRIs from an ldp:inbox as a list.
"""
headers = kwargs.pop("headers", dict())
if 'accept' not in headers:
headers['accept'] = kwargs.pop("accept", self.accept_headers)
r = requests.get(inbox, headers=headers, **kwargs)
r.raise_for_status()
g = Graph().parse(data=r.text, format=self.content_type_to_mime_type(
r.headers['content-type']))
return [str(o) for (s, o) in g[:URIRef(self.LDP_CONTAINS)]]
def notification(self, iri, **kwargs):
"""
Retrieve a single LDN notification and decode into a Python object.
"""
headers = kwargs.pop("headers", dict())
if 'accept' not in headers:
headers['accept'] = kwargs.pop("accept", self.accept_headers)
r = requests.get(iri, headers=headers, **kwargs)
r.raise_for_status()
mime_type = self.content_type_to_mime_type(r.headers['content-type'])
if mime_type == self.JSON_LD:
return r.json()
else:
g = Graph().parse(data=r.text, format=mime_type)
return json.loads(g.serialize(format="json-ld"))