Skip to content

Commit

Permalink
add endpoint for get pickersets
Browse files Browse the repository at this point in the history
  • Loading branch information
70ray committed Dec 1, 2024
1 parent 64d63e3 commit d90fa4c
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
14 changes: 14 additions & 0 deletions SETUP/tests/unittests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,20 @@ public function test_validate_text()
];
$this->assertEquals($expected, $response);
}

public function test_pickersets()
{
$project = $this->_create_project();
$path = "v1/projects/$project->projectid/pickersets";
$query_params = "";
$router = ApiRouter::get_router();
$_SERVER["REQUEST_METHOD"] = "GET";
$response = $router->route($path, $query_params);
$this->assertEquals("basic-latin", $response[0]["name"]);
$this->assertEquals("!", $response[0]["subsets"][3]["name"]);
$this->assertEquals("Punctuation", $response[0]["subsets"][3]["title"]);
$this->assertEquals(["0", "DIGIT ZERO"], $response[0]["subsets"][5]["rows"][0][0]);
}
}

// this mocks the function in index.php
Expand Down
60 changes: 60 additions & 0 deletions api/dp-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,66 @@ paths:
default:
$ref: '#/components/responses/UnexpectedError'

/projects/{projectid}/pickersets:
get:
tags:
- project
description: Gets pickersets for the project including utf8 character names
parameters:
- $ref: '#/components/parameters/projectid'
responses:
200:
description: picker data
content:
application/json:
schema:
type: array
items:
type: object
properties:
name:
type: string
subsets:
type: array
items:
type: object
properties:
name:
type: string
title:
type: string
rows:
type: array
items:
type: array
items:
type: array
items:
type: string
example:
- name: basic-latin
subsets:
- name: A-Z
title: A-Z
rows:
- [[A, LATIN CAPITAL LETTER A], [B, LATIN CAPITAL LETTER B]]
- [[a, LATIN SMALL LETTER A], [b, LATIN SMALL LETTER B]]
- name: ^
title: Letters with acute, grave, and circumflex
rows:
- [[Á, LATIN CAPITAL LETTER A WITH ACUTE]]

'400':
$ref: '#/components/responses/InvalidValue'
'401':
$ref: '#/components/responses/Unauthorized'
'404':
$ref: '#/components/responses/NotFound'
'429':
$ref: '#/components/responses/RateLimitExceeded'
default:
$ref: '#/components/responses/UnexpectedError'

/projects/{projectid}/pages/{pagename}:
put:
tags:
Expand Down
1 change: 1 addition & 0 deletions api/v1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ $router->add_route("GET", "v1/queues/:queueid/projects", "api_v1_queue_projects"

$router->add_route("PUT", "v1/projects/:projectid/checkout", "api_v1_project_checkout");
$router->add_route("PUT", "v1/projects/:projectid/validatetext", "api_v1_project_validatetext");
$router->add_route("GET", "v1/projects/:projectid/pickersets", "api_v1_project_pickersets");
$router->add_route("PUT", "v1/projects/:projectid/pages/:pagename", "api_v1_project_page");
$router->add_route("GET", "v1/projects/:projectid/pages/:pagename", "api_v1_project_page");

Expand Down
34 changes: 34 additions & 0 deletions api/v1_projects.inc
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,40 @@ function api_v1_project_validatetext($method, $data, array $query_params)
return $text_checker->analyse(receive_project_text_from_request_body('text'), $data[":projectid"]);
}

function api_v1_project_pickersets($method, $data, $query_params)
{
// expand the character ranges so that each character can have its name
$project = $data[":projectid"];
return expand_pickersets($project->get_pickersets());
}

function expand_pickersets($pickersets)
{
$expanded_pickersets = [];
foreach ($pickersets as $pickerset) {
$expanded_subsets = [];
foreach ($pickerset->get_subsets() as $subset_name => $codepoint_rows) {
$expanded_rows = [];
foreach ($codepoint_rows as $row) {
$chars = convert_codepoint_ranges_to_characters($row);
$char_titles = [];
foreach ($chars as $char) {
if (null == $char) {
$char_title = [null, null];
} else {
$char_title = [$char, utf8_character_name($char)];
}
$char_titles[] = $char_title;
}
$expanded_rows[] = $char_titles;
}
$expanded_subsets[] = ["name" => $subset_name, "title" => $pickerset->get_title($subset_name), "rows" => $expanded_rows];
}
$expanded_pickersets[] = ["name" => $pickerset->name, "subsets" => $expanded_subsets];
}
return $expanded_pickersets;
}

function api_v1_project_page(string $method, array $data, array $query_params)
{
global $pguser;
Expand Down

0 comments on commit d90fa4c

Please sign in to comment.