1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
|
########################################################################
##
## Copyright (C) 2018-2026 The Octave Project Developers
##
## See the file COPYRIGHT.md in the top-level directory of this
## distribution or <https://octave.org/copyright/>.
##
## This file is part of Octave.
##
## Octave is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING. If not, see
## <https://www.gnu.org/licenses/>.
##
########################################################################
classdef weboptions < handle
## -*- texinfo -*-
## @deftypefn {} {@var{options} =} weboptions ()
## @deftypefnx {} {@var{options} =} weboptions (@var{name1}, @var{value1}, @dots{})
##
## Specify parameters for RESTful web services.
##
## When called with no inputs return a default @code{weboptions} object
## to specify parameters for a request to a web service. A @code{weboptions}
## object is an optional input argument to the @code{webread} and
## @code{webwrite} functions.
##
## Multiple name and value pair arguments may be specified in any order as
## @var{name1}, @var{value1}, @var{name2}, @var{value2}, etc.
##
## The option names must match @strong{exactly} one of those specified in the
## table below.
##
## The following options are available:
##
## @itemize @bullet
## @item
## @samp{CharacterEncoding} --- Specify the character encoding of the data:
##
## @samp{auto} (default), @samp{UTF-8}, @samp{US-ASCII}.
## @samp{auto} chooses an encoding based on the content-type of the data.
##
## @item
## @samp{UserAgent} --- Specify the User Agent for the connection.
##
## Default value is @samp{Octave/version}, where @samp{version} is the
## current version of Octave as returned by @code{version}.
##
## @item
## @samp{Timeout} --- Specify the timeout value for the connection in
## seconds.
##
## Default is 5 seconds. The special value @samp{Inf} sets the timeout to
## the maximum value of 2147.483647 seconds.
##
## @item
## @samp{Username} --- User identifier for a basic HTTP connection.
##
## Default is @qcode{''}. It must be a string.
##
## @item
## @samp{Password} --- User authentication password for HTTP connection.
##
## Default is @qcode{''}. It must be a string or character vector.
##
## Programming Note: If you display a @code{weboption} object with the
## Password property set, the value is displayed as a string containing
## @qcode{'*'}. However, the object stores the value of the Password
## property as plain text.
##
## @item
## @samp{KeyName} --- Specify the name of an additional key to be added to
## the HTTP request header.
##
## It must be a string or character vector. It should be coupled with
## @samp{KeyValue}.
##
## @item
## @samp{KeyValue} --- Specify the value of the key @samp{KeyName}.
##
## @samp{KeyName} must already be assigned in order to specify this field.
##
## @item
## @samp{@nospell{HeaderFields}} --- Specify header fields for the
## connection.
##
## Names and values of header fields, specified as an m-by-2 cell array of
## strings, to add to the HTTP request header.
## @code{@nospell{HeaderFields}@{i,1@}} is the name of a field and
## @code{@nospell{HeaderFields}@{i,2@}} is its value.
##
## @example
## @group
## weboptions ("HeaderFields",
## @{"Content-Length", "78" ;
## "Content-Type", "application/json"@})
## @end group
## @end example
##
## @noindent
## creates a weboptions object that contains two header fields:
## @code{Content-Length} with value @code{78} and @code{Content-Type} with
## value @code{application/json}.
##
## @item
## @samp{ContentType} --- Specify the content type of the data.
##
## The following values are available:
## @samp{auto}, @samp{text}, @samp{json}
##
## Default is @samp{auto}. It automatically determines the content type.
## All other formats like @samp{audio}, @samp{binary}, etc.@: available in
## @sc{matlab} are not currently supported.
##
## @item
## @samp{ContentReader} --- Not yet implemented. Only for @sc{matlab}
## compatibility.
##
## @item
## @samp{MediaType} --- Not yet implemented. Only for @sc{matlab}
## compatibility.
##
## @item
## @samp{RequestMethod} --- Specifies the type of request to be made.
##
## The following methods are available:
## @samp{get}, @samp{put}, @samp{post}, @samp{delete}, @samp{patch}
##
## @code{webread} uses the HTTP GET method. @code{webwrite} uses the HTTP
## POST method as default.
##
## @item
## @samp{ArrayFormat} -- Not yet implemented. Only for @sc{matlab}
## compatibility.
##
## @item
## @samp{CertificateFilename} --- Not yet implemented. Only for @sc{matlab}
## compatibility.
## @end itemize
##
## @seealso{webread, webwrite}
## @end deftypefn
properties
CharacterEncoding = "auto";
UserAgent = ["Octave/", version()];
Timeout = 5;
Username = "";
Password = "";
KeyName = "";
KeyValue = "";
HeaderFields = {};
ContentType = "auto";
ContentReader = "";
MediaType = "auto";
RequestMethod = "auto";
ArrayFormat = "csv";
CertificateFilename = "";
endproperties
methods
function f = weboptions (varargin)
if (rem (numel (varargin), 2) != 0)
error ("weboptions: invalid number of arguments");
elseif (numel (varargin) > 28)
error ("weboptions: invalid number of arguments");
endif
propnames = properties (f);
for i = 1:2:numel (varargin)
idx = find (strcmpi (varargin{i}, propnames), 1);
if (isempty (idx))
error ("weboptions: Undefined field '%s'", varargin{i});
endif
f.(propnames{idx}) = varargin{i+1};
endfor
endfunction
function f = set.CharacterEncoding (f, value)
## FIXME: Why validate this? There are many other possible encodings.
if (! any (strcmpi (value, {'auto', 'US-ASCII', 'UTF-8'})))
error ("weboptions: Invalid CharacterEncoding value");
endif
f.CharacterEncoding = value;
endfunction
function f = set.UserAgent (f, value)
if (! ischar (value) && ! isrow (value))
error ("weboptions: UserAgent must be a string");
endif
f.UserAgent = value;
endfunction
function f = set.Timeout (f, value)
if (! (isreal (value) && isscalar (value) && value > 0))
error ("weboptions: Timeout must be a real scalar > 0");
endif
if (value == Inf)
f.Timeout = 2147.483647;
else
f.Timeout = value;
endif
endfunction
function f = set.Username (f, value)
if (! ischar (value) && ! isrow (value))
error ("weboptions: Username must be a string");
endif
f.Username = value;
endfunction
function f = set.Password (f, value)
if (! ischar (value) && ! isrow (value))
error ("weboptions: Password must be a string");
endif
f.Password = value;
endfunction
function f = set.KeyName (f, value)
if (! ischar (value) && ! isrow (value))
error ("weboptions: invalid KeyName value");
endif
f.KeyName = value;
endfunction
function f = set.KeyValue (f, value)
if (isempty (f.KeyName) && ! isempty (value))
error ("weboptions: KeyName field empty. Cannot set KeyValue.");
endif
f.KeyValue = value;
endfunction
function f = set.HeaderFields (f, value)
if (! isempty (value))
if (! iscellstr (value))
error ("weboptions: HeaderFields must be a cell array of strings");
elseif (ndims (value) != 2 || columns (value) != 2)
error ("weboptions: HeaderFields must be of size m-by-2");
endif
endif
## C++ code requires row vector of "prop", "value" pairs.
f.HeaderFields = (value')(:)';
endfunction
function f = set.ContentType (f, value)
if (! isempty (value))
if (! any (strcmpi (value, {"auto", "json", "text"})))
error ("weboptions: invalid ContentType value");
endif
endif
f.ContentType = value;
endfunction
function f = set.ContentReader (f, value)
if (! is_function_handle (value))
error ("weboptions: ContentReader must be a function handle");
endif
## FIXME: Should emit a warning about unimplemented feature
f.ContentReader = value;
endfunction
function f = set.MediaType (f, value)
## FIXME: Should emit a warning about unimplemented feature
f.MediaType = value;
endfunction
function f = set.RequestMethod (f, value)
if (! isempty (value))
if (! any (strcmpi (value,
{"auto", "get", "put", "post", "delete", "patch"})))
error ("weboptions: invalid RequestMethod value");
endif
endif
f.RequestMethod = value;
endfunction
function f = set.ArrayFormat (f, value)
if (! isempty (value))
if (! any (strcmpi (value, {"csv", "json", "php", "repeating"})))
error ("weboptions: invalid ArrayFormat value");
endif
endif
f.ArrayFormat = value;
endfunction
function f = set.CertificateFilename (f, value)
## FIXME: Should emit a warning about unimplemented feature
f.CertificateFilename = value;
endfunction
function disp (f)
Timeout = num2str (f.Timeout);
Password = repmat ("*", 1, numel (num2str (f.Password)));
if (! isempty (f.ContentReader))
ContentReader = disp (f.ContentReader);
else
ContentReader = "[]";
endif
if (! isempty (f.HeaderFields))
HeaderFields = ['{"', strjoin(f.HeaderFields, '", "'), '"}'];
else
HeaderFields = "{}";
endif
if (! isempty (f.KeyValue))
KeyValue = num2str (f.KeyValue);
else
KeyValue = "''";
endif
output = [" weboptions with properties:\n",...
"\n CharacterEncoding: '", f.CharacterEncoding, "'",...
"\n UserAgent: '", f.UserAgent, "'",...
"\n Timeout: " , Timeout,...
"\n Username: '", f.Username, "'",...
"\n Password: '", Password, "'",...
"\n KeyName: '", f.KeyName, "'",...
"\n KeyValue: " , KeyValue,...
"\n ContentType: '", f.ContentType, "'",...
"\n ContentReader: " , ContentReader,...
"\n MediaType: '", f.MediaType, "'",...
"\n RequestMethod: '", f.RequestMethod, "'",...
"\n ArrayFormat: '", f.ArrayFormat, "'",...
"\n HeaderFields: " , HeaderFields,...
"\n CertificateFilename: '", f.CertificateFilename "'"];
disp (output);
endfunction
endmethods
endclassdef
|