Skip to content

Commit

Permalink
Merge pull request #72 from srl295/ht-api-2
Browse files Browse the repository at this point in the history
support initial TR features
  • Loading branch information
srl295 authored Aug 1, 2017
2 parents 32f2eb4 + fdbdb52 commit 9a59eec
Show file tree
Hide file tree
Showing 14 changed files with 749 additions and 34 deletions.
75 changes: 75 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ API reference
<dd><p>ResourceEntry
Creating this object does not modify any data.</p>
</dd>
<dt><a href="#TranslationRequest">TranslationRequest</a></dt>
<dd></dd>
</dl>

## Members
Expand Down Expand Up @@ -215,6 +217,8 @@ params.credentials is required unless params.appEnv is supplied.</p>
* [.user(id)](#Client+user)[<code>User</code>](#User)
* [.users([opts], cb)](#Client+users)
* [.bundles([opts], cb)](#Client+bundles)
* [.tr(opts)](#Client+tr)[<code>TranslationRequest</code>](#TranslationRequest)
* [.trs([opts], cb)](#Client+trs)
* _inner_
* [~supportedTranslationsCallback](#Client..supportedTranslationsCallback) : <code>function</code>
* [~serviceInfoCallback](#Client..serviceInfoCallback) : <code>function</code>
Expand Down Expand Up @@ -343,6 +347,32 @@ bundle access objects.
| [opts] | <code>Object</code> | <code>{}</code> | options |
| cb | [<code>listBundlesCallback</code>](#Client..listBundlesCallback) | | given a map of Bundle objects |

<a name="Client+tr"></a>

### client.tr(opts) ⇒ [<code>TranslationRequest</code>](#TranslationRequest)
Create a Translation Request access object.
This doesn’t create the TR itself, just a handle object.
Call create() on the translation request to create it.

**Kind**: instance method of [<code>Client</code>](#Client)

| Param | Type | Description |
| --- | --- | --- |
| opts | <code>Object</code> | String (id) or map with options (for a new TR) |

<a name="Client+trs"></a>

### client.trs([opts], cb)
List Translation Requests. Callback is called with an map of
TR access objects.

**Kind**: instance method of [<code>Client</code>](#Client)

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [opts] | <code>Object</code> | <code>{}</code> | options |
| cb | <code>Client~listTranslationRequestsCallback</code> | | given a map of Bundle objects |

<a name="Client..supportedTranslationsCallback"></a>

### Client~supportedTranslationsCallback : <code>function</code>
Expand Down Expand Up @@ -804,6 +834,51 @@ Callback called by ResourceEntry~getInfo()
| err | <code>object</code> | error, or null |
| entry | [<code>ResourceEntry</code>](#ResourceEntry) | On success, the new or updated ResourceEntry object. |

<a name="TranslationRequest"></a>

## TranslationRequest
**Kind**: global class

* [TranslationRequest](#TranslationRequest)
* [new TranslationRequest()](#new_TranslationRequest_new)
* [.getInfo()](#TranslationRequest+getInfo)
* [.delete([opts], cb)](#TranslationRequest+delete)
* [.create([opts])](#TranslationRequest+create)

<a name="new_TranslationRequest_new"></a>

### new TranslationRequest()
This class represents a request for professional editing of machine-translated content.

<a name="TranslationRequest+getInfo"></a>

### translationRequest.getInfo()
Fetch the full record for this translation request.

**Kind**: instance method of [<code>TranslationRequest</code>](#TranslationRequest)
<a name="TranslationRequest+delete"></a>

### translationRequest.delete([opts], cb)
Delete this translation request.

**Kind**: instance method of [<code>TranslationRequest</code>](#TranslationRequest)

| Param | Type | Default |
| --- | --- | --- |
| [opts] | <code>Object</code> | <code>{}</code> |
| cb | <code>BasicCallBack</code> | |

<a name="TranslationRequest+create"></a>

### translationRequest.create([opts])
Create a translation request with the specified options

**Kind**: instance method of [<code>TranslationRequest</code>](#TranslationRequest)

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [opts] | <code>Object</code> | <code>{}</code> | Options object - if present, overrides values in this |

<a name="serviceRegex"></a>

## serviceRegex
Expand Down
2 changes: 2 additions & 0 deletions lib/cfenv-credsbylabel.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* see https://www.npmjs.com/package/cfenv#appenv-getservice-spec
* This is implemented by calling appEnv.getServices()
* - see https://www.npmjs.com/package/cfenv#appenv-getservices
* @ignore
*/
var getServiceByLabel =
module.exports.getServiceByLabel =
Expand All @@ -46,6 +47,7 @@ function getServiceByLabel(appEnv, regex) {
/**
* like getServiceCreds, but match the label
* see https://www.npmjs.com/package/cfenv#appenv-getservicecreds-spec
* @ignore
*/
var getServiceCredsByLabel =
module.exports.getServiceCredsByLabel =
Expand Down
87 changes: 58 additions & 29 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var utils = require('./utils.js');
var SwaggerClient = require('swagger-client');
var GpHmac = require('./gp-hmac');
var cfEnvUtil = require('./cfenv-credsbylabel');
const TranslationRequest = require('./tr.js');

/**
* Construct a g11n-pipeline client.
Expand Down Expand Up @@ -557,7 +558,7 @@ Bundle.prototype.getInfo = function getBundleInfo(opts, cb) {
data.bundle.metadata = data.bundle.metadata || {};
// Make this a date object and not a string.
if(data.bundle.updatedAt) {
data.bundle.updatedAt = new Date(data.bundle.updatedAt);
data.bundle.updatedAt = utils.datify(data.bundle.updatedAt);
}
// Copy these fields over. The REST getInfo call will not set them.
data.bundle.id = data.bundle.id || that.id;
Expand Down Expand Up @@ -971,7 +972,7 @@ function User(gp, props) {

// fixups:
if( this.updatedAt ) {
this.updatedAt = new Date(this.updatedAt);
this.updatedAt = utils.datify(this.updatedAt);
}
}

Expand Down Expand Up @@ -1086,7 +1087,7 @@ function ResourceEntry(bundle, props) {

// fixups:
if( this.updatedAt ) {
this.updatedAt = new Date(this.updatedAt);
this.updatedAt = utils.datify(this.updatedAt);
}
}

Expand Down Expand Up @@ -1146,6 +1147,54 @@ ResourceEntry.prototype.update = function resourceEntryUpdate(opts, cb) {
}, cb);
};

// -------- TR support --------

/**
* Create a Translation Request access object.
* This doesn’t create the TR itself, just a handle object.
* Call create() on the translation request to create it.
* @param {Object} opts - String (id) or map with options (for a new TR)
* @return {TranslationRequest}
*/
Client.prototype.tr = function translationRequest(opts) {
if(typeof(opts) === "string") {
opts = {id: opts};
}
return new TranslationRequest(this, opts);
};

// TODO: document filters, etc
/**
* List Translation Requests. Callback is called with an map of
* TR access objects.
*
* @param {Object} [opts={}] - options
* @param {Client~listTranslationRequestsCallback} cb - given a map of Bundle objects
*/
Client.prototype.trs = function listTranslationRequests(opts, cb) {
// make opts optional
if(!cb) {
cb = opts;
opts = {};
}
var serviceInstance = this.getServiceInstance(opts);
var that = this;
this.restCall('translation_request.getTranslationRequests',
{
serviceInstanceId: serviceInstance
}, function(err, data) {
if(err) return cb(err);
const trIds = Object.keys(data.translationRequests);
var trs = {};
for(var n in trIds) {
var trId = trIds[n];
trs[trId] = that.tr(data.translationRequests[trId]);
}
return cb(null, trs);
});
};


// -----------------------------

var assert = require('assert');
Expand Down Expand Up @@ -1196,30 +1245,10 @@ function addTrailing(str, chr) {
}
};

/**
* Copy all properties from props to o
* @param {object} o - target of properties
* @param {object} props - source of properties (map)
* @ignore
*/
function _copyProps(o, props) {
if ( props ) {
// copy properties to this
for(var k in props) {
o[k] = props[k];
}
}
}
// REDIRECT
// TODO: remove this by fixing call sites
const _copyProps = utils.copyProps;

/**
* Init a subsidiary client object from a Client
* @param {Object} o - client object to init
* @param {Client} gp - parent g11n-pipeline client object
* @param {Object} props - properties to inherit
* @ignore
*/
function _initSubObject(o, gp, props) {
_copyProps(o, props);
o.gp = gp; // actually Client
o.serviceInstance = gp.getServiceInstance(o); // get the service instance ID
}
// REDIRECT
// TODO: remove this by fixing call sites
const _initSubObject = utils.initSubObject;
1 change: 1 addition & 0 deletions lib/gp-hmac.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var crypto = require('crypto');

/**
* @author Steven R. Loomis
* @ignore
*/

var GaasHmac = function GaasHmac(name, user, secret) {
Expand Down
110 changes: 110 additions & 0 deletions lib/tr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright IBM Corp. 2017
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const utils = require('./utils.js');

/**
* This class represents a request for professional editing of machine-translated content.
* @class TranslationRequest
*/
function TranslationRequest(gp, props) {
utils.initSubObject(this, gp, props);
// turn some string dates into date objects
if(this.createdAt) {
this.createdAt = utils.datify(this.createdAt);
}
if(this.startedAt) {
this.startedAt = utils.datify(this.startedAt);
}
if(this.translatedAt) {
this.translatedAt = utils.datify(this.translatedAt);
}
if(this.mergedAt) {
this.mergedAt = utils.datify(this.mergedAt);
}
if(this.estimatedCompletion) {
this.estimatedCompletion = utils.datify(this.estimatedCompletion);
}
}

/**
* Fetch the full record for this translation request.
*/
TranslationRequest.prototype.getInfo = function getTranslationRequest(opts, cb) {
if( !cb ) {
cb = opts;
opts = {};
}

const serviceInstance = opts.serviceInstanceId || this.serviceInstance;
const requestId = opts.requestId || this.id;
const that = this;

this.gp.restCall("translation_request.getTranslationRequest", {serviceInstanceId: serviceInstance, requestId: requestId}, function(err, resp) {
if(err) return cb(err); // bail out, error.
const newTr = new TranslationRequest(that.gp, resp.translationRequest);
newTr.id = resp.id;
return cb(null, newTr);
});
};

/**
* Delete this translation request.
* @param {Object} [opts={}]
* @param {BasicCallBack} cb
*/
TranslationRequest.prototype.delete = function deleteTranslationRequest(opts, cb) {
if( !cb ) {
cb = opts;
opts = {};
}

const serviceInstance = opts.serviceInstanceId || this.serviceInstance;
const requestId = opts.requestId || this.id;
const that = this;

return this.gp.restCall("translation_request.deleteTranslationRequest", {serviceInstanceId: serviceInstance, requestId: requestId}, cb);
};

/**
* Create a translation request with the specified options
* @param {Object} [opts={}] - Options object - if present, overrides values in this
*/
TranslationRequest.prototype.create = function createTranslationRequest(opts, cb) {
if( !cb ) {
cb = opts;
opts = {};
}
const translationRequest = {};
utils.copyProps(translationRequest, this); // first, props from obj
utils.copyProps(translationRequest, opts); // then, extra opts

// clear out stuff
delete(translationRequest.serviceInstance);
delete(translationRequest.gp);

const that = this;

this.gp.restCall("translation_request.createTranslationRequest",
{serviceInstanceId: this.serviceInstance, body: translationRequest}, function(err, resp) {
if(err) return cb(err); // bail out, error.
const newTr = new TranslationRequest(that.gp, resp.translationRequest);
newTr.id = resp.id;
return cb(null, newTr);
});
};

module.exports = TranslationRequest;
Loading

0 comments on commit 9a59eec

Please sign in to comment.