Skip to content

Commit

Permalink
Merge pull request #21 from manavo/api
Browse files Browse the repository at this point in the history
API started
  • Loading branch information
jbrooksuk committed Nov 24, 2014
2 parents a4eac4b + 7e6f627 commit 7384da4
Show file tree
Hide file tree
Showing 9 changed files with 458 additions and 3 deletions.
4 changes: 4 additions & 0 deletions app/config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@
'Illuminate\View\ViewServiceProvider',
'Illuminate\Workbench\WorkbenchServiceProvider',

'Dingo\Api\ApiServiceProvider',

),

/*
Expand Down Expand Up @@ -190,6 +192,8 @@
'Validator' => 'Illuminate\Support\Facades\Validator',
'View' => 'Illuminate\Support\Facades\View',

'API' => 'Dingo\Api\Facades\API'

),

);
153 changes: 153 additions & 0 deletions app/config/packages/dingo/api/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| API Vendor
|--------------------------------------------------------------------------
|
| Your vendor is used in the "Accept" request header and will be used by
| the consumers of your API. Typically this will be the name of your
| application or website.
|
*/

'vendor' => 'cachet',

/*
|--------------------------------------------------------------------------
| Default API Version
|--------------------------------------------------------------------------
|
| When a request is made to the API and no version is specified then it
| will default to the version specified here.
|
*/

'version' => 'v1',

/*
|--------------------------------------------------------------------------
| Default API Prefix
|--------------------------------------------------------------------------
|
| A default prefix to use for your API routes so you don't have to
| specify it for each group.
|
*/

'prefix' => null,

/*
|--------------------------------------------------------------------------
| Default API Domain
|--------------------------------------------------------------------------
|
| A default domain to use for your API routes so you don't have to
| specify it for each group.
|
*/

'domain' => null,

/*
|--------------------------------------------------------------------------
| Conditional Requests
|--------------------------------------------------------------------------
|
| Globally enable conditional requests so that an ETag header is added to
| any successful response. Subsequent requests will perform a check and
| will return a 304 Not Modified. This can also be enabled or disabled
| on certain groups or routes.
|
*/

'conditional_request' => true,

/*
|--------------------------------------------------------------------------
| Authentication Providers
|--------------------------------------------------------------------------
|
| The authentication providers that should be used when attempting to
| authenticate an incoming API request.
|
*/

'auth' => [
'basic' => function ($app) {
return new Dingo\Api\Auth\BasicProvider($app['auth']);
}
],

/*
|--------------------------------------------------------------------------
| Rate Limiting
|--------------------------------------------------------------------------
|
| Consumers of your API can be limited to the amount of requests they can
| make. You can configure the limit based on whether the consumer is
| authenticated or unauthenticated.
|
| The "limit" is the number of requests the consumer can make within a
| certain amount time which is defined by "reset" in minutes.
|
| By default rate limiting is disabled.
|
*/

'rate_limiting' => [

'authenticated' => [
'limit' => 0,
'reset' => 60
],

'unauthenticated' => [
'limit' => 0,
'reset' => 60
],

'exceeded' => 'API rate limit has been exceeded.'

],

/*
|--------------------------------------------------------------------------
| Response Transformer
|--------------------------------------------------------------------------
|
| Responses can be transformed so that they are easier to format. By
| default a Fractal transformer will be used to transform any
| responses prior to formatting. You can easily replace
| this with your own transformer.
|
*/

'transformer' => function ($app) {
$fractal = new League\Fractal\Manager;

return new Dingo\Api\Transformer\FractalTransformer($fractal);
},

/*
|--------------------------------------------------------------------------
| Response Formats
|--------------------------------------------------------------------------
|
| Responses can be returned in multiple formats by registering different
| response formatters. You can also customize an existing response
| formatter.
|
*/

'default_format' => 'json',

'formats' => [

'json' => new Dingo\Api\Http\ResponseFormat\JsonResponseFormat

]

];
18 changes: 18 additions & 0 deletions app/controllers/ApiController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

class ApiController extends Dingo\Api\Routing\Controller{

public function getComponents() {
return Component::all();
}

public function getComponent($id) {
$component = Component::find($id);
if ($component) {
return $component;
} else {
App::abort(404, 'Component not found');
}
}

}
12 changes: 11 additions & 1 deletion app/models/Component.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class Component extends Eloquent {
class Component extends Eloquent implements Dingo\Api\Transformer\TransformableInterface {
/**
* Looks up the human readable version of the status.
* @return string
Expand All @@ -26,4 +26,14 @@ public function getColorAttribute() {
case 4: return 'text-danger';
}
}

/**
* Get the transformer instance.
*
* @return mixed
*/
public function getTransformer()
{
return new ComponentTransformer();
}
}
9 changes: 9 additions & 0 deletions app/routes/api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

Route::api(['version' => 'v1', 'prefix' => 'api'], function()
{

Route::get('components', 'ApiController@getComponents');
Route::get('components/{id}', 'ApiController@getComponent');

});
1 change: 1 addition & 0 deletions app/start/global.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/transformers',
app_path().'/database/seeds',
app_path().'/filters',

Expand Down
16 changes: 16 additions & 0 deletions app/transformers/ComponentTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

class ComponentTransformer extends League\Fractal\TransformerAbstract {

public function transform(Component $component)
{
return [
'id' => (int) $component->id,
'name' => $component->name,
'description' => $component->description,
'status_id' => (int) $component->status,
'status' => $component->getHumanStatusAttribute(),
];
}

}
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
"type": "project",
"require": {
"laravel/framework": "4.2.*",
"guzzlehttp/guzzle": "4.*"
"guzzlehttp/guzzle": "4.*",
"dingo/api": "~0.6"
},
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/transformers",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
Expand Down
Loading

0 comments on commit 7384da4

Please sign in to comment.