Skip to content

Instantly share code, notes, and snippets.

@fernandojunior
Last active January 26, 2017 14:41
Show Gist options
  • Save fernandojunior/69e24b776d871e0d9c9c550ca033a71d to your computer and use it in GitHub Desktop.
Save fernandojunior/69e24b776d871e0d9c9c550ca033a71d to your computer and use it in GitHub Desktop.
api example
/api/file/:
post:
summary: Upload (create or update) a new file
consumes:
- multipart/form-data
produces:
- application/json
parameters:
- name: file
in: formData
description: File to be uploaded.
required: true
type: file
- name: auth
in: formData
description: Authentication details
required: true
schema:
- name: user
type: string
description: The user name
- name: password
type: string
description: The user password
- name: path
type: string
description: The main path of content repository
responses:
'201':
description: Created file information
schema:
- name: id
type: string
- name: version
type: string
- name: name
type: string
- name: length
type: int
- mimeType: length
type: string
/api/file/{id}:
post:
summary: Return a file based on ID
consumes:
- application/json
parameters:
- name: id
in: path
description: ID of file to download.
required: true
type: string
- name: version
description: A specific version of file
type: string
- name: auth
in: body
description: Authentication details.
required: true
schema:
- name: user
type: string
description: The user name
- name: password
type: string
description: The user password
- name: path
type: string
description: The main path of content repository
responses:
'200':
description: A file
schema:
type: file
/api/file/{id}:
delete:
summary: Delete a file based on ID
consumes:
- application/json
parameters:
- name: id
in: path
description: ID of file to delete.
required: true
type: string
- name: allVersions
description: Define whether only the latest version (false) or all versions (true) should be deleted
default: false
type: boolean
- name: auth
in: body
description: Authentication details.
required: true
schema:
- name: user
type: string
description: The user name
- name: password
type: string
description: The user password
- name: path
type: string
description: The main path of content repository
responses:
'200':
description: Deleted file

File Upload:

HTTP:

POST /api/file HTTP/1.1
Host: 192.168.24.83:8080
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
Cache-Control: no-cache
Postman-Token: 63c5cd27-47df-20eb-380a-144c39017b9b

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="hello.txt"
Content-Type: application/txt


------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="auth"
Content-Type: application/json

"{'user':'admin', 'password':'password', 'path':'/mapoteca'}"
------WebKitFormBoundary7MA4YWxkTrZu0gW--

Python:

import os
import json
import requests

url = 'http://localhost:8080/api/file'
auth = {'user':'<ALFRESCO_USER>', 'password':'<ALFRESCO_PASSWORD>', 'path':'/User Homes/<ALFRESCO_USER>'}

form = {
    'auth': (None, json.dumps(auth), 'application/json'),
    'file': ('hello.txt', open('hello.txt', 'rb'), 'text/plain')
}

requests.post(url=url, files=form).content

JavaScript:

const url = 'http://localhost:8080/api/file'
const auth = {'user':'<ALFRESCO_USER>', 'password':'<ALFRESCO_PASSWORD>', 'path':'/User Homes/<ALFRESCO_USER>'}

let form = new FormData()
form.append('auth', new Blob([JSON.stringify(auth)], {type: 'application/json'}))

form.append('file', new Blob(['Hello World'], {type: 'application/txt'}), 'hello.txt') // or
// form.append('file', document.forms[formName].file.files[0]) // from input type == 'file'

// let request = { url: url, method: 'POST', data: form }

let xhr = new XMLHttpRequest()
xhr.open( 'POST', url, true)
// xhr.setRequestHeader('Content-Type', 'multipart/form-data') // optional
xhr.onreadystatechange = function () {console.log(this.responseText)}
xhr.send(form)

Response example:

{"name":"hello.txt","length":11,"id":"837b14bb-4c24-4562-bf49-a243d621bf32","mimeType":"application/txt","version":"3.0"}

File Download example:

Python:

import os
import json

# download latest version or specific version
url = 'http://localhost:8080/api/file/f0f066fb-1b1e-4145-a02d-7968f5a3d507'
# url = 'http://localhost:8080/api/file/f0f066fb-1b1e-4145-a02d-7968f5a3d507?version=2.0'

auth = {'user':'<ALFRESCO_USER>', 'password':'<ALFRESCO_PASSWORD>', 'path':'/User Homes/<ALFRESCO_USER>'}

data = {
    'auth': auth,
}

requests.post(url=url, json=data).content

JavaScript:

// download latest version or a specific version
const url = 'http://localhost:8080/api/file/f0c3ebd7-869a-49c8-bfaa-c2868ccad7ae'
// const url = 'http://localhost:8080/api/file/f0f066fb-1b1e-4145-a02d-7968f5a3d507?version=2.0'

const auth = {'user':'<ALFRESCO_USER>', 'password':'<ALFRESCO_PASSWORD>', 'path':'/User Homes/<ALFRESCO_USER>'}

let data = {
    'auth': auth
}

setDownloadClickEvent = function(el, filename, contentType, content) {
    const uri = `data:${contentType};base64,${content}`
    downloadLink = el
    downloadLink.href = uri
    downloadLink.target   = '_blank'
    downloadLink.download = filename
    return downloadLink;
}

let xhr = new XMLHttpRequest()
xhr.open( 'POST', url)
xhr.setRequestHeader('Content-Type', 'application/json')
xhr.onreadystatechange = function () {
    // xhr.getAllResponseHeaders()
    // window.open("data:" + xhr.getResponseHeader('Content-Type') + ";base64," + this.response, '_blank')
    let link = setDownloadClickEvent(document.createElement('a'), 'test.pdf', xhr.getResponseHeader('Content-Type'), this.response)
    link.click()
}
xhr.send(JSON.stringify(data))

File remove example:

Python:

import os
import json

# remove only latest version or all versions
url = 'http://localhost:8080/api/file/f0f066fb-1b1e-4145-a02d-7968f5a3d507'  
# url = 'http://localhost:8080/api/file/f0f066fb-1b1e-4145-a02d-7968f5a3d507?allVersions=true'

auth = {'user':'<ALFRESCO_USER>', 'password':'<ALFRESCO_PASSWORD>', 'path':'/User Homes/<ALFRESCO_USER>'}

data = {
    'auth': auth,
}

requests.delete(url=url, json=data).content

JavaScript:

// remove only latest version or all versions
const url = 'http://localhost:8080/api/file/f0c3ebd7-869a-49c8-bfaa-c2868ccad7ae' 
// url = 'http://localhost:8080/api/file/f0f066fb-1b1e-4145-a02d-7968f5a3d507?allVersions=true'

const auth = {'user':'<ALFRESCO_USER>', 'password':'<ALFRESCO_PASSWORD>', 'path':'/User Homes/<ALFRESCO_USER>'}

let data = {
    'auth': auth
}

let xhr = new XMLHttpRequest()
xhr.open( 'DELETE', url)
xhr.setRequestHeader('Content-Type', 'application/json')
xhr.onreadystatechange = function () {console.log(this.responseText)}
xhr.send(JSON.stringify(data))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment