Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MATLAB generator #154

Merged
merged 29 commits into from Mar 9, 2020
Merged

Conversation

DainisGorbunovs
Copy link
Contributor

@DainisGorbunovs DainisGorbunovs commented Aug 18, 2019

MATLAB generator

It generates two variants of MATLAB code, where the variants use either:

  • web services (using webread, webwrite with weboptions)
  • HTTP interface (matlab.net.http.RequestMessage)

Generated output

Web services (webread/webwrite)

Many requests are possible, but not all. Supports only requests with get, post, put, delete, patch methods. Requests with a rich payload such as multiform data or form data are not possible. Binary and image payload is possible, however, is not recommended.

The generator creates the following variables:

  • params - query string parameters for the URL
  • cookies - n x 2 cell matrix with cookies
  • baseURI - URI without the query string
  • uri - constructed URI from params and baseURI
  • body - payload
    • it can be a character vector
    • or if it can be parsed as JSON, then it is stored as MATLAB structs
  • options - weboptions object

Then it calls either webread (if there is no body to send in the request) or webwrite, returns it as a response.

Alternatively, if the uri is constructed wrong, it shows fullURI given by cURL, and sends a request with this URI.

HTTP Interface (RequestMessage)

This is a more advanced API and allows to have higher freedom with requests.

The generator creates the following variables:

  • params - query string parameters for the URL
  • cookies - n x 2 cell matrix with cookies
  • header - column vector of headers, which is transposed into a row vector
  • uri - constructed URI from the base URI and params query parameters
  • body - payload, it can be a character vector

Then it calls either RequestMessage with the method, headers and body to the encoded URI uri.EncodedURI, returns it as a response.

Thanks / what I found helpful

Thanks, @NickCarneiro for the advice during this little project. Specifically, the below was helpful:

  • can use nc -l 3000, then in another tab curl -X POST http://localhost:3000 --data "hello=world" (thanks @NickCarneiro)
  • can use Wireshark to check
  • can use httpbin service

Findings

While trying to capture some cURL commands, I found that Safari may give a cURL command with -Xnull option. This means that Safari was unable to determine the request type. In order for any generator to return at least something, I am removing -Xnull option from parsed cURL command. See util.js#L36.

Limitations:

Below are the limitations I have found. They can be improved later in another pull request, should that be technically possible to achieve in MATLAB.

  • not following redirects
  • MATLAB structs can only contain keys starting with an alphabetic symbol, i.e. if we have __type, then we cannot store it as a key. Thus the only workaround seems to be to store everything as a character vector instead of a MATLAB structure.

@cf512, @NickCarneiro

@DainisGorbunovs
Copy link
Contributor Author

DainisGorbunovs commented Aug 18, 2019

The tests fail because I am using Node v11.10.0, but Travis uses v6.17.1 which does not support Object.entries (see https://node.green).
edit: I bumped up Travis to use Node v7. It uses v7.10.1 now.
edit 2: Bumped Node to v12, so that flatten() can be used.

@DainisGorbunovs

This comment has been minimized.

@DainisGorbunovs
Copy link
Contributor Author

Please squash and merge this pull request.

@DainisGorbunovs DainisGorbunovs marked this pull request as ready for review March 8, 2020 23:54
@NickCarneiro NickCarneiro merged commit 5cfedbf into curlconverter:master Mar 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants