RAML for multiple files as output - mulesoft

I am new to RAML 1.0 I want to mention multiple csv files as response.
My API will be creating multiple csv file for different entities.
I want to mention this multiple file in response in RAML. Can you suggest what should be response for multiple files in RAML.

The body in HTTP requests and reponses is formed of one payload. You should use one zip, or a tar, tar.gz, etc, file to compress the files inside one file, or use a multi-part response to convey that each file is an attachement.
Example of a zip response:
#%RAML 1.0
title: test-attachment
/resource:
post:
responses:
200:
body:
application/zip:
Example of multi-part response:
#%RAML 1.0
title: test-attachment
/resource:
post:
responses:
200:
body:
multipart/form-data:
properties:
file1:
type: file
fileTypes: ['application/csv']
file2:
type: file
fileTypes: ['application/csv']

Related

Model form-data body request using Swagger 2.0 in yaml file

I am using swagger 2.0 and I have an endpoint which is using form-data as the payload for the request, I am using the same form-data body request in several places and I don't want to write it again and again (duplicate it) and I don't know how to model it inside my yaml file (I do know how to model object when it is part of the request body as payload).
This is the form-data payload I am trying to model:
I looked at swagger's documentation: https://swagger.io/docs/specification/2-0/file-upload/
and tried to do the same but I got errors as follow:
I don't want to duplicate the same values (if I do so it will work but the solution won't be elegant).
In OpenAPI 2.0, you can only reuse the definitions of individual form fields/parameters. There's no way to reuse a set of fields/parameters.
swagger: '2.0'
parameters:
name:
in: formData
name: name
type: string
upfile1:
in: formData
name: upfile1
type: file
...
paths:
/foo:
post:
consumes:
- multipart/form-data
parameters:
- $ref: '#/parameters/name'
- $ref: '#/parameters/upfile1'
- ...
/bar:
post:
consumes:
- multipart/form-data
parameters:
- $ref: '#/parameters/name'
- $ref: '#/parameters/upfile1'
- ...
To reuse the entire request body definition, you need OpenAPI 3.0. In OAS 3, request bodies can be defined in the global components/requestBodies section and referenced using $ref:
openapi: 3.0.0
paths:
/foo:
post:
requestBody:
$ref: '#/components/requestBodies/statusMode'
responses:
...
/bar:
post:
requestBody:
$ref: '#/components/requestBodies/statusMode'
responses:
...
components:
requestBodies:
statusMode:
required: true
content:
multipart/form-data:
schema:
type: object
# The properties correspond to individual form fields
properties:
name:
type: string
upfile1:
type: string
format: binary

Form Data: wrong Content-Type for .p7m files

I need to save a file with correct MimeType for .p7m files (application/pkcs7-mime) via form upload to the server.
In the request I noticed that Content-Type is wrong:
------WebKitFormBoundaryaglEgtBJlb65v7d5
Content-Disposition: form-data; name="file0"; filename="getmymimeplease.p7m"
Content-Type: application/pkcs7
it should be:
Content-Type: application/pkcs7-mime
How is possible that the '-mime' part is missing (or truncated) ?
This is usually controlled by OS and/or Browser. On windows, this is set in the registry, in HKEY_CLASSES_ROOT\.<fileextension>, e.g. HKEY_CLASSES_ROOT\.p7m, in the Field Content Type:
So in the end, this is controlled by the client. So if there are several possible mime types for the same extension, you need to cover that in your server code (accept or decline, convert to your default or not)

Creating reusable array definitions in swagger

I'm working on a definition/documentation-first specification of an API using Swagger 2.0. I've managed to break-out most reusable components into a definitions section however I am having trouble figuring out how to create reusable definitions for arrays of constants.
For example, I have a few paths that will return images, like this one:
paths:
/resource/{imageId}:
get:
produces:
- image/jpeg
- image/png
- image/gif
parameters:
- in: path
name: imageId
type: string
required: true
responses:
200:
description: Success
schema:
type: file
Which works just fine, but I would like to be able to define a reusable array of the values for the "produces" element so that I can reuse the same list for any path that will produce an image.
The following seemed like the intuitive approach, but swagger reports that the definition for imageMimeTypes is not valid:
paths:
/resource/{imageId}:
get:
produces:
$ref: "#/definitions/imageMimeTypes"
parameters:
- in: path
name: imageId
type: string
required: true
responses:
200:
description: Success
schema:
type: file
definitions:
imageMimeTypes:
- image/jpeg
- image/png
- image/gif
Is it possible to create a definition for an array like this? If so, what syntax should be used?
First of all, if these produces values are used in most operations, you can define them as global produces and override where needed.
produces:
- image/jpeg
- image/png
- image/gif
paths:
/resource/{imageId}:
get:
# Inherits global "produces"
...
/something:
get:
# Overrides global "produces"
produces:
- application/json
...
Your second example is not valid, because produces cannot have a $ref value. But you can use YAML anchors to achieve a similar effect. Note that an anchor must be defined before it is used, so you need to put the list above the path definition.
x-types:
imageMimeTypes: &IMAGE-MIME-TYPES
- image/jpeg
- image/png
- image/gif
paths:
/resource/{imageId}:
get:
produces: *IMAGE-MIME-TYPES
parameters:
- in: path
name: imageId
type: string
required: true
responses:
200:
description: Success
schema:
type: file
I put the list under an extension key x-types instead of definitions 1) because definitions is intended for input and output models and not random lists, and 2) to prevent the "unused definition" warning in the Swagger Editor.
This works in (at least) Swagger Editor and Swagger UI.

swagger editor does not generate correct request for file upload

I am using Swagger editor to build a post request to upload a file. My swagger json file is defined as follows.
swagger: '2.0'
info:
version: 0.0.0
title: '<enter your title>'
host: 'localhost:11235'
paths:
/form/upload:
post:
description: |
Post files.
consumes:
- multipart/form-data
produces:
- application/octet-stream
parameters:
- name: file
in: formData
description: upload file
required: true
type: file
- name: text
description: additional info
in: formData
type: string
responses:
'200':
description: Successful response
schema:
type: file
Then on the right side of the editor. I click the Try this operation. I select a file. The constructed request looks like this.
POST http://localhost:11235/form/upload HTTP/1.1
....
Content-Length: 40
Content-Type: multipart/form-data
file: C:\fakepath\swagger.json
text: 123
The file content is not transferred in the request. Only the file name is written into the request. Is this because swagger editor does not support file upload or my swagger json is wrong? Could anyone kindly help me with this? Thanks.
There is still an open issue for swagger-editor:
https://github.com/swagger-api/swagger-editor/issues/599
as reported it has been fixed in swagger-api:
https://github.com/swagger-api/swagger-ui/issues/838
Use swagger-ui for file uploading, it is working:
- in: formData
name: document
description: Documento Fiscal
required: true
type: file

Write variable to a file in Ansible

I am pulling JSON via the URI module and want to write the received content out to a file. I am able to get the content and output it to the debugger so I know the content has been received, but I do not know the best practice for writing files.
An important comment from tmoschou:
As of Ansible 2.10, The documentation for ansible.builtin.copy says:
If you need variable interpolation in copied files, use the
ansible.builtin.template module. Using a variable in the content
field will result in unpredictable output.
For more details see this and an explanation
Original answer:
You could use the copy module, with the content parameter:
- copy: content="{{ your_json_feed }}" dest=/path/to/destination/file
The docs here: copy module
Unless you are writing very small files, you should probably use templates.
Example:
- name: copy upstart script
template:
src: myCompany-service.conf.j2
dest: "/etc/init/myCompany-service.conf"
Based on Ramon's answer I run into an error. The problem where spaces in the JSON I tried to write I got it fixed by changing the task in the playbook to look like:
- copy:
content: "{{ your_json_feed }}"
dest: "/path/to/destination/file"
As of now I am not sure why this was needed. My best guess is that it had something to do with how variables are replaced in Ansible and the resulting file is parsed.
We can directly specify the destination file with the dest option now. In the below example, the output json is stored into the /tmp/repo_version_file
- name: Get repository file repo_version model to set ambari_managed_repositories=false
uri:
url: 'http://<server IP>:8080/api/v1/stacks/HDP/versions/3.1/repository_versions/1?fields=operating_systems/*'
method: GET
force_basic_auth: yes
user: xxxxx
password: xxxxx
headers:
"X-Requested-By": "ambari"
"Content-type": "Application/json"
status_code: 200
dest: /tmp/repo_version_file

Resources