Skip to content

Commit

Permalink
API spec
Browse files Browse the repository at this point in the history
  • Loading branch information
kohler committed Sep 13, 2024
1 parent 2c6977b commit 6ec7a53
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
8 changes: 5 additions & 3 deletions etc/apifunctions.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"name": "fieldhtml", "get": true,
"function": "Search_API::fieldhtml",
"parameters": "f ?aufull ?session q ?t ?sort",
"response": "fields data ?stat"
"response": "fields data ?stat ?classes"
},
{
"name": "follow", "paper": true, "post": true, "xxx": true,
Expand Down Expand Up @@ -208,12 +208,14 @@
{
"name": "reviewrating", "paper": true, "get": true,
"function": "Review_API::reviewrating",
"parameters": "r"
"parameters": "r",
"response": "?ratings ?user_rating"
},
{
"name": "reviewrating", "paper": true, "post": true,
"function": "Review_API::reviewrating",
"parameters": "r =user_rating"
"parameters": "r =user_rating",
"response": "?ratings ?user_rating"
},
{
"name": "reviewround", "paper": true, "post": true, "xxx": true,
Expand Down
46 changes: 23 additions & 23 deletions src/api/api_specvalidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ static function request($uf, Qrequest $qreq) {
} else if ($p[$i] === ":") {
$t |= self::F_SUFFIX;
$has_suffix = true;
} else if ($p[$i] === "*") {
$t &= ~self::F_REQUIRED;
break;
} else {
break;
}
Expand All @@ -41,8 +44,7 @@ static function request($uf, Qrequest $qreq) {
if (($t = self::lookup_type($n, $known, $has_suffix)) === null) {
if (!in_array($n, ["post", "base", "fn", "forceShow", "cap", "actas", "smsg", "_"])
&& ($n !== "p" || !($uf->paper ?? false))
&& ($n !== "redirect" || !($uf->redirect ?? false))
&& !isset($known["*"])) {
&& ($n !== "redirect" || !($uf->redirect ?? false))) {
self::error($qreq, "query param `{$n}` unknown");
}
} else if (($t & self::F_BODY) !== 0) {
Expand All @@ -51,9 +53,7 @@ static function request($uf, Qrequest $qreq) {
}
foreach (array_keys($_POST) as $n) {
if (($t = self::lookup_type($n, $known, $has_suffix)) === null) {
if (!isset($known["*"])) {
self::error($qreq, "post param `{$n}` unknown");
}
self::error($qreq, "post param `{$n}` unknown");
} else if (!isset($_GET[$n])
&& ($t & self::F_BODY) === 0) {
self::error($qreq, "post param `{$n}` should be in query");
Expand All @@ -62,9 +62,7 @@ static function request($uf, Qrequest $qreq) {
foreach (array_keys($_FILES) as $n) {
if (($t = self::lookup_type($n, $known, $has_suffix)) === null
|| ($t & (self::F_FILE | self::F_BODY)) === 0) {
if (!isset($known["*"])) {
self::error($qreq, "file param `{$n}` unknown");
}
self::error($qreq, "file param `{$n}` unknown");
}
}
foreach ($known as $n => $t) {
Expand Down Expand Up @@ -101,6 +99,9 @@ static function response($uf, Qrequest $qreq, JsonResult $jr) {
} else if ($p[$i] === ":") {
$t |= self::F_SUFFIX;
$has_suffix = true;
} else if ($p[$i] === "*") {
$t &= ~self::F_REQUIRED;
break;
} else {
break;
}
Expand Down Expand Up @@ -138,23 +139,22 @@ static function lookup_type($n, &$known, $has_suffix) {
$known[$n] |= self::F_PRESENT;
return $known[$n];
}
if (!$has_suffix) {
return null;
}
$colon = strpos($n, ":");
$slash = strpos($n, "/");
if ($colon === false || ($slash !== false && $colon > $slash)) {
$colon = $slash;
}
if ($colon !== false) {
$pfx = substr($n, 0, $colon);
$t = $known[$pfx] ?? 0;
if (($t & self::F_SUFFIX) !== 0) {
$known[$pfx] |= self::F_PRESENT;
return $t;
if ($has_suffix) {
$colon = strpos($n, ":");
$slash = strpos($n, "/");
if ($colon === false || ($slash !== false && $colon > $slash)) {
$colon = $slash;
}
if ($colon !== false) {
$pfx = substr($n, 0, $colon);
$t = $known[$pfx] ?? 0;
if (($t & self::F_SUFFIX) !== 0) {
$known[$pfx] |= self::F_PRESENT;
return $t;
}
}
}
return null;
return $known["*"] ?? null;
}

static function unparse_param_type($n, $t) {
Expand Down

0 comments on commit 6ec7a53

Please sign in to comment.