Skip to content

A Laravel API Wrapper for Basecamp3

License

Notifications You must be signed in to change notification settings

clickbar/laravel-basecamp-api

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Basecamp API

An API wrapper for Basecamp 3.

Prerequisites

Create an integration

Visit https://launchpad.37signals.com/integrations and register your app.

Add the credentials in your .env file as:

THIRTYSEVENSIGNALS_CLIENT_ID=your_client_id
THIRTYSEVENSIGNALS_CLIENT_SECRET=your_client_secret_key
THIRTYSEVENSIGNALS_REDIRECT_URI=http://localhost/login/basecamp/callback

And add the config variables to your config/services.php file:

    '37signals' => [
        'client_id' => env('THIRTYSEVENSIGNALS_CLIENT_ID'),
        'client_secret' => env('THIRTYSEVENSIGNALS_CLIENT_SECRET'),
        'redirect' => env('THIRTYSEVENSIGNALS_REDIRECT_URI')
    ],

OAuth2

The Basecamp3 API only supports 3-legged authentication and makes calls on behalf of one user. To receive an access token, you can use Laravel Socialite with the 37signals Socialite Driver. After you save the access token and the desired Basecamp3 account, you can now create an instance of the API wrapper.

composer require socialiteproviders/37signals

Add the socialite service provider to your config/app.php providers array key:

'providers' => [
    ...
    \SocialiteProviders\Manager\ServiceProvider::class,
]

Add the socialite event listener in your app/Providers/EventServiceProvider.php listen property:

protected $listen = [
    ...
    \SocialiteProviders\Manager\SocialiteWasCalled::class => [
        \SocialiteProviders\ThirtySevenSignals\ThirtySevenSignalsExtendSocialite::class.'@handle',
    ],
];

Installation

composer require coopbelvedere/laravel-basecamp-api
php artisan vendor:publish --provider="Belvedere\Basecamp\BasecampServiceProvider"

Add a user-agent to identify your app in your config/basecamp.php file. This is mandatory or any request will return a 400 error.

Usage

Retrieve your basecamp id, base uri (href), token and refresh token and initialize the API wrapper. Here's a basic example on the routes/web.php file to get you started.

Route::get('/login/basecamp', function () {
    return Socialite::driver('37signals')->redirect();
});

Route::get('/login/basecamp/callback', function () {
    $user = Socialite::driver('37signals')->user();

    Basecamp::init([
        'id' => $user->user['accounts'][0]['id'],
        'href' => $user->user['accounts'][0]['href'],
        'token' => $user->token,
        'refresh_token' => $user->refreshToken,
    ]);

    $projects = Basecamp::projects();
    dd($projects->index());
});

NOTE: You shouldn't initialize the API in the callback route, this is only to show you what data to keep from the socialite user. Save this data in the database, and/or the session to keep the connection active.

Caching

The client uses a Laravel Filesystem cache strategy by default. You can override this with your preferred Laravel cache store:

Basecamp::setCache(Cache::store('redis'));

Middlewares (optional)

You can optionally add an array of middlewares to the Guzzle Handler Stack. Here is an example for logging a request:

Basecamp::setMiddlewares([
    \GuzzleHttp\Middleware::log(
        Log::getLogger(),
        new \GuzzleHttp\MessageFormatter('{method} {uri} HTTP/{version} {req_body}')
    )
]);

Event Listener

The Client also comes with a middleware which will refresh an expired access token and automatically retry the intended endpoint. You can listen to the basecamp.refreshed_token event to update the access token in your app. The event returns 2 parameters, your basecamp user id and the new access token.

You can add something like this in your EventServiceProvider.php boot method:

Event::listen('basecamp.refreshed_token', function ($id, $token) {
    $user = \App\User::where('basecamp_id', $id)->first();
    $user->access_token = $token->access_token;
    $user->expires_at = \Carbon\Carbon::now()->addSeconds($token->expires_in);
    $user->save();
});

Pagination

Most collection of resources in Basecamp can be paginated.

// Get projects.
$projects = Basecamp::projects()->index();

// Get total of projects
$projects->total();

// Save the next page link for your next request
$nextPage = $projects->nextPage();

// Call the next page of projects.
$projects = Basecamp::projects()->index($nextPage);

Resources documentation

License

MIT

Copyright (c) 2017-present, Coopérative Belvédère Communication

About

A Laravel API Wrapper for Basecamp3

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%