Added new public endpoints to get languages and countries

GET api/public/v1/countries
GET api/public/v1/languages

Change-Id: I718d8578b62c64c25ba46dfafa0fac2803f8b54d
This commit is contained in:
Sebastian Marcet 2018-10-17 13:15:46 -03:00
parent 63a1083a93
commit 71e076e2a3
6 changed files with 230 additions and 3 deletions

View File

@ -0,0 +1,64 @@
<?php namespace App\Http\Controllers;
/**
* Copyright 2018 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use App\Models\Foundation\Main\CountryCodes;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Log;
use models\exceptions\EntityNotFoundException;
use models\exceptions\ValidationException;
use utils\PagingResponse;
/**
* Class CountriesApiController
* @package App\Http\Controllers
*/
final class CountriesApiController extends JsonController
{
/**
* @return mixed
*/
public function getAll(){
try {
$countries = [];
foreach(CountryCodes::$iso_3166_countryCodes as $iso_code => $name){
$countries[] = [
'iso_code' => $iso_code,
'name' => $name,
];
}
$response = new PagingResponse
(
count($countries),
count($countries),
1,
1,
$countries
);
return $this->ok($response->toArray($expand = Input::get('expand','')));
}
catch (ValidationException $ex1) {
Log::warning($ex1);
return $this->error412(array($ex1->getMessage()));
}
catch(EntityNotFoundException $ex2)
{
Log::warning($ex2);
return $this->error404(array('message'=> $ex2->getMessage()));
}
catch (\Exception $ex) {
Log::error($ex);
return $this->error500($ex);
}
}
}

View File

@ -0,0 +1,71 @@
<?php namespace App\Http\Controllers;
/**
* Copyright 2018 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use App\Models\Foundation\Main\Repositories\ILanguageRepository;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Log;
use models\exceptions\EntityNotFoundException;
use models\exceptions\ValidationException;
use utils\PagingResponse;
/**
* Class LanguagesApiController
* @package App\Http\Controllers
*/
final class LanguagesApiController extends JsonController
{
/**
* @var ILanguageRepository
*/
private $language_repository;
/**
* LanguagesApiController constructor.
* @param ILanguageRepository $language_repository
*/
public function __construct(ILanguageRepository $language_repository)
{
$this->language_repository = $language_repository;
}
/**
* @return mixed
*/
public function getAll(){
try {
$languages = $this->language_repository->getAll();
$response = new PagingResponse
(
count($languages),
count($languages),
1,
1,
$languages
);
return $this->ok($response->toArray($expand = Input::get('expand','')));
}
catch (ValidationException $ex1) {
Log::warning($ex1);
return $this->error412(array($ex1->getMessage()));
}
catch(EntityNotFoundException $ex2)
{
Log::warning($ex2);
return $this->error404(array('message'=> $ex2->getMessage()));
}
catch (\Exception $ex) {
Log::error($ex);
return $this->error500($ex);
}
}
}

View File

@ -13,9 +13,7 @@
**/
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Route;
// public api ( without AUTHZ [OAUTH2.0])
Route::group([
'namespace' => 'App\Http\Controllers',
'prefix' => 'api/public/v1',
@ -77,4 +75,15 @@ Route::group([
Route::get('', 'PublicCloudsApiController@getAll');
});
});
// countries
Route::group(array('prefix' => 'countries'), function () {
Route::get('', 'CountriesApiController@getAll');
});
// languages
Route::group(array('prefix' => 'languages'), function () {
Route::get('', 'LanguagesApiController@getAll');
});
});

View File

@ -21,6 +21,6 @@ final class LanguageSerializer extends SilverStripeSerializer
{
protected static $array_mappings = [
'Name' => 'name:json_string',
'IsoCode(' => 'iso_code:json_string',
'IsoCode' => 'iso_code:json_string',
];
}

View File

@ -0,0 +1,40 @@
<?php
/**
* Copyright 2018 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
final class CountriesApiTest extends TestCase
{
public function testGetAllCountries()
{
$params = [
];
$response = $this->action(
"GET",
"CountriesApiController@getAll",
$params,
[],
[],
[],
[]
);
$content = $response->getContent();
$countries = json_decode($content);
$this->assertTrue(!is_null($countries));
$this->assertResponseStatus(200);
}
}

View File

@ -0,0 +1,43 @@
<?php
/**
* Copyright 2018 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
/**
* Class LanguagesApiTest
*/
final class LanguagesApiTest extends TestCase
{
public function testGetAllLanguages()
{
$params = [
];
$response = $this->action(
"GET",
"LanguagesApiController@getAll",
$params,
[],
[],
[],
[]
);
$content = $response->getContent();
$languages = json_decode($content);
$this->assertTrue(!is_null($languages));
$this->assertResponseStatus(200);
}
}