-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.cfc
More file actions
137 lines (109 loc) · 4.18 KB
/
base.cfc
File metadata and controls
137 lines (109 loc) · 4.18 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
component accessors="true" displayname="Webhooks.io Client Library" {
property name="api_endpoint";
property name="api_version";
property name="api_token";
property name="account_id";
property name="application_id";
property name="base_url";
public any function init(required string account_id,
required string application_id,
required string api_token,
string api_endpoint="https://api.webhooks.io",
string api_version="v1"){
variables.account_id = Arguments.account_id;
variables.application_id = Arguments.application_id;
variables.api_token = Arguments.api_token;
variables.api_endpoint = Arguments.api_endpoint;
variables.api_version = Arguments.api_version;
return this;
}
private string function expandEndpoint(required string endpoint, required array params){
var placeholder = "";
var finalEndpoint = "";
var replacementCount = 1;
for(var i = 1; i <= ListLen(endpoint, '/'); i++){
placeholder = listGetAt(endpoint, i, '/');
if(left(placeholder, 1) == ':'){
placeholder = right(placeholder, len(placeholder)-1);
if(ArrayLen(params) <= i){
finalEndpoint = finalEndpoint & "/" & params[replacementCount];
replacementCount++;
}else{
finalEndpoint = finalEndpoint & "/" & placeholder;
}
}else{
finalEndpoint = finalEndpoint & "/" & placeholder;
}
}
return finalEndpoint;
}
/**
* @Hint Sends a given request as an HTTPPOST to the webhooks.io API.
**/
private any function execute(required string endpoint,
string method="GET",
any body="",
struct headers=StructNew(),
string content_type="application/json"){
var results = {
"successful" = true,
"http_status_text" = "",
"http_status_code" = "",
"response_parsed" = "",
"response" = "",
"headers" = {},
"meta" = {
"server_id" = "",
"server_version" = ""
}
};
var i = "";
var httpSvc = new http();
httpSvc.setUrl(getApi_endpoint() & '/' & arguments.endpoint);
httpSvc.setMethod(Arguments.Method);
httpSvc.addParam(type="header", name="Authorization",value="Bearer " & getApi_Token());
httpSvc.addParam(type="header", name="Content-Type",value=Arguments.content_type);
if(isStruct(Arguments.Body) && arguments.content_type == 'application/json'){
httpSvc.addParam(type="body",value=serializeJSON(arguments.body));
}
// add the request specific headers.
for(i in arguments.headers){
httpSvc.addParam(type="header", name=i,value=arguments.headers[i]);
}
var sendResult = httpSvc.send().getPrefix();
results["http_status_code"] = sendResult.responseheader.status_code;
results["http_status_text"] = sendResult.responseheader.explanation;
results["response"] = sendResult.filecontent;
for(i in sendResult.responseheader){
if(listFirst(i, '-') == 'Webhooksio'){
results["headers"][i] = sendResult.responseheader[i];
}
}
if(structKeyExists(sendResult.responseheader, "Server")){
results.meta.server_id = trim(listLast(sendResult.responseheader.Server, ';'));
results.meta.server_version = trim(listFirst(sendResult.responseheader.Server, ';'));
}
if(sendResult.responseheader.status_code >= 300){
successful = false;
}
return results;
}
/**
* @Hint Sends a webhook to the system.
*
* @Version The version of the logger API that should be utilized. This is v1 by default.
* @ProjectId The Id of the project this logger instance is for.
* @CollectorId The Id of the collector this logger instance is for.
**/
public any function sendWebhook(required string consumer_id, required string bucket_key, required string event, struct headers=StructNew(), any body=StructNew(), string application_id=getApplication_id()){
// add the event name to the group of headers...
arguments.headers['Webhooksio-Incoming-Event'] = arguments.event;
var endpoint = expandEndpoint("/v1/accounts/:account_id/applications/:application_id/consumers/:consumer_id/send/:bucket_key",
[arguments.account_id, arguments.application_id, arguments.consumer_id, arguments.bucket_key]);
return execute(
endpoint=endpoint,
method="POST",
body = body,
headers = headers);
}
}