Skip to content

Instantly share code, notes, and snippets.

@robertz
Last active July 9, 2019 23:02
Show Gist options
  • Save robertz/c56ecc736985fc824e79e4f0ff905a20 to your computer and use it in GitHub Desktop.
Save robertz/c56ecc736985fc824e79e4f0ff905a20 to your computer and use it in GitHub Desktop.
component output = "false" {
public function init() {
variables['instance'] = {};
instance['hasExecuted'] = false;
instance['isJson'] = "";
instance['created'] = now();
instance['url'] = "";
instance['resolvedUrl'] = "";
instance['verb'] = "GET";
instance['charset'] = "utf-8";
instance['headers'] = [];
instance['tokens'] = [];
instance['urlParams'] = [];
instance['formFields'] = [];
instance['body'] = {};
instance['result'] = {};
instance['responseHeader'] = {};
instance['timeout'] = 5;
return this;
}
// set the url for the cfhttp request
public function setURL(required string urlString) {
instance['url'] = urlString;
return this;
}
// set the url for the cfhttp request
public function setTimeout(required numeric timeout) {
instance['timeout'] = timeout;
return this;
}
// set the verb of the request (GET/PUT/POST/DELETE)
public function setVerb(required string verb) {
instance['verb'] = verb;
return this;
}
// set the charset, defaults to UTF-8
public function setCharset(required string charset) {
instance['charset'] = charset;
return this;
}
// set a new kvp array element for token replacement in the url (:key=value)
public function setToken(required string tokenString) {
instance.tokens.append(tokenString);
return this;
}
// set a new kvp array element to be appended to the url (key=value)
public function setUrlParam(required string paramString) {
instance.urlParams.append(paramString);
return this;
}
// set a new kvp array element to be added as a formfield (key=value)
public function setFormField(required string formField) {
instance.formFields.append(formField);
return this;
}
// set a new kvp array element to be added as a header (Content-Type=application/json)
public function setHeader(required string header) {
instance.headers.append(header);
return this;
}
// set a struct that will be the form body, automatically set the content-type to application/json
public function setBody(required struct body) {
// if body is set, set the content-type to application/json
instance.headers.append('Content-Type=application/json');
instance['body'] = body;
return this;
}
// execute the cfhttp request
public function execute() {
cfhttp(url = buildUrlString(), method = instance.verb, charset = instance.charset, timeout = instance.timeout) {
// build headers
if (instance.headers.len()) {
for (var h in instance.headers) {
cfhttpparam(type = "header", name = listFirst(h, "="), value = listRest(h, "="));
}
}
// form fields
if (instance.formFields.len()) {
for (var f in instance.formFields) {
cfhttpparam(type = "formfield", name = listFirst(f, "="), value = listRest(f, "="));
}
}
// body
if (!instance.body.isEmpty()) {
cfhttpparam(type = "body", value = serializeJSON(instance.body));
}
};
if (cfhttp.keyExists("responseHeader")) {
instance.responseHeader.append(cfhttp.responseHeader);
}
if (isJSON(cfhttp.fileContent)) {
instance['result'] = deserializeJSON(cfhttp.fileContent);
} else {
instance['result'] = cfhttp.fileContent;
}
instance['isJson'] = isJSON(cfhttp.fileContent) ? true : false;
instance['hasExecuted'] = true;
return this;
}
// return all instance variables for the current request
public function getMeta() {
return instance;
}
// return the cfhttp response
public function getResult() {
return instance.result;
}
// return the cfhttp responseHeader structure
public function getResponseHeader() {
return instance.responseHeader;
}
// return the status_code from the response header
public function getStatusCode() {
return instance.responseHeader.keyExists("status_code") ? instance.responseHeader.status_code : "";
}
// return the status_code from the response header
public function isJson() {
return (instance.isJson && instance.hasExecuted) ? true : false;
}
// build url string based on all route tokens and query params
private function buildUrlString() {
var urlString = instance.url;
// replace url tokens
if (instance.tokens.len()) {
for (var t in instance.tokens) {
urlString = urlString.replace(listFirst(t, "="), listRest(t, "="), "all");
}
}
// build query params
if (instance.urlParams.len()) {
urlString = urlString & "?" & arrayToList(instance.urlParams, "&");
}
instance['resolvedUrl'] = urlString;
return urlString;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment