Skip to content
This repository has been archived by the owner on Mar 11, 2024. It is now read-only.

codinglabsau/laravel-basecamp-api

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 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.

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.

This example app should help you get started: Basecamp Example App

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.

Usage

Retrieve your basecamp id, base uri (href), token and refresh token and initialize the API wrapper.

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

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

Packages

No packages published

Languages

  • PHP 100.0%