1 module viva.requests.requests; 2 3 // TODO: Replace with viva JSON lib once ready 4 import std.json; 5 6 private Requests _requests = Requests(); 7 /// Perform simple web requests 8 @property public Requests requests() { return _requests; } 9 10 /++ 11 + An object for holding information about request reponses 12 +/ 13 struct Response 14 { 15 /++ 16 + The response code 17 +/ 18 uint code; 19 20 /++ 21 + The response JSON data 22 +/ 23 JSONValue data; 24 } 25 26 // TODO: How do we find response code? 27 private struct Requests 28 { 29 /++ 30 + Perform a `GET` request on the specified URL 31 + Params: 32 + url = The URL to perform the request to 33 + Returns: The reponse data from the request 34 +/ 35 public Response get(string url) 36 { 37 import std.net.curl : get; 38 39 return Response(0, parseJSON(get(url))); 40 } 41 42 /++ 43 + Perform a `POST` request on the specified URL 44 + Params: 45 + url = The URL to perform the request to 46 + data = The data to be sent with the request 47 + Returns: The reponse data from the request 48 +/ 49 public Response post(string url, string[string] data) 50 { 51 import std.net.curl : post; 52 53 return Response(0, parseJSON(post(url, data))); 54 } 55 56 /++ 57 + Perform a `PUT` request on the specified URL 58 + Params: 59 + url = The URL to perform the request to 60 + data = The data to be sent with the request 61 + Returns: The reponse data from the request 62 +/ 63 public Response put(string url, const(char[]) data) 64 { 65 import std.net.curl : put; 66 67 return Response(0, parseJSON(put(url, data))); 68 } 69 70 /++ 71 + Perform a `DELETE` request on the specified URL 72 + Params: 73 + url = The URL to perform the request to 74 + Returns: The reponse data from the request 75 +/ 76 public Response del(string url) 77 { 78 import std.net.curl : del; 79 80 del(url); 81 // TODO: Hmm? 82 return Response(0, parseJSON("{}")); 83 } 84 }