Giter VIP home page Giter VIP logo

laravel-basecamp-api's Introduction

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

laravel-basecamp-api's People

Contributors

mixislv avatar rehmatworks avatar stevethomas avatar vrodriguero avatar yuters avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.