Skip to content

Commit

Permalink
fixes #40
Browse files Browse the repository at this point in the history
  • Loading branch information
martinroob committed Jul 9, 2017
1 parent baac802 commit a0eb2e7
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@
"typescript": "2.3.1"
},
"dependencies": {
"@akanass/rx-http-request": "^2.3.0",
"chalk": "^2.0.1",
"commander": "^2.9.0",
"ngx-i18nsupport-lib": "^1.4.6",
"request": "^2.81.0",
"rxjs": "^5.4.2"
}
}
61 changes: 59 additions & 2 deletions src/autotranslate/auto-translate-service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {format} from 'util';
import * as request from 'request';
import {Observable} from 'rxjs';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import {RxHR} from "@akanass/rx-http-request";

/**
* Created by roobm on 03.07.2017.
* Low Level Service to call Google Translate.
Expand Down Expand Up @@ -43,14 +44,21 @@ interface TranslationsListResponse {
translations: TranslationsResource[];
}

interface InternalRequestResponse {
response: request.RequestResponse;
body: any;
}

const MAX_SEGMENTS = 128;

export class AutoTranslateService {

private _request: request.RequestAPI<request.Request, request.CoreOptions, request.RequiredUriUrl>;
_rootUrl: string;
_apiKey: string;

constructor(apiKey: string) {
this._request = request;
this._apiKey = apiKey;
this._rootUrl = 'https://translation.googleapis.com/';
}
Expand Down Expand Up @@ -134,7 +142,7 @@ export class AutoTranslateService {
json: true,
// proxy: 'http://127.0.0.1:8888' To set a proxy use env var HTTPS_PROXY
};
return RxHR.post(realUrl, options).map((data) => {
return this.post(realUrl, options).map((data) => {
const body: any = data.body;
if (!body) {
throw new Error('no result received');
Expand Down Expand Up @@ -171,4 +179,53 @@ export class AutoTranslateService {
}
return langLower;
}

/**
* Function to do a POST HTTP request
*
* @param uri {string}
* @param options {CoreOptions}
*
* @return {Observable<InternalRequestResponse>}
*/
post(uri: string, options?: request.CoreOptions): Observable<InternalRequestResponse> {
return <Observable<InternalRequestResponse>> this._call.apply(this, [].concat('post', <string> uri,
<request.CoreOptions> Object.assign({}, options || {})));
}

/**
* Function to do a HTTP request for given method
*
* @param method {string}
* @param uri {string}
* @param options {CoreOptions}
*
* @return {Observable<InternalRequestResponse>}
*
* @private
*/
private _call(method: string, uri: string, options?: request.CoreOptions): Observable<InternalRequestResponse> {
return <Observable<InternalRequestResponse>> Observable.create((observer) => {
// build params array
const params = [].concat(<string> uri, <request.CoreOptions> Object.assign({}, options || {}),
<RequestCallback>(error: any, response: request.RequestResponse, body: any) => {
if (error) {
return observer.error(error);
}

observer.next(<InternalRequestResponse> Object.assign({}, {
response: <request.RequestResponse> response,
body: <any> body
}));
observer.complete();
});

// _call request method
try {
this._request[<string> method].apply(<request.RequestAPI<request.Request, request.CoreOptions, request.RequiredUriUrl>> this._request, params);
} catch (error) {
observer.error(error);
}
});
}
}
2 changes: 2 additions & 0 deletions src/xliffmerge/xliff-merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {ProgramOptions, IConfigFile} from './i-xliff-merge-options';
import {NgxTranslateExtractor} from './ngx-translate-extractor';
import {TranslationMessagesFileReader} from './translation-messages-file-reader';
import {Observable} from 'rxjs';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import {XliffMergeAutoTranslateService} from '../autotranslate/xliff-merge-auto-translate-service';
import {AutoTranslateSummaryReport} from '../autotranslate/auto-translate-summary-report';

Expand Down
24 changes: 2 additions & 22 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@
# yarn lockfile v1


"@akanass/rx-http-request@^2.3.0":
version "2.3.0"
resolved "https://registry.yarnpkg.com/@akanass/rx-http-request/-/rx-http-request-2.3.0.tgz#3729707ef94a781723d6421848f5cd8c1dee5630"
dependencies:
"@types/node" "^7.0.18"
"@types/request" "^0.0.43"
request "^2.81.0"
rxjs "^5.4.0"

"@types/form-data@*":
version "0.0.33"
resolved "https://registry.yarnpkg.com/@types/form-data/-/form-data-0.0.33.tgz#c9ac85b2a5fd18435b8c85d9ecb50e6d6c893ff8"
Expand All @@ -21,21 +12,10 @@
version "2.5.47"
resolved "https://registry.yarnpkg.com/@types/jasmine/-/jasmine-2.5.47.tgz#bbba9bcf0e95e7890c6f4a47394e8bacaa960eb6"

"@types/node@*", "@types/node@^7.0.18":
version "7.0.33"
resolved "https://registry.yarnpkg.com/@types/node/-/node-7.0.33.tgz#ae3c53ad01d7e9d62c7f1a85c5f7500d59b9d25b"

"@types/node@^8.0.8":
"@types/node@*", "@types/node@^8.0.8":
version "8.0.8"
resolved "https://registry.yarnpkg.com/@types/node/-/node-8.0.8.tgz#0dc4ca2c6f6fc69baee16c5e928c4a627f517ada"

"@types/request@^0.0.43":
version "0.0.43"
resolved "https://registry.yarnpkg.com/@types/request/-/request-0.0.43.tgz#fcc59cfd88e63034e813c6884a0aade2d0f7e935"
dependencies:
"@types/form-data" "*"
"@types/node" "*"

"@types/request@^0.0.45":
version "0.0.45"
resolved "https://registry.yarnpkg.com/@types/request/-/request-0.0.45.tgz#c6e52be8b108eb035c35aa9af56a38a260c3e7e6"
Expand Down Expand Up @@ -1306,7 +1286,7 @@ rimraf@2, rimraf@^2.5.1, rimraf@^2.6.1:
dependencies:
glob "^7.0.5"

rxjs@^5.4.0, rxjs@^5.4.2:
rxjs@^5.4.2:
version "5.4.2"
resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-5.4.2.tgz#2a3236fcbf03df57bae06fd6972fd99e5c08fcf7"
dependencies:
Expand Down

0 comments on commit a0eb2e7

Please sign in to comment.