From 1609ed54199769f7e03b19c2201039799cf2434f Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Sat, 23 Jun 2018 15:41:22 -0400 Subject: [PATCH] import apis --- src/abstractions/api.service.ts | 4 ++ src/models/request/collectionRequest.ts | 9 ++++ src/models/request/importCiphersRequest.ts | 9 ++++ .../importOrganizationCiphersRequest.ts | 9 ++++ src/models/request/kvpRequest.ts | 9 ++++ src/services/api.service.ts | 44 +++++++++++++++++++ 6 files changed, 84 insertions(+) create mode 100644 src/models/request/collectionRequest.ts create mode 100644 src/models/request/importCiphersRequest.ts create mode 100644 src/models/request/importOrganizationCiphersRequest.ts create mode 100644 src/models/request/kvpRequest.ts diff --git a/src/abstractions/api.service.ts b/src/abstractions/api.service.ts index 4267fbbdf2..fe8600ec38 100644 --- a/src/abstractions/api.service.ts +++ b/src/abstractions/api.service.ts @@ -9,7 +9,9 @@ import { CipherShareRequest } from '../models/request/cipherShareRequest'; import { EmailRequest } from '../models/request/emailRequest'; import { EmailTokenRequest } from '../models/request/emailTokenRequest'; import { FolderRequest } from '../models/request/folderRequest'; +import { ImportCiphersRequest } from '../models/request/importCiphersRequest'; import { ImportDirectoryRequest } from '../models/request/importDirectoryRequest'; +import { ImportOrganizationCiphersRequest } from '../models/request/importOrganizationCiphersRequest'; import { PasswordHintRequest } from '../models/request/passwordHintRequest'; import { PasswordRequest } from '../models/request/passwordRequest'; import { PasswordVerificationRequest } from '../models/request/passwordVerificationRequest'; @@ -56,6 +58,8 @@ export abstract class ApiService { putShareCiphers: (request: CipherBulkShareRequest) => Promise; putCipherCollections: (id: string, request: CipherCollectionsRequest) => Promise; postPurgeCiphers: (request: PasswordVerificationRequest) => Promise; + postImportCiphers: (request: ImportCiphersRequest) => Promise; + postImportOrganizationCiphers: (request: ImportOrganizationCiphersRequest) => Promise; postCipherAttachment: (id: string, data: FormData) => Promise; deleteCipherAttachment: (id: string, attachmentId: string) => Promise; postShareCipherAttachment: (id: string, attachmentId: string, data: FormData, diff --git a/src/models/request/collectionRequest.ts b/src/models/request/collectionRequest.ts new file mode 100644 index 0000000000..38e62a449f --- /dev/null +++ b/src/models/request/collectionRequest.ts @@ -0,0 +1,9 @@ +import { Collection } from '../domain/collection'; + +export class CollectionRequest { + name: string; + + constructor(collection: Collection) { + this.name = collection.name ? collection.name.encryptedString : null; + } +} diff --git a/src/models/request/importCiphersRequest.ts b/src/models/request/importCiphersRequest.ts new file mode 100644 index 0000000000..40f073d5f2 --- /dev/null +++ b/src/models/request/importCiphersRequest.ts @@ -0,0 +1,9 @@ +import { CipherRequest } from './cipherRequest'; +import { FolderRequest } from './folderRequest'; +import { KvpRequest } from './kvpRequest'; + +export class ImportCiphersRequest { + ciphers: CipherRequest[]; + folders: FolderRequest[]; + folderRelationships: Array>; +} diff --git a/src/models/request/importOrganizationCiphersRequest.ts b/src/models/request/importOrganizationCiphersRequest.ts new file mode 100644 index 0000000000..3a63693235 --- /dev/null +++ b/src/models/request/importOrganizationCiphersRequest.ts @@ -0,0 +1,9 @@ +import { CipherRequest } from './cipherRequest'; +import { CollectionRequest } from './collectionRequest'; +import { KvpRequest } from './kvpRequest'; + +export class ImportOrganizationCiphersRequest { + ciphers: CipherRequest[]; + collections: CollectionRequest[]; + collectionRelationships: Array>; +} diff --git a/src/models/request/kvpRequest.ts b/src/models/request/kvpRequest.ts new file mode 100644 index 0000000000..0611a4e01e --- /dev/null +++ b/src/models/request/kvpRequest.ts @@ -0,0 +1,9 @@ +export class KvpRequest { + key: TK; + value: TV; + + constructor(key: TK, value: TV) { + this.key = key; + this.value = value; + } +} diff --git a/src/services/api.service.ts b/src/services/api.service.ts index de68749643..c03025c281 100644 --- a/src/services/api.service.ts +++ b/src/services/api.service.ts @@ -15,7 +15,9 @@ import { CipherShareRequest } from '../models/request/cipherShareRequest'; import { EmailRequest } from '../models/request/emailRequest'; import { EmailTokenRequest } from '../models/request/emailTokenRequest'; import { FolderRequest } from '../models/request/folderRequest'; +import { ImportCiphersRequest } from '../models/request/importCiphersRequest'; import { ImportDirectoryRequest } from '../models/request/importDirectoryRequest'; +import { ImportOrganizationCiphersRequest } from '../models/request/importOrganizationCiphersRequest'; import { PasswordHintRequest } from '../models/request/passwordHintRequest'; import { PasswordRequest } from '../models/request/passwordRequest'; import { PasswordVerificationRequest } from '../models/request/passwordVerificationRequest'; @@ -607,6 +609,48 @@ export class ApiService implements ApiServiceAbstraction { } } + async postImportCiphers(request: ImportCiphersRequest): Promise { + const authHeader = await this.handleTokenState(); + const response = await fetch(new Request(this.apiBaseUrl + '/ciphers/import', { + body: JSON.stringify(request), + cache: 'no-cache', + credentials: this.getCredentials(), + headers: new Headers({ + 'Accept': 'application/json', + 'Content-Type': 'application/json; charset=utf-8', + 'Authorization': authHeader, + 'Device-Type': this.deviceType, + }), + method: 'POST', + })); + + if (response.status !== 200) { + const error = await this.handleError(response, false); + return Promise.reject(error); + } + } + + async postImportOrganizationCiphers(request: ImportOrganizationCiphersRequest): Promise { + const authHeader = await this.handleTokenState(); + const response = await fetch(new Request(this.apiBaseUrl + '/ciphers/import-organization', { + body: JSON.stringify(request), + cache: 'no-cache', + credentials: this.getCredentials(), + headers: new Headers({ + 'Accept': 'application/json', + 'Content-Type': 'application/json; charset=utf-8', + 'Authorization': authHeader, + 'Device-Type': this.deviceType, + }), + method: 'POST', + })); + + if (response.status !== 200) { + const error = await this.handleError(response, false); + return Promise.reject(error); + } + } + // Attachments APIs async postCipherAttachment(id: string, data: FormData): Promise {