@@ -3,11 +3,67 @@ defmodule Caylir.Graph.Request do
3
3
Sends requests to the server.
4
4
"""
5
5
6
+ alias Caylir.Graph
7
+
8
+ @ doc """
9
+ Deletes a quad from the graph.
10
+ """
11
+ @ spec delete ( Keyword . t , Keyword . t ) :: Graph . t_delete
12
+ def delete ( quad , conn ) do
13
+ url = Graph.URL . delete ( conn )
14
+ body = [ quad ] |> Poison . encode!
15
+
16
+ case send ( :post , url , body ) do
17
+ { :ok , 200 , _success } -> :ok
18
+ { :ok , 400 , % { error: reason } } -> { :error , reason }
19
+ end
20
+ end
21
+
22
+ @ doc """
23
+ Queries the graph.
24
+ """
25
+ @ spec query ( String . t , Keyword . t ) :: Graph . t_query
26
+ def query ( query , conn ) do
27
+ url = Graph.URL . query ( conn )
28
+
29
+ case send ( :post , url , query ) do
30
+ { :ok , 200 , % { result: result } } -> result
31
+ { :ok , 400 , % { error: reason } } -> { :error , reason }
32
+ end
33
+ end
34
+
35
+
6
36
@ doc """
7
- Send a request to the server .
37
+ Gets the shape of a query .
8
38
"""
9
- @ spec send ( { atom , String . t , String . t } ) :: any
10
- def send ( { method , url , payload } ) do
39
+ @ spec shape ( String . t , Keyword . t ) :: Graph . t_query
40
+ def shape ( query , conn ) do
41
+ url = Graph.URL . shape ( conn )
42
+
43
+ case send ( :post , url , query ) do
44
+ { :ok , 200 , shape } -> shape
45
+ { :ok , 400 , % { error: reason } } -> { :error , reason }
46
+ end
47
+ end
48
+
49
+ @ doc """
50
+ Writes a quad to the graph.
51
+ """
52
+ @ spec write ( Keyword . t , Keyword . t ) :: Graph . t_write
53
+ def write ( quad , conn ) do
54
+ url = Graph.URL . write ( conn )
55
+ body = [ quad ] |> Poison . encode!
56
+
57
+ case send ( :post , url , body ) do
58
+ { :ok , 200 , _success } -> :ok
59
+ { :ok , 400 , % { error: reason } } -> { :error , reason }
60
+ end
61
+ end
62
+
63
+
64
+ # Utility methods
65
+
66
+ defp send ( method , url , payload ) do
11
67
body = payload |> :binary . bin_to_list ( )
12
68
headers = [ { 'Content-Type' , 'application/x-www-form-urlencoded' } ,
13
69
{ 'Content-Length' , length ( body ) } ]
@@ -18,7 +74,6 @@ defmodule Caylir.Graph.Request do
18
74
parse_response ( status , response )
19
75
end
20
76
21
-
22
77
defp parse_response ( status , '' ) , do: { :ok , status }
23
78
defp parse_response ( status , body ) do
24
79
{ :ok , status , Poison . decode! ( body , keys: :atoms ) }
0 commit comments