Skip to content
This repository has been archived by the owner on Oct 21, 2022. It is now read-only.

Commit

Permalink
dry-up auth, handle no-credential job
Browse files Browse the repository at this point in the history
  • Loading branch information
taylordowns2000 committed Sep 25, 2017
1 parent fa76562 commit 6be64ed
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 81 deletions.
79 changes: 28 additions & 51 deletions lib/Adaptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ Object.defineProperty(exports, 'sourceValue', {

var _Client = require('./Client');

var _Utils = require('./Utils');

/**
* Execute a sequence of operations.
* Wraps `language-common/execute`, and prepends initial state for http.
Expand Down Expand Up @@ -123,11 +125,7 @@ function execute() {
function get(path, params, callback) {

return function (state) {
var _state$configuration = state.configuration,
baseUrl = _state$configuration.baseUrl,
username = _state$configuration.username,
password = _state$configuration.password,
authType = _state$configuration.authType;
var baseUrl = state.configuration.baseUrl;

var url = baseUrl ? baseUrl + path : path;

Expand All @@ -136,14 +134,9 @@ function get(path, params, callback) {
headers = _expandReferences.headers,
authentication = _expandReferences.authentication;

var auth = authentication || {
'username': username,
'password': password,
'sendImmediately': authType != 'digest'
};
var auth = (0, _Utils.setAuth)(state.configuration, authentication);

return (0, _Client.req)("GET", { url: url, query: query, auth: auth, headers: headers }).then(function (response) {
console.log(response);
var nextState = (0, _languageCommon.composeNextState)(state, response);
if (callback) return callback(nextState);
return nextState;
Expand Down Expand Up @@ -173,11 +166,11 @@ function get(path, params, callback) {
function post(path, params, callback) {

return function (state) {
var _state$configuration2 = state.configuration,
baseUrl = _state$configuration2.baseUrl,
username = _state$configuration2.username,
password = _state$configuration2.password,
authType = _state$configuration2.authType;
var _state$configuration = state.configuration,
baseUrl = _state$configuration.baseUrl,
username = _state$configuration.username,
password = _state$configuration.password,
authType = _state$configuration.authType;

var url = baseUrl ? baseUrl + path : path;

Expand All @@ -187,11 +180,7 @@ function post(path, params, callback) {
authentication = _expandReferences2.authentication,
body = _expandReferences2.body;

var auth = authentication || {
'username': username,
'password': password,
'sendImmediately': authType != 'digest'
};
var auth = (0, _Utils.setAuth)(state.configuration, authentication);

return (0, _Client.req)("POST", { url: url, query: query, body: body, auth: auth, headers: headers }).then(function (response) {
var nextState = (0, _languageCommon.composeNextState)(state, response);
Expand Down Expand Up @@ -223,11 +212,11 @@ function post(path, params, callback) {
function put(path, params, callback) {

return function (state) {
var _state$configuration3 = state.configuration,
baseUrl = _state$configuration3.baseUrl,
username = _state$configuration3.username,
password = _state$configuration3.password,
authType = _state$configuration3.authType;
var _state$configuration2 = state.configuration,
baseUrl = _state$configuration2.baseUrl,
username = _state$configuration2.username,
password = _state$configuration2.password,
authType = _state$configuration2.authType;

var url = baseUrl ? baseUrl + path : path;

Expand All @@ -237,11 +226,7 @@ function put(path, params, callback) {
authentication = _expandReferences3.authentication,
body = _expandReferences3.body;

var auth = authentication || {
'username': username,
'password': password,
'sendImmediately': authType != 'digest'
};
var auth = (0, _Utils.setAuth)(state.configuration, authentication);

return (0, _Client.req)("PUT", { url: url, query: query, body: body, auth: auth, headers: headers }).then(function (response) {
var nextState = (0, _languageCommon.composeNextState)(state, response);
Expand Down Expand Up @@ -273,11 +258,11 @@ function put(path, params, callback) {
function patch(path, params, callback) {

return function (state) {
var _state$configuration4 = state.configuration,
baseUrl = _state$configuration4.baseUrl,
username = _state$configuration4.username,
password = _state$configuration4.password,
authType = _state$configuration4.authType;
var _state$configuration3 = state.configuration,
baseUrl = _state$configuration3.baseUrl,
username = _state$configuration3.username,
password = _state$configuration3.password,
authType = _state$configuration3.authType;

var url = baseUrl ? baseUrl + path : path;

Expand All @@ -287,11 +272,7 @@ function patch(path, params, callback) {
authentication = _expandReferences4.authentication,
body = _expandReferences4.body;

var auth = authentication || {
'username': username,
'password': password,
'sendImmediately': authType != 'digest'
};
var auth = (0, _Utils.setAuth)(state.configuration, authentication);

return (0, _Client.req)("PATCH", { url: url, query: query, body: body, auth: auth, headers: headers }).then(function (response) {
var nextState = (0, _languageCommon.composeNextState)(state, response);
Expand Down Expand Up @@ -323,11 +304,11 @@ function patch(path, params, callback) {
function del(path, params, callback) {

return function (state) {
var _state$configuration5 = state.configuration,
baseUrl = _state$configuration5.baseUrl,
username = _state$configuration5.username,
password = _state$configuration5.password,
authType = _state$configuration5.authType;
var _state$configuration4 = state.configuration,
baseUrl = _state$configuration4.baseUrl,
username = _state$configuration4.username,
password = _state$configuration4.password,
authType = _state$configuration4.authType;

var url = baseUrl ? baseUrl + path : path;

Expand All @@ -337,11 +318,7 @@ function del(path, params, callback) {
authentication = _expandReferences5.authentication,
body = _expandReferences5.body;

var auth = authentication || {
'username': username,
'password': password,
'sendImmediately': authType != 'digest'
};
var auth = (0, _Utils.setAuth)(state.configuration, authentication);

return (0, _Client.req)("DELETE", { url: url, query: query, body: body, auth: auth, headers: headers }).then(function (response) {
var nextState = (0, _languageCommon.composeNextState)(state, response);
Expand Down
17 changes: 14 additions & 3 deletions lib/Utils.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
"use strict";
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.setAuth = setAuth;
exports.assembleError = assembleError;
exports.tryJson = tryJson;
function setAuth(configuration, manualAuth) {
if (manualAuth) return manualAuth;else if (configuration.username) return {
'username': configuration.username,
'password': configuration.password,
'sendImmediately': configuration.authType != 'digest'
};else return null;
}

function assembleError(_ref) {
var response = _ref.response,
error = _ref.error;

if ([200, 201, 202].indexOf(response.statusCode) > -1) return false;
if (response) {
if ([200, 201, 202].indexOf(response.statusCode) > -1) return false;
}
if (error) return error;
return new Error("Server responded with " + response.statusCode);
return new Error('Server responded with ' + response.statusCode);
}

function tryJson(data) {
Expand Down
33 changes: 7 additions & 26 deletions src/Adaptor.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/** @module Adaptor */
import { req } from './Client';
import { setAuth } from './Utils';
import {
execute as commonExecute,
expandReferences,
Expand Down Expand Up @@ -53,16 +54,12 @@ export function execute(...operations) {

return state => {

const { baseUrl, username, password, authType } = state.configuration;
const { baseUrl } = state.configuration;
const url = ( baseUrl ? baseUrl + path : path );

const { query, headers, authentication } = expandReferences(params)(state);

const auth = authentication || {
'username': username,
'password': password,
'sendImmediately': (authType != 'digest')
}
const auth = setAuth(state.configuration, authentication);

return req("GET", {url, query, auth, headers})
.then((response) => {
Expand Down Expand Up @@ -103,11 +100,7 @@ export function execute(...operations) {

const { query, headers, authentication, body } = expandReferences(params)(state);

const auth = authentication || {
'username': username,
'password': password,
'sendImmediately': (authType != 'digest')
}
const auth = setAuth(state.configuration, authentication);

return req("POST", {url, query, body, auth, headers})
.then((response) => {
Expand Down Expand Up @@ -148,11 +141,7 @@ export function put(path, params, callback) {

const { query, headers, authentication, body } = expandReferences(params)(state);

const auth = authentication || {
'username': username,
'password': password,
'sendImmediately': (authType != 'digest')
}
const auth = setAuth(state.configuration, authentication);

return req("PUT", {url, query, body, auth, headers})
.then((response) => {
Expand Down Expand Up @@ -192,11 +181,7 @@ export function patch(path, params, callback) {

const { query, headers, authentication, body } = expandReferences(params)(state);

const auth = authentication || {
'username': username,
'password': password,
'sendImmediately': (authType != 'digest')
}
const auth = setAuth(state.configuration, authentication);

return req("PATCH", {url, query, body, auth, headers})
.then((response) => {
Expand Down Expand Up @@ -235,11 +220,7 @@ export function del(path, params, callback) {

const { query, headers, authentication, body } = expandReferences(params)(state);

const auth = authentication || {
'username': username,
'password': password,
'sendImmediately': (authType != 'digest')
}
const auth = setAuth(state.configuration, authentication);

return req("DELETE", {url, query, body, auth, headers})
.then((response) => {
Expand Down
14 changes: 13 additions & 1 deletion src/Utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
export function setAuth(configuration, manualAuth) {
if ( manualAuth ) return manualAuth
else if ( configuration.username ) return {
'username': configuration.username,
'password': configuration.password,
'sendImmediately': ( configuration.authType != 'digest' )
}
else return null
}

export function assembleError({ response, error }) {
if ([200,201,202].indexOf(response.statusCode) > -1) return false;
if (response) {
if ([200,201,202].indexOf(response.statusCode) > -1) return false;
}
if (error) return error;
return new Error(`Server responded with ${response.statusCode}`)
}
Expand Down

0 comments on commit 6be64ed

Please sign in to comment.