277 lines
6.9 KiB
TypeScript
Raw Normal View History

2019-09-18 15:02:21 +09:00
import { Injectable, Inject } from '@angular/core';
2019-10-29 18:17:16 +09:00
import {
HttpClient,
HttpEventType,
HttpResponse,
2019-11-05 13:46:17 +09:00
HttpRequest
2019-10-29 18:17:16 +09:00
} from '@angular/common/http';
2019-09-18 15:02:21 +09:00
import { Observable, Subject } from 'rxjs';
2019-11-05 13:46:17 +09:00
import { map, filter } from 'rxjs/operators';
2019-09-18 15:02:21 +09:00
import { _MODULE_CONFIG } from '../types/token';
import { ModuleConfig } from '../types/module-config';
import {
FileProfileSaveRequest,
2019-09-20 11:39:09 +09:00
FileProfileSaveResponse,
encodeFileProfileSave,
decodeFileProfileSave
2019-10-08 15:42:36 +09:00
} from '../apis/file-profile-save';
2019-09-20 11:39:09 +09:00
import {
FileTalkDownloadRequest,
encodeFileTalkDownload
2019-10-08 15:42:36 +09:00
} from '../apis/file-talk-download';
2019-09-18 15:02:21 +09:00
import {
FileTalkSaveRequest,
2019-09-20 11:39:09 +09:00
FileTalkSaveResponse,
encodeFileTalkSave,
decodeFileTalkSave
2019-10-08 15:42:36 +09:00
} from '../apis/file-talk-save';
2019-09-18 15:02:21 +09:00
import {
FileTalkShareRequest,
2019-09-20 11:39:09 +09:00
FileTalkShareResponse,
encodeFileTalkShare,
decodeFileTalkShare
2019-10-08 15:42:36 +09:00
} from '../apis/file-talk-share';
2019-09-18 15:02:21 +09:00
import {
MassTalkDownloadRequest,
2019-09-20 11:39:09 +09:00
MassTalkDownloadResponse,
encodeMassTalkDownload,
decodeMassTalkDownload
2019-10-08 15:42:36 +09:00
} from '../apis/mass-talk-download';
2019-09-18 15:02:21 +09:00
import {
MassTalkSaveRequest,
2019-09-20 11:39:09 +09:00
MassTalkSaveResponse,
encodeMassTalkSave,
decodeMassTalkSave
2019-10-08 15:42:36 +09:00
} from '../apis/mass-talk-save';
2019-09-18 15:02:21 +09:00
import {
TransMassTalkDownloadRequest,
2019-09-20 11:39:09 +09:00
TransMassTalkDownloadResponse,
encodeTransMassTalkDownload,
decodeTransMassTalkDownload
2019-10-08 15:42:36 +09:00
} from '../apis/trans-mass-talk-download';
2019-09-18 15:02:21 +09:00
import {
TransMassTalkSaveRequest,
2019-09-20 11:39:09 +09:00
TransMassTalkSaveResponse,
encodeTransMassTalkSave,
decodeTransMassTalkSave
2019-10-08 15:42:36 +09:00
} from '../apis/trans-mass-talk-save';
2019-09-18 15:02:21 +09:00
import {
TranslationReqRequest,
2019-09-20 11:39:09 +09:00
TranslationReqResponse,
encodeTranslationReq,
decodeTranslationReq
2019-10-08 15:42:36 +09:00
} from '../apis/translation-req';
2019-09-18 15:02:21 +09:00
import {
TranslationSaveRequest,
2019-09-20 11:39:09 +09:00
TranslationSaveResponse,
encodeTranslationSave,
decodeTranslationSave
2019-10-08 15:42:36 +09:00
} from '../apis/translation-save';
2019-09-18 15:02:21 +09:00
@Injectable({
providedIn: 'root'
})
export class CommonApiService {
constructor(
@Inject(_MODULE_CONFIG) private moduleConfig: ModuleConfig,
private httpClient: HttpClient
) {}
public fileProfileSave(
2019-10-29 16:53:25 +09:00
req: FileProfileSaveRequest,
fileProfileSaveUrl?: string
2019-09-18 15:02:21 +09:00
): Observable<FileProfileSaveResponse> {
return this.httpClient
.post<any>(
2019-10-29 16:53:25 +09:00
!!fileProfileSaveUrl
? fileProfileSaveUrl
: this.moduleConfig.urls.fileProfileSave,
2019-09-18 15:02:21 +09:00
{},
{
2019-09-20 11:39:09 +09:00
params: encodeFileProfileSave(req)
2019-09-18 15:02:21 +09:00
}
)
2019-09-20 11:39:09 +09:00
.pipe(map(res => decodeFileProfileSave(res)));
2019-09-18 15:02:21 +09:00
}
2019-10-29 16:53:25 +09:00
public fileTalkDownload(
req: FileTalkDownloadRequest,
fileTalkDownloadUrl?: string
): Observable<Blob> {
const httpReq = new HttpRequest(
'POST',
2019-10-29 16:53:25 +09:00
!!fileTalkDownloadUrl
? fileTalkDownloadUrl
: this.moduleConfig.urls.fileTalkDownload,
encodeFileTalkDownload(req),
{ reportProgress: true, responseType: 'arraybuffer' }
);
let progress: Subject<number>;
if (!!req.fileDownloadItem) {
progress = req.fileDownloadItem.downloadStart();
}
return this.httpClient.request(httpReq).pipe(
filter(event => {
if (event instanceof HttpResponse) {
return true;
} else if (HttpEventType.DownloadProgress === event.type) {
if (!!progress) {
progress.next(Math.round((100 * event.loaded) / event.total));
}
}
return false;
}),
map((event: HttpResponse<any>) => {
if (!!progress) {
req.fileDownloadItem.downloadComplete();
}
return event.body;
})
2019-09-18 15:02:21 +09:00
);
}
public fileTalkSave(
2019-10-29 16:53:25 +09:00
req: FileTalkSaveRequest,
fileTalkSaveUrl?: string
2019-11-05 13:46:17 +09:00
): Observable<FileTalkSaveResponse> {
2019-10-29 18:17:16 +09:00
const httpReq = new HttpRequest(
'POST',
!!fileTalkSaveUrl ? fileTalkSaveUrl : this.moduleConfig.urls.fileTalkSave,
encodeFileTalkSave(req),
2019-11-05 13:46:17 +09:00
{ reportProgress: true, responseType: 'text' as 'json' }
2019-10-29 18:17:16 +09:00
);
2019-11-05 13:46:17 +09:00
const progress = req.fileUploadItem.uploadStart();
2019-10-29 18:17:16 +09:00
return this.httpClient.request(httpReq).pipe(
2019-11-05 13:46:17 +09:00
filter(event => {
2019-10-29 18:17:16 +09:00
if (event instanceof HttpResponse) {
2019-11-05 13:46:17 +09:00
return true;
2019-10-29 18:17:16 +09:00
} else if (HttpEventType.UploadProgress === event.type) {
2019-11-05 13:46:17 +09:00
progress.next(Math.round((100 * event.loaded) / event.total));
2019-09-18 15:02:21 +09:00
}
2019-11-05 13:46:17 +09:00
return false;
}),
map((event: HttpResponse<any>) => {
req.fileUploadItem.uploadComplete();
return decodeFileTalkSave(event.body);
2019-10-29 18:17:16 +09:00
})
);
2019-09-18 15:02:21 +09:00
}
2019-11-05 14:55:17 +09:00
public acceptableExtensionForFileTalk(extensions: string[]): boolean {
for (const extension of extensions) {
if (
-1 === this.moduleConfig.acceptableFileExtensions.indexOf(extension)
) {
return false;
}
}
return true;
}
2019-09-18 15:02:21 +09:00
public fileTalkShare(
req: FileTalkShareRequest
): Observable<FileTalkShareResponse> {
return this.httpClient
.post<any>(
this.moduleConfig.urls.fileTalkShare,
{},
{
2019-09-20 11:39:09 +09:00
params: encodeFileTalkShare(req)
2019-09-18 15:02:21 +09:00
}
)
2019-09-20 11:39:09 +09:00
.pipe(map(res => decodeFileTalkShare(res)));
2019-09-18 15:02:21 +09:00
}
public massTalkDownload(
req: MassTalkDownloadRequest
): Observable<MassTalkDownloadResponse> {
return this.httpClient
.post<any>(
this.moduleConfig.urls.massTalkDownload,
{},
{
2019-10-28 18:03:27 +09:00
params: encodeMassTalkDownload(req),
responseType: 'text' as 'json'
2019-09-18 15:02:21 +09:00
}
)
2019-09-20 11:39:09 +09:00
.pipe(map(res => decodeMassTalkDownload(res)));
2019-09-18 15:02:21 +09:00
}
public massTalkSave(
req: MassTalkSaveRequest
): Observable<MassTalkSaveResponse> {
return this.httpClient
.post<any>(
this.moduleConfig.urls.massTalkSave,
{},
{
2019-10-28 18:03:27 +09:00
params: encodeMassTalkSave(req),
responseType: 'text' as 'json'
2019-09-18 15:02:21 +09:00
}
)
2019-09-20 11:39:09 +09:00
.pipe(map(res => decodeMassTalkSave(res)));
2019-09-18 15:02:21 +09:00
}
public transMassTalkDownload(
req: TransMassTalkDownloadRequest
): Observable<TransMassTalkDownloadResponse> {
return this.httpClient
.post<any>(
this.moduleConfig.urls.transMassTalkDownload,
{},
{
2019-09-20 11:39:09 +09:00
params: encodeTransMassTalkDownload(req)
2019-09-18 15:02:21 +09:00
}
)
2019-09-20 11:39:09 +09:00
.pipe(map(res => decodeTransMassTalkDownload(res)));
2019-09-18 15:02:21 +09:00
}
public transMassTalkSave(
req: TransMassTalkSaveRequest
): Observable<TransMassTalkSaveResponse> {
return this.httpClient
.post<any>(
this.moduleConfig.urls.transMassTalkSave,
{},
{
2019-09-20 11:39:09 +09:00
params: encodeTransMassTalkSave(req)
2019-09-18 15:02:21 +09:00
}
)
2019-09-20 11:39:09 +09:00
.pipe(map(res => decodeTransMassTalkSave(res)));
2019-09-18 15:02:21 +09:00
}
public translationReq(
req: TranslationReqRequest
): Observable<TranslationReqResponse> {
return this.httpClient
.post<any>(
this.moduleConfig.urls.translationReq,
{},
{
2019-09-20 11:39:09 +09:00
params: encodeTranslationReq(req)
2019-09-18 15:02:21 +09:00
}
)
2019-09-20 11:39:09 +09:00
.pipe(map(res => decodeTranslationReq(res)));
2019-09-18 15:02:21 +09:00
}
public translationSave(
req: TranslationSaveRequest
): Observable<TranslationSaveResponse> {
return this.httpClient
.post<any>(
this.moduleConfig.urls.translationSave,
{},
{
2019-09-20 11:39:09 +09:00
params: encodeTranslationSave(req)
2019-09-18 15:02:21 +09:00
}
)
2019-09-20 11:39:09 +09:00
.pipe(map(res => decodeTranslationSave(res)));
2019-09-18 15:02:21 +09:00
}
}