1 module konnexengine.integrations.mailerlite.api; 2 3 import std.conv : to; 4 import std.typecons: Nullable; 5 6 import vibe.core.log : logInfo, logWarn; 7 import vibe.data.json : Json, parseJsonString; 8 import vibe.stream.operations : readAllUTF8; 9 import vibe.http.common : HTTPMethod; 10 import vibe.http.client : requestHTTP; 11 import vibe.textfilter.urlencode : formEncode; 12 13 string BASE_URL = "https://api.mailerlite.com/api/v2/"; 14 15 struct AuthInfo 16 { 17 this(string apiKey) 18 { 19 this.apiKey = apiKey; 20 } 21 22 string apiKey; 23 string authHeader = "X-MailerLite-ApiKey"; 24 string contentHeader = "Content-Type"; 25 string contentType = "application/json"; 26 } 27 28 /// Subscriber Identifier 29 static struct CustomerDetails 30 { 31 this(string email, string name, string lastname, string countryName) 32 { 33 this.email = email; 34 this.name = name; 35 this.lastname = lastname; 36 this.countryName = countryName; 37 } 38 39 string email; 40 string name; 41 string lastname; 42 string countryName; 43 } 44 /// 45 unittest 46 { 47 48 } 49 50 /// Subscriber Identifier 51 static struct SubscriberIdentifier 52 { 53 this(string identifier) 54 { 55 this.identifier = identifier; 56 } 57 58 string identifier; 59 } 60 /// 61 unittest 62 { 63 64 } 65 66 Json createActiveSubscriberFrom(T)(T t, AuthInfo authInfo) 67 { 68 string _url = BASE_URL ~ "subscribers"; 69 // auto authInfo = AuthInfo(t.apiKey); 70 string response = ""; 71 Json reqBody = Json([ 72 "email": Json(t.email), 73 "type": Json("active"), 74 "name": Json(t.name), 75 "fields": Json([ 76 "last_name": Json(t.lastname), 77 "country": Json(t.countryName) 78 ]) 79 // "last_name": Json(t.lastName), 80 // "country": Json(t.country) 81 ]); 82 83 requestHTTP(_url, 84 (scope req) { 85 req.method = HTTPMethod.POST; 86 req.headers[authInfo.authHeader] = authInfo.apiKey; 87 req.headers[authInfo.contentHeader] = authInfo.contentType; 88 req.writeJsonBody(reqBody); 89 }, 90 (scope res) { 91 response = res.bodyReader.readAllUTF8(); 92 logWarn("\nMailerLite Subscribers API --- Response: %s\n", response); 93 }); 94 return parseJsonString(response); 95 } 96 97 Json updateActiveSubscriberFrom(T)(T t, AuthInfo authInfo) 98 { 99 string _url = BASE_URL ~ "subscribers/" ~ t.email; 100 // auto authInfo = AuthInfo(t.apiKey); 101 string response = ""; 102 Json reqBody = Json([ 103 "type": Json("active"), 104 "fields": Json([ 105 "last_name": Json(t.lastname), 106 "country": Json(t.countryName) 107 ]) 108 // "last_name": Json(t.lastName), 109 // "country": Json(t.country) 110 ]); 111 logInfo(reqBody.toPrettyString()); 112 113 requestHTTP(_url, 114 (scope req) { 115 req.method = HTTPMethod.PUT; 116 req.headers[authInfo.authHeader] = authInfo.apiKey; 117 req.headers[authInfo.contentHeader] = authInfo.contentType; 118 req.writeJsonBody(reqBody); 119 }, 120 (scope res) { 121 response = res.bodyReader.readAllUTF8(); 122 logWarn("\n1MailerLite Subscribers API --- Response: %s\n", response); 123 }); 124 return parseJsonString(response); 125 } 126 127 Json getSubscriberBy(T)(T t, AuthInfo authInfo) 128 { 129 string _url = BASE_URL ~ "subscribers/" ~t.identifier; 130 // auto authInfo = AuthInfo(t.apiKey); 131 string response = ""; 132 133 requestHTTP(_url, 134 (scope req) { 135 req.method = HTTPMethod.GET; 136 req.headers[authInfo.authHeader] = authInfo.apiKey; 137 }, 138 (scope res) { 139 response = res.bodyReader.readAllUTF8(); 140 logWarn("\n2MailerLite Subscribers API --- Response: %s\n", response); 141 }); 142 return parseJsonString(response); 143 } 144 145 146 147 static struct NewSubscriberToGroup 148 { 149 this(string g, string e, string n, string ln) //, string ln, string c) 150 { 151 // this.apiKey = a; 152 this.groupName = g; 153 this.email = e; 154 this.name = n; 155 this.lastName = ln; 156 // this.country = c; 157 } 158 159 // string apiKey; 160 // string apiEndpoint; 161 string groupName; 162 string email; 163 string name; 164 string lastName; 165 string country; 166 } 167 168 static class NewSubscriberToGroups 169 { 170 this(string a, string[] g, string e, string n) //, string ln, string c) 171 { 172 this.apiKey = a; 173 this.apiEndpoint = "groups/group_name/subscribers"; 174 this.groupNames = g; 175 this.email = e; 176 this.name = n; 177 // this.lastName = ln; 178 // this.country = c; 179 } 180 181 string apiKey; 182 string apiEndpoint; 183 string[] groupNames; 184 string email; 185 string name; 186 string lastName; 187 string country; 188 } 189 190 // Json prepareRequestBody(T)(T t) { 191 192 // } 193 194 Json addSubscriberToGroup(T)(T t, AuthInfo authInfo) 195 { 196 string _url = BASE_URL ~ "groups/group_name/subscribers"; 197 // auto authInfo = AuthInfo(t.apiKey); 198 string response = ""; 199 Json reqBody = Json([ 200 "group_name": Json(t.groupName), 201 "email": Json(t.email), 202 "name": Json(t.name) 203 // "last_name": Json(t.lastName), 204 // "country": Json(t.country) 205 ]); 206 207 requestHTTP(_url, 208 (scope req) { 209 req.method = HTTPMethod.POST; 210 req.headers[authInfo.authHeader] = authInfo.apiKey; 211 req.headers[authInfo.contentHeader] = authInfo.contentType; 212 req.writeJsonBody(reqBody); 213 }, 214 (scope res) { 215 response = res.bodyReader.readAllUTF8(); 216 logWarn("\nMailerLite Groups API --- Response: %s\n", response); 217 }); 218 return parseJsonString(response); 219 } 220 /// 221 unittest 222 { 223 auto APICall = NewSubscriberToGroup("03450809c61a7f48416e6527320867c1", "Survey Started", "mirellakimsing@gmail.com", "Mirella", formEncode("Kim Sing"), "United Kingdom"); // @suppress(dscanner.style.long_line) 224 225 auto result = addSubscriberToGroup!NewSubscriberToGroup(APICall); 226 assert(is(typeof(result) == Json)); 227 } 228 229 Json batchAddSubscriberToGroups(T)(T t) 230 { 231 string _url = BASE_URL ~ t.apiEndpoint; 232 auto authInfo = AuthInfo(t.apiKey); 233 string response = ""; 234 Json reqBody = Json([ 235 "requests": Json([ 236 Json([ 237 "method": "POST", 238 "path": "/api/v2/groups/group_name/subscribers", 239 "body": Json([ 240 "group_name": Json(t.groupName), 241 "email": Json(t.email), 242 "name": Json(t.name) 243 ]), 244 ]) 245 ]) 246 ]); 247 248 requestHTTP(_url, 249 (scope req) { 250 req.method = HTTPMethod.POST; 251 req.headers[authInfo.authHeader] = authInfo.apiKey; 252 req.headers[authInfo.contentHeader] = authInfo.contentType; 253 req.writeJsonBody(reqBody); 254 }, 255 (scope res) { 256 response = res.bodyReader.readAllUTF8(); 257 logWarn("\nMailerLite Groups API --- Response: %s\n", response); 258 }); 259 return parseJsonString(response); 260 }