-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Изменение внутренней логики создания экземпляра клиента
- Loading branch information
Showing
1 changed file
with
58 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,60 @@ | ||
/** | ||
* MoyskladClient | ||
* Date: 11.01.14 | ||
* Vitaliy V. Makeev ([email protected]) | ||
*/ | ||
|
||
var clientStamp = require('./client'), | ||
query = require('./rest-clients/ms-xml/query'), | ||
tools = require('project/tools'), | ||
logger = require('project/logger'), | ||
Auth = require('project/auth'), | ||
pkg = require('../../package'); | ||
|
||
var xmlClientStamp = require('../rest-clients/ms-xml'), | ||
jsonServicesClientStamp = require('../rest-clients/json'); | ||
|
||
logger.info('moysklad-client v' + pkg.version); | ||
var _ = require('lodash') | ||
var have = require('project/have') | ||
var tools = require('project/tools') | ||
var logger = require('project/logger') | ||
var AuthStore = require('project/auth-store') | ||
var pkg = require('../../package') | ||
|
||
var resolverBehavior = require('project/behaviors/resolver') | ||
|
||
var Query = require('./rest-clients/ms-xml/query') | ||
var ClientBase = require('./client') | ||
var MsXmlClient = require('../rest-clients/ms-xml') | ||
var JsonClient = require('../rest-clients/json') // TODO Переименовать в json-services | ||
|
||
var Client = ClientBase | ||
.init(resolverBehavior) | ||
.init(function (options, context) { | ||
var authStore = new AuthStore() | ||
|
||
this.addDependency({ | ||
'logger': logger, | ||
'ms-xml': MsXmlClient().init(resolverBehavior).create(), | ||
'json-service': JsonClient().init(resolverBehavior).create(), | ||
'auth': authStore | ||
}) | ||
|
||
if (options.login) { | ||
authStore.setAuth(options.login, options.password) | ||
} | ||
}) | ||
|
||
logger.info('moysklad-client v' + pkg.version) | ||
|
||
module.exports = { | ||
|
||
createClient: function (login, password) { | ||
var auth = new Auth(login, password); | ||
|
||
var client = clientStamp(); | ||
client.setAuthStore(auth); | ||
|
||
var xmlClient = xmlClientStamp(); | ||
xmlClient.setAuthStore(auth); | ||
xmlClient.setProvider('logger', logger); | ||
xmlClient.options = client.options; | ||
|
||
var jsonServicesClient = jsonServicesClientStamp(); | ||
jsonServicesClient.setAuthStore(auth); | ||
jsonServicesClient.setProvider('logger', logger); | ||
jsonServicesClient.options = client.options; | ||
|
||
client.setProvider('ms-xml', xmlClient); | ||
client.setProvider('json-services', jsonServicesClient); | ||
client.setProvider('logger', logger); | ||
|
||
return client; | ||
}, | ||
|
||
createQuery: query.createQuery, | ||
tools: tools, | ||
logger: logger, | ||
version: pkg.version | ||
}; | ||
createClient: function () { | ||
var args = have(arguments, [ | ||
{}, | ||
{ login: 'string', password: 'string' }, | ||
{ options: 'object' } | ||
]) | ||
var options = {} | ||
|
||
if (args.options) { | ||
options = _.assign(options, args.options) | ||
} | ||
|
||
return Client.init(function () { | ||
if (args.login) { | ||
this.resolve('auth').setAuth(args.login, args.password) | ||
} | ||
}).create(options) | ||
}, | ||
|
||
Client: Client, | ||
|
||
createQuery: Query.createQuery, | ||
tools: tools, | ||
logger: logger, | ||
version: pkg.version | ||
} |