1 /**
2 * Copyright © The Bot Blog 2019
3 * License: MIT (https://github.com/TheBotBlog/thebotbloglib/blob/master/LICENSE)
4 * Author: Jacob Jensen (bausshf)
5 */
6 module thebotbloglib.webmanager;
7 
8 import std.string : strip, format;
9 import std.array : join;
10 import std.algorithm : map;
11 import std.net.curl : get, post;
12 
13 import vibe.d : deserializeJson;
14 
15 import thebotbloglib.facebookservice;
16 import thebotbloglib.exceptions;
17 
18 package(thebotbloglib):
19 /// A web manager.
20 final class WebManager
21 {
22   /// The Facebook service associated with the manager.
23   private FacebookService _service;
24 
25   /// The base url for the manager.
26   private string _baseUrl;
27 
28   /**
29   * Creates a new web manager.
30   * Params:
31   *   service = The service of the web manager.
32   */
33   this(FacebookService service)
34   {
35     _service = service;
36 
37     _baseUrl = "https://graph.facebook.com/%s/%s%s";
38   }
39 
40   /**
41   * Gets a raw GET request and deserializes it as json.
42   * Params:
43   *   rawUrl = The url of the GET request.
44   * Returns:
45   *   Returns the deserialized json object.
46   */
47   T getRequestRaw(T)(string rawUrl)
48   {
49     if (!rawUrl || !rawUrl.strip.length)
50     {
51       throw new WebException("Missing url.");
52     }
53 
54     auto result = cast(string)get(rawUrl);
55 
56     auto obj = deserializeJson!T(result);
57 
58     return obj;
59   }
60 
61   /**
62   * Gets a raw POST request and deserializes it as json.
63   * Params:
64   *   rawUrl = The url of the POST request.
65   *   data = The data of the POST request.
66   * Returns:
67   *   Returns the deserialized json object.
68   */
69   T postRequestRaw(T)(string rawUrl, string[string] data)
70   {
71     if (!rawUrl || !rawUrl.strip.length)
72     {
73       throw new WebException("Missing url.");
74     }
75 
76     auto result = cast(string)post(rawUrl, data);
77 
78     import std.stdio : writeln, readln;
79     writeln(result);
80     readln();
81 
82     auto obj = deserializeJson!T(result);
83 
84     return obj;
85   }
86 
87   /**
88   * Creates a GET request and deserializes the response as json.
89   * Params:
90   *   id = The id of the request.
91   *   call = The graph api call of the request.
92   *   parameters = The parameters associated with the request.
93   * Returns:
94   *   Returns the deserialized json object.
95   */
96   T getRequest(T)(string id, string call, string[string] parameters)
97   {
98     parameters["access_token"] = _service.token;
99 
100     string[] parametersMapped = [];
101 
102     foreach (k,v; parameters)
103     {
104       parametersMapped ~= format("%s=%s", k, v);
105     }
106 
107     auto query = "?" ~ join(parametersMapped, "&");
108     auto formattedUrl = format(_baseUrl, id, call ? call : "", query);
109 
110     return getRequestRaw!T(formattedUrl);
111   }
112 
113   /**
114   * Creates a POST request and deserializes the response as json.
115   * Params:
116   *   id = The id of the request.
117   *   call = The graph api call of the request.
118   *   parameters = The parameters associated with the request.
119   * Returns:
120   *   Returns the deserialized json object.
121   */
122   T postRequest(T)(string id, string call, string[string] parameters)
123   {
124     parameters["access_token"] = _service.token;
125 
126     auto formattedUrl = format(_baseUrl, id, call ? call : "", "");
127 
128     return postRequestRaw!T(formattedUrl, parameters);
129   }
130 }