Created
July 18, 2016 18:17
-
-
Save jasonhuck/a6d8f5d96d812dd9af29ebe8efec4ce9 to your computer and use it in GitHub Desktop.
Lasso 8.x wrapper for Mailchimp API v3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[//lasso | |
library('mailchimp.lasso'); | |
// create a new mailchimp instance | |
// NOTE: -verifypeer worked fine for me on Linux, but not Windows | |
var('mc') = mailchimp( -key='{YOUR_MAILCHIMP_API_KEY}', -version=3.0, -verifypeer=false); | |
// get all campaigns | |
$mc->get('/campaigns'); | |
// get all campaigns, filter results | |
var('campaigns') = $mc->get('/campaigns', map('type' = 'regular', 'status' = 'sent')); | |
$campaigns; | |
// get a specific campaign | |
$mc->get('/campaigns/{YOUR_CAMPAIGN_ID}', map('exclude_fields' = 'settings,long_archive_url')); | |
// get all lists | |
$mc->get('/lists', map('fields' = 'lists.id,lists.name')); | |
// get a specific list by ID | |
$mc->get('/lists/{YOUR_LIST_ID}'); | |
// subscribe someone to a list | |
$mc->post('/lists/{YOUR_LIST_ID}/members/', map('email_address' = '[email protected]', 'status' = 'subscribed')); | |
// create a new campaign | |
$mc->post('/campaigns', map( | |
'recipients' = map('list_id' = '{YOUR_LIST_ID}'), | |
'type' = 'regular', | |
'settings' = map('subject_line' = 'Created via Lasso.', 'reply_to' = '[email protected]', 'from_name' = 'Your Name') | |
)); | |
// manage content for a campaign | |
$mc->put('/campaigns/{YOUR_CAMPAIGN_ID}/content', map('plain_text' = 'This is a test.', 'html' = '<strong>This is a <em>test</em>!</strong>')); | |
// test a campaign | |
$mc->post('/campaigns/{YOUR_CAMPAIGN_ID}/actions/test', map('test_emails' = array('[email protected]'), 'send_type' = 'html')); | |
// schedule a campaign | |
$mc->post('/campaigns/{YOUR_CAMPAIGN_ID}/actions/schedule', map('schedule_time' = date_localtogmt(date('2025-07-15 15:45:00')))); | |
// immediately send a campaign | |
$mc->post('/campaigns/{YOUR_CAMPAIGN_ID}/actions/send'); | |
// delete a campaign | |
$mc->delete('/campaigns/{YOUR_CAMPAIGN_ID}'); | |
// view the last error, if there was one | |
$mc->getLastError; | |
// inspect the headers, body, and other details from the last response | |
$mc->getLastResponse; | |
// details of the last request | |
$mc->getLastRequest; | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[//lasso | |
/** | |
* Super-simple, minimum abstraction MailChimp API v3 wrapper for Lasso 8.x. | |
* Based heavily on Drew McLellan's original version for PHP: | |
* https://github.com/drewm/mailchimp-api | |
* MailChimp API v3: http://developer.mailchimp.com | |
* | |
* :NOTE: Batch operations and webhooks are not implemented. | |
* | |
* @author Jason Huck <[email protected]> | |
* @version 1.0 | |
*/ | |
define_type( | |
'mailchimp', | |
-prototype, | |
-description='Wraps v3 of the Mailchimp API.' | |
); | |
local( | |
'api_key' = string, | |
'api_endpoint' = 'https://us1.api.mailchimp.com/', | |
'version' = 3.0, | |
'verifypeer' = true, | |
'request_successful' = false, | |
'last_error' = map( | |
'code' = integer, | |
'message' = string | |
), | |
'last_response' = map( | |
'headers' = null, | |
'body' = null | |
), | |
'last_request' = map( | |
'headers' = null, | |
'body' = null | |
) | |
); | |
define_tag( | |
'oncreate', | |
-req='key', -type='string', | |
-opt='version', -type='decimal', | |
-opt='verifypeer', -type='boolean' | |
); | |
self->'api_key' = #key; | |
local('data_center') = string(self->'api_key')->split('-')->second; | |
self->'api_endpoint' = (self->'api_endpoint')->replace('us1', #data_center)&; | |
local_defined('version') ? self->'version' = #version; | |
local_defined('verifypeer') ? self->'verifypeer' = #verifypeer; | |
/define_tag; | |
// use this to generate a subscriber hash when editing a list member | |
define_tag('subscriberHash', -req='email'); | |
return(encrypt_md5(string(#email)->lowercase&)); | |
/define_tag; | |
define_tag('success'); | |
return(self->'request_successful'); | |
/define_tag; | |
define_tag('getLastError'); | |
return(self->'last_error'); | |
/define_tag; | |
define_tag('getLastResponse'); | |
return(self->'last_response'); | |
/define_tag; | |
define_tag('getLastRequest'); | |
return(self->'last_request'); | |
/define_tag; | |
define_tag( | |
'delete', | |
-req='method', -type='string', | |
-opt='args', -type='map', | |
-opt='timeout', -type='integer' | |
); | |
!local_defined('args') ? local('args') = map; | |
!local_defined('timeout') ? local('timeout') = 10; | |
return(self->makeRequest('DELETE', #method, #args, #timeout)); | |
/define_tag; | |
define_tag( | |
'get', | |
-req='method', -type='string', | |
-opt='args', -type='map', | |
-opt='timeout', -type='integer' | |
); | |
!local_defined('args') ? local('args') = map; | |
!local_defined('timeout') ? local('timeout') = 10; | |
return(self->makeRequest('GET', #method, #args, #timeout)); | |
/define_tag; | |
define_tag( | |
'patch', | |
-req='method', -type='string', | |
-opt='args', -type='map', | |
-opt='timeout', -type='integer' | |
); | |
!local_defined('args') ? local('args') = map; | |
!local_defined('timeout') ? local('timeout') = 10; | |
return(self->makeRequest('PATCH', #method, #args, #timeout)); | |
/define_tag; | |
define_tag( | |
'post', | |
-req='method', -type='string', | |
-opt='args', -type='map', | |
-opt='timeout', -type='integer' | |
); | |
!local_defined('args') ? local('args') = map; | |
!local_defined('timeout') ? local('timeout') = 10; | |
return(self->makeRequest('POST', #method, #args, #timeout)); | |
/define_tag; | |
define_tag( | |
'put', | |
-req='method', -type='string', | |
-opt='args', -type='map', | |
-opt='timeout', -type='integer' | |
); | |
!local_defined('args') ? local('args') = map; | |
!local_defined('timeout') ? local('timeout') = 10; | |
return(self->makeRequest('PUT', #method, #args, #timeout)); | |
/define_tag; | |
define_tag( | |
'makeRequest', | |
-req='http_verb', -type='string', | |
-req='method', -type='string', | |
-opt='args', -type='map', | |
-opt='timeout', -type='integer' | |
); | |
!local_defined('args') ? local('args') = map; | |
!local_defined('timeout') ? local('timeout') = 10; | |
local('url') = self->'api_endpoint' + self->'version' + (!#method->beginswith('/') ? '/') + #method; | |
self->'last_error' = map( | |
'code' = integer, | |
'message' = string | |
); | |
self->'request_successful' = false; | |
self->'last_response' = map('headers' = null, 'body' = null); | |
local('headers') = array( | |
'Accept' = 'application/vnd.api+json', | |
'Content-Type' = 'application/vnd.api+json', | |
'Authorization' = 'apikey ' + self->'api_key', | |
'User-Agent' = 'Lasso ' + lasso_version | |
); | |
#http_verb != 'GET' ? #headers->insert('X-HTTP-Method-Override' = #http_verb); | |
self->'last_request' = map( | |
'method' = #http_verb, | |
'path' = #method, | |
'url' = #url, | |
'headers' = #headers, | |
'body' = '', | |
'timeout' = #timeout | |
); | |
protect; | |
handle_error; | |
self->'request_successful' = false; | |
if(local_defined('result') && #result->find('status')); | |
local('msg') = #result->find('status'); | |
#result->find('detail') ? #msg += ': ' + #result->find('detail'); | |
self->'last_error' = map( | |
'code' = error_code, | |
'message' = #msg | |
); | |
else; | |
self->'last_error' = map( | |
'code' = error_code, | |
'message' = error_msg | |
); | |
local('result') = false; | |
/if; | |
/handle_error; | |
if(#http_verb == 'GET'); | |
local('body') = #args; | |
local('request_params') = array( | |
#url, | |
-getparams=#args, | |
-sendmimeheaders=#headers, | |
-retrievemimeheaders='response_headers', | |
-timeout=#timeout, | |
-connecttimeout=#timeout | |
); | |
else; | |
local('body') = encode_json(#args); | |
local('request_params') = array( | |
#url, | |
-postparams=#body, | |
-sendmimeheaders=#headers, | |
-retrievemimeheaders='response_headers', | |
-timeout=#timeout, | |
-connecttimeout=#timeout | |
); | |
/if; | |
self->'verifypeer' ? #request_params->insert( -verifypeer); | |
local('request') = \include_url->run( -params=#request_params); | |
self->'last_error' = map( | |
'code' = error_code, | |
'message' = error_msg | |
); | |
local('result') = decode_json(#request); | |
self->'request_successful' = true; | |
/protect; | |
self->'last_request'->find('body') = #body; | |
self->'last_response'->find('headers') = $response_headers; | |
self->'last_response'->find('body') = #result; | |
return(#result); | |
/define_tag; | |
/define_type; | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment