diff service/rdf_to_mqtt/rdf_over_http.py @ 732:fdddbdaf07b5

more service renaming; start a lot more serv.n3 job files Ignore-this: 635aaefc7bd2fa5558eefb8b3fc9ec75
author drewp@bigasterisk.com
date Thu, 06 Feb 2020 16:36:35 -0800
parents service/mqtt_graph_bridge/rdf_over_http.py@925bc4137c93
children 3f4b447d65f5
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/service/rdf_to_mqtt/rdf_over_http.py	Thu Feb 06 16:36:35 2020 -0800
@@ -0,0 +1,34 @@
+from rdflib import Graph, URIRef, Literal, Namespace
+from rdflib.parser import StringInputSource
+
+ROOM = Namespace('http://projects.bigasterisk.com/room/')
+
+
+def rdfGraphBody(body, headers):
+    g = Graph()
+    g.parse(StringInputSource(body), format='nt')
+    return g
+
+
+def expandQueryParamUri(txt) -> URIRef:
+    if txt.startswith(':'):
+        return ROOM[txt.lstrip(':')]
+    # etc
+    return URIRef(txt)
+
+
+def rdfStatementsFromRequest(arg, body, headers):
+    if arg.get('s') and arg.get('p'):
+        subj = expandQueryParamUri(arg['s'][-1])
+        pred = expandQueryParamUri(arg['p'][-1])
+        turtleLiteral = body
+        try:
+            obj = Literal(float(turtleLiteral))
+        except ValueError:
+            obj = Literal(turtleLiteral)
+        yield (subj, pred, obj)
+    else:
+        g = rdfGraphBody(body, headers)
+        assert len(g) == 1, len(g)
+        yield g.triples((None, None, None)).next()
+        # could support multiple stmts