Skip to content

Commit

Permalink
Add get method to HttpClient
Browse files Browse the repository at this point in the history
There are two new HttpClient instances available:

* this.options.api (api.nexmo.com)
* this.options.rest (rest.nexmo.com)

If you specify `host` in `options` when running `new Nexmo()` you
can override the URL used
  • Loading branch information
mheap committed Dec 1, 2017
1 parent 902b435 commit d60e259
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 34 deletions.
23 changes: 21 additions & 2 deletions src/HttpClient.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
var https = require("https");
var http = require("http");
var querystring = require("querystring");

class HttpClient {
constructor(options) {
constructor(options, credentials) {
this.credentials = credentials;
this.host = options.host || "rest.nexmo.com";
this.port = options.port || 443;
this.https = options.https || https;
this.http = options.http || http;
Expand Down Expand Up @@ -32,7 +35,7 @@ class HttpClient {
// headers['Content-Length'] = 0;
}
var options = {
host: endpoint.host ? endpoint.host : "rest.nexmo.com",
host: endpoint.host ? endpoint.host : this.host,
port: this.port,
path: endpoint.path,
method: endpoint.method,
Expand Down Expand Up @@ -163,6 +166,22 @@ class HttpClient {
callback(error, response);
}
}

get(path, params, callback) {
params = params || {};
params["api_key"] = this.credentials.apiKey;
params["api_secret"] = this.credentials.apiSecret;

path = path + '?' + querystring.stringify(params);

this.request(
{
path: path
},
"GET",
callback
)
}
}

export default HttpClient;
30 changes: 1 addition & 29 deletions src/Message.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use strict";

import querystring from "querystring";
import nexmo from "./index";

class Message {
Expand Down Expand Up @@ -67,34 +66,7 @@ class Message {
}

search(id, callback) {
this.options.httpClient.request(
this.getRequestConfig("GET", "/search/message", { id: id }),
callback
);
}

getRequestConfig(verb, path, params) {
params["api_key"] = this.options.auth.api_key;
params["api_secret"] = this.options.auth.api_secret;

var bodyParams = "";
params = querystring.stringify(params);

if (verb === "GET") {
path += "?" + params;
} else {
bodyParams = params;
}

return {
host: "rest.nexmo.com",
path: path,
method: verb,
body: bodyParams,
headers: {
"Content-Type": "application/json"
}
};
this.options.rest.get("/search/message", { id: id }, callback);
}
}

Expand Down
17 changes: 16 additions & 1 deletion src/Nexmo.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,22 @@ class Nexmo {
if (this.options.appendToUserAgent) {
this.options.userAgent += ` ${this.options.appendToUserAgent}`;
}
this.options.httpClient = new HttpClient(this.options);

// This is legacy, everything should use rest or api going forward
this.options.httpClient = new HttpClient(
Object.assign({"host": "rest.nexmo.com"}, this.options),
this.credentials
);

// We have two different hosts, so we use two different HttpClients
this.options.api = new HttpClient(
Object.assign({"host": "api.nexmo.com"}, this.options),
this.credentials
);
this.options.rest = new HttpClient(
Object.assign({"host": "rest.nexmo.com"}, this.options),
this.credentials
);

this.message = new Message(this.credentials, this.options);
this.voice = new Voice(this.credentials, this.options);
Expand Down
2 changes: 0 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ exports.initialize = function(pkey, psecret, options) {
api_key: pkey,
api_secret: psecret
};

options.auth = up;
_options = options;
};

Expand Down

0 comments on commit d60e259

Please sign in to comment.