Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Use built-in 'webread' and 'webwrite' (introduced in R2015a) instead …
…of vendoring 'urlread2'
  • Loading branch information
robertoffmoura committed Aug 13, 2024
commit 0b830477054136039b2e655a2222ebb719ac56c7
28 changes: 12 additions & 16 deletions plotly/plotly_aux/makecall.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
function st = makecall(args, origin, structargs)
function st = makecall(args, origin, kwargs)
% check if signed in and grab username, key, domain
[un, key, domain] = signin;

if isempty(un) || isempty(key)
error('Plotly:CredentialsNotFound',...
['It looks like you haven''t set up your plotly '...
Expand All @@ -13,21 +14,16 @@

platform = 'MATLAB';

args = m2json(args);
kwargs = m2json(structargs);
url = [domain '/clientresp'];
payload = {'platform', platform, 'version', plotly_version, ...
'args', args, 'un', un, 'key', key, 'origin', origin, ...
'kwargs', kwargs};

if (is_octave)
% use octave super_powers
resp = urlread(url, 'post', payload);
else
% do it matlab way
resp = urlread(url, 'Post', payload);
end
payload = struct( ...
platform=platform, ...
version=plotly_version, ...
args=args, ...
un=un, ...
key=key, ...
origin=origin, ...
kwargs=kwargs ...
);

st = jsondecode(resp);
response_handler(resp);
st = webwrite(url, payload);
end
47 changes: 12 additions & 35 deletions plotly/plotly_aux/plotlygenimage.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function plotlygenimage(figure_or_data, filename, varargin)
[pathstr, name, ext] = fileparts(filename);
[~, ~, ext] = fileparts(filename);
if nargin < 3
format = ext(2:length(ext));
else
Expand All @@ -23,46 +23,23 @@ function plotlygenimage(figure_or_data, filename, varargin)
figure = struct('data', data);
end

body = struct('figure', figure, 'format', format);

payload = m2json(body);
payload = struct(figure=figure, format=format);

[un, key, domain] = signin;

url = [domain, '/apigenimage/'];

headers = struct(...
'name',...
{...
'plotly-username',...
'plotly-apikey',...
'plotly-version',...
'plotly-platform',...
'user-agent'
},...
'value',...
{...
un,...
key,...
plotly_version,...
'MATLAB',...
'MATLAB'
});
% return the response as bytes -
% convert the bytes to unicode chars if the response fails or isn't
% (pdf, png, or jpeg) ... gnarly!
[response_string, extras] = urlread2(url, 'Post', payload, headers, ...
'CAST_OUTPUT', false);
if ( extras.status.value ~= 200 || ...
~(strcmp(extras.allHeaders.Content_Type, 'image/jpeg') || ...
strcmp(extras.allHeaders.Content_Type, 'image/png') || ...
strcmp(extras.allHeaders.Content_Type, 'application/pdf')))
response_string = native2unicode(response_string);
end
headerFields = [ ...
"plotly-username", string(un); ...
"plotly-apikey", string(key); ...
"plotly-version", plotly_version; ...
"plotly-platform", "MATLAB"; ...
"user-agent", "MATLAB" ...
];

response_object = webwrite(url, payload, weboptions("HeaderFields", headerFields));
image_data = response_object;

response_handler(response_string, extras);
image_data = response_string;

fileID = fopen(filename, 'w');
fwrite(fileID, image_data);
fclose(fileID);
Expand Down
29 changes: 8 additions & 21 deletions plotly/plotly_aux/plotlygetfile.m
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
function figure = plotlygetfile(file_owner, file_id)
[un, key, domain] = signin;

headers = struct(...
'name',...
{...
'plotly-username',...
'plotly-apikey',...
'plotly-version',...
'plotly-platform',...
'user-agent'
},...
'value',...
{...
un,...
key,...
plotly_version,...
'MATLAB',...
'MATLAB'
});

headerFields = [ ...
"plotly-username", string(un); ...
"plotly-apikey", string(key); ...
"plotly-version", plotly_version; ...
"plotly-platform", "MATLAB"; ...
"user-agent", "MATLAB" ...
];
url = [domain, '/apigetfile/', file_owner, '/', num2str(file_id)];

[response_string, extras] = urlread2(url, 'Get', '', headers);
response_handler(response_string, extras);
response_object = jsondecode(response_string);
response_object = webread(url, weboptions("HeaderFields", headerFields));
figure = response_object.payload.figure;
end
41 changes: 0 additions & 41 deletions plotly/plotly_aux/response_handler.m

This file was deleted.

11 changes: 0 additions & 11 deletions plotly/plotly_aux/urlread2/http_createHeader.m

This file was deleted.

62 changes: 0 additions & 62 deletions plotly/plotly_aux/urlread2/http_paramsToString.m

This file was deleted.

24 changes: 0 additions & 24 deletions plotly/plotly_aux/urlread2/license.txt

This file was deleted.

Loading