Skip to content

Commit

Permalink
add copy function
Browse files Browse the repository at this point in the history
  • Loading branch information
chregu committed Mar 28, 2018
1 parent 37f2bf7 commit 0230f16
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
25 changes: 25 additions & 0 deletions src/apis/sourceimages.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,31 @@ export default (state) => {
return state.request('POST', `sourceimages/${organization}/${hash}/restore`)
}

/**
* Copy image by hash to another org.
*
* ```js
* rokka.sourceimages.copy('myorg', 'c421f4e8cefe0fd3aab22832f51e85bacda0a47a', 'anotherorg', true)
* .then(function(result) {})
* .catch(function(err) {});
* ```
*
* @authenticated
* @param {string} organization the org the image is copied from
* @param {string} hash image hash
* @param {string} destinationOrganization the org the image is copied to
* @param {boolean} [overwrite = false] if an existing image should be overwritten
*
* @return {Promise}
*/
sourceimages.copy = (organization, hash, destinationOrganization, overwrite = true) => {
const headers = {'Destination': destinationOrganization}
if (overwrite === false) {
headers['Overwrite'] = 'F'
}
return state.request('COPY', `sourceimages/${organization}/${hash}`, null, null, {headers: headers})
}

/**
* ### Dynamic metadata
*
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ export default (config = {}) => {
request (method, path, payload = null, queryParams = null, options = {}) {
const uri = [state.apiHost, path].join('/')

const headers = {
'Api-Version': state.apiVersion
}
const headers = options.headers || {}

headers['Api-Version'] = state.apiVersion

if (options.noAuthHeaders !== true) {
if (!state.apiKey) {
Expand Down
32 changes: 32 additions & 0 deletions test/apis/sourceimages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,38 @@ test('sourceimages.restore', t => {
td.verify(requestStub(td.matchers.contains(expectedArgs), td.matchers.anything()))
})

test('sourceimages.copy', t => {
const rokka = rka({ apiKey: 'APIKEY' })

rokka.sourceimages.copy('myorg', 'c421f4e8cefe0fd3aab22832f51e85bacda0a47a', 'otherorg')

const expectedArgs = {
method: 'COPY',
uri: 'https://api.rokka.io/sourceimages/myorg/c421f4e8cefe0fd3aab22832f51e85bacda0a47a',
qs: null,
body: null,
headers: {'Api-Version': 1, 'Api-Key': 'APIKEY', 'Destination': 'otherorg'}
}

td.verify(requestStub(td.matchers.contains(expectedArgs), td.matchers.anything()))
})

test('sourceimages.copy with no overwrite', t => {
const rokka = rka({ apiKey: 'APIKEY' })

rokka.sourceimages.copy('myorg', 'c421f4e8cefe0fd3aab22832f51e85bacda0a47a', 'otherorg', false)

const expectedArgs = {
method: 'COPY',
uri: 'https://api.rokka.io/sourceimages/myorg/c421f4e8cefe0fd3aab22832f51e85bacda0a47a',
qs: null,
body: null,
headers: {'Api-Version': 1, 'Api-Key': 'APIKEY', 'Destination': 'otherorg', 'Overwrite': 'F'}
}

td.verify(requestStub(td.matchers.contains(expectedArgs), td.matchers.anything()))
})

test('sourceimages.deleteWithBinaryHash', t => {
const rokka = rka({ apiKey: 'APIKEY' })

Expand Down

0 comments on commit 0230f16

Please sign in to comment.