This repository has been archived by the owner on Jul 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Utility.cfc
117 lines (108 loc) · 6.45 KB
/
Utility.cfc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<cfcomponent displayname="Utility" output="false" initmethod="init" access="public">
<cfset variables.requireRequestBody = "post,put">
<cffunction name="init" access="public" output="false">
<cfreturn this>
</cffunction>
<cffunction name="CTCTRequest" access="public" output="false" returntype="any" hint="Processes CTCT WebRequests">
<cfargument name="method" required="true" type="string" hint="valid options: get, put, post, delete">
<cfargument name="address" required="true" type="string">
<cfargument name="requestValue" type="any">
<cfscript>
local.httpRequest = new Http(username=application.apiKey & '%' & application.ccUsername, password=application.ccPassword);
local.httpRequest.setMethod('#arguments.method#');
local.httpRequest.setUrl('#arguments.address#');
local.httpRequest.addParam(type='header', name='Content-Type', value='application/atom+xml');
local.httpRequest.addParam(type='header', name='accept', value='application/atom+xml');
if(listContains(variables.requireRequestBody, arguments.method)) {
local.httpRequest.addParam(type='body', value='#arguments.requestValue#');
}
local.httpResponse = local.httpRequest.send().getPrefix();
debug(local.httpResponse, arguments.method, arguments.address);
return local.httpResponse.fileContent;
</cfscript>
</cffunction>
<cffunction name="multiPartPost" access="private" output="false" returntype="any">
<cfargument name="activityType" required="true" type="string">
<cfargument name="lists" required="true" type="array">
<cfargument name="dataFile" type="string">
<cfscript>
local.httpRequest = new Http(username=application.apiKey & '%' & application.ccUsername, password=application.ccPassword);
local.httpRequest.setMethod('post');
local.httpRequest.setUrl(application.apiPath & '/activities');
local.httpRequest.addParam(type='header', name='accept', value='application/atom+xml');
local.httpRequest.addParam(type='formField', name='activityType', value='#arguments.activityType#');
for (i=1; i LTE arrayLen(arguments.lists); i=i+1) {
local.httpRequest.addParam(type='formField', name='lists', value='#arguments.lists[i]#');
}
if(isdefined('arguments.dataFile')) {
local.httpRequest.addParam(type='formField', name='data', value='#arguments.dataFile#');
}
local.httpResponse = local.httpRequest.send().getPrefix();
debug(local.httpResponse, 'post', '#application.apiPath#/activities');
return local.httpResponse.fileContent;
</cfscript>
</cffunction>
<cffunction name="urlencodedPost" access="private" output="false" returntype="any">
<cfargument name="uploadString" required="true" hint="encoded string to upload">
<cfscript>
local.httpRequest = new Http(username=application.apiKey & '%' & application.ccUsername, password=application.ccPassword);
local.httpRequest.setUrl(application.apiPath & '/activities');
local.httpRequest.setMethod('post');
//local.httpRequest.addParam(type='header', name='Content-Type', value='multipart/form-data');
local.httpRequest.addParam(type='header', name='Content-Type', value='application/x-www-form-urlencoded');
local.httpRequest.addParam(type='body',value='#arguments.uploadString#');
local.httpResponse = local.httpRequest.send().getPrefix();
debug(local.httpResponse, 'post', '#application.apiPath#/activities');
return local.httpResponse.fileContent;
</cfscript>
</cffunction>
<cffunction name="exportContactsPost" access="private" output="false" returntype="any">
<cfargument name="listId" required="true" type="string" hint="id of list to be exported" />
<cfargument name="columns" required="true" type="array" hint="array of columns to be exported" />
<cfargument name="fileType" type="string" default="CSV" hint="Valid: CSV, TXT" />
<cfargument name="exportOptDate" type="boolean" default="true" hint="include optDate in export" />
<cfargument name="exportOptSource" type="boolean" default="true" hint="include opt source in export" />
<cfargument name="exportListName" type="boolean" default="true" hint="include list name in export" />
<cfargument name="sortBy" type="string" default="EMAIL_ADDRESS" hint="Valid: EMAIL_ADDRESS, DATE_DESC" />
<cfscript>
local.httpRequest = new Http(username=application.apiKey & '%' & application.ccUsername, password=application.ccPassword);
local.httpRequest.setMethod('post');
local.httpRequest.setUrl(application.apiPath & '/activities');
local.httpRequest.addParam(type='header', name='accept', value='application/atom+xml');
local.httpRequest.addParam(type='formField', name='activityType', value='EXPORT_CONTACTS');
local.httpRequest.addParam(type='formField', name='listId', value="#arguments.listId#");
local.httpRequest.addParam(type='formField', name='fileType', value="#arguments.fileType#");
local.httpRequest.addParam(type='formField', name='exportOptDate', value="#arguments.exportOptDate#");
local.httpRequest.addParam(type='formField', name='exportOptSource', value="#arguments.exportOptSource#");
local.httpRequest.addParam(type='formField', name='exportListName', value="#arguments.exportListName#");
local.httpRequest.addParam(type='formField', name='sortBy', value="#arguments.sortBy#");
for(i=1; i LTE arrayLen(columns); i++) {
local.httpRequest.addParam(type='formField', name='columns', value="#arguments.columns[i]#");
}
local.httpResponse = local.httpRequest.send().getPrefix();
debug(local.httpResponse, 'post', '#application.apiPath#/activities');
return local.httpResponse.fileContent;
</cfscript>
</cffunction>
<cffunction name="debug" access="private" output="false" hint="file write">
<cfargument name="httpResponse" required="true">
<cfargument name="requestMethod" required="true" type="string">
<cfargument name="requestAddress" required="true" type="string">
<cfif application.debug EQ true>
<cfset local.failedStatus = "400,401,403,404,409,415,500">
<cfif listContains(local.failedStatus, arguments.httpResponse.responseHeader['status_code'])>
<cfset responseText = xmlFormat('#arguments.httpResponse.fileContent#') >
<cfsavecontent variable="local.errormessage">
<cfoutput>
Status Code: #arguments.httpResponse.statusCode#<br />
Request Method: #arguments.requestMethod#<br />
Request Address: #arguments.requestAddress#<br />
Response: #responseText#<br />
</cfoutput>
</cfsavecontent>
<cfthrow detail="#local.errormessage#"
message="HTTP Request error with Constant Contact">
</cfif>
</cfif>
</cffunction>
</cfcomponent>