Coder Social home page Coder Social logo

stackexchangeapiclient's Introduction

stackExchange-logo Stack Exchange v2.2 API Client

PHP library for interacting with the Stack Exchange's version 2.2 REST API.

SensioLabsInsight Build Status Scrutinizer Code Quality Total Downloads Latest Stable Version Latest Unstable Version

Requirements

PHP >= 7.1

Installation

The easiest way to install this bundle is using Composer

$ composer require benatespina/stack-exchange-api-client

Usage

If you check out the API documentation, you will see that there are some calls that do not need authentication, but nevertheless there are other calls that need it so, this library allows to instantiate StackExchange client with or without Authentication. Furthermore there two ways to use this library.

Default usage

<?php

require_once __DIR__ . '/../vendor/autoload.php';

use BenatEspina\StackExchangeApiClient\StackExchange;

$client = StackExchange::withAuth('stack-exchange-key', 'stack-exchange-access-token');

$profile = $client->user()->me();
$answers = $client->answer()->answers();
$answer = $client->answer()->createAnswer('the-question-id', 'This is my awesome answer!');


$client = StackExchange::withoutAuth();

// Throws an AuthenticationIsRequired exception
$profile = $client->user()->me();

$answers = $client->answer()->answers();

// Throws an AuthenticationIsRequired exception
$answer = $client->answer()->createAnswer('the-question-id', 'This is my awesome answer!');

Full customized usage

<?php

require_once __DIR__ . '/../vendor/autoload.php';

use BenatEspina\StackExchangeApiClient\Authentication\Authentication;
use BenatEspina\StackExchangeApiClient\Http\GuzzleHttpClient;
use BenatEspina\StackExchangeApiClient\Model\Answer;
use BenatEspina\StackExchangeApiClient\Serializer\ToModelSerializer;
use BenatEspina\StackExchangeApiClient\StackExchange;

$httpClient = new GuzzleHttpClient();
$authentication = new Authentication('stack-exchange-key', 'stack-exchange-access-token');

$client = new StackExchange($httpClient, $authentication);

$profile = $client->user()->me();
$answers = $client->answer()->answers();
$answer = $client->answer()->createAnswer('the-question-id', 'This is my awesome answer!');


// CUSTOM SERIALIZATION
//
// Instantiate the AnswerApi with custom serializer
// The following calls returns an answer model instance instead of a plain array

$answersApi = $client->answer(new ToModelSerializer(Answer::class));
$answers = $answersApi->answers();
$answer = $answersApi->createAnswer('the-question-id', 'This is my awesome answer!');

Current status

The API has many methods, so the calls' implementation status are separated by type in the following files:

Tests

This bundle is completely tested by PHPSpec, SpecBDD framework for PHP.

Run the following command to launch tests:

$ vendor/bin/phpspec run -fpretty

Contributing

This bundle follows PHP coding standards, so pull requests need to execute the Fabien Potencier's PHP-CS-Fixer. Furthermore, if the PR creates some not-PHP file remember that you have to put the license header manually. In order to simplify we provide a Composer script that wraps all the commands related with this process.

$ composer run-script cs

There is also a policy for contributing to this bundle. Pull requests must be explained step by step to make the review process easy in order to accept and merge them. New methods or code improvements must come paired with PHPSpec tests.

Credits

This library is created by:

@benatespina - [email protected]

Licensing Options

License

stackexchangeapiclient's People

Contributors

benatespina avatar schmunk42 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

stackexchangeapiclient's Issues

NOTES

I decide to continue the V2 branch instead V3. The hexagonal architecture in this kind of libraries is an overkill, and it does not have any sense because the usage becomes more awful.

The following list is about of improvements to do in V2 branch:

  • Decouple API methods in classes: one method - one class
  • Make non-static the serialize method of Serializer and inject them via constructor in API methods instead of hardcoded.
  • Make model constructor public and remove arguments (call setters inside fromProperties factory method)
  • Make all the models implement the JsonSerializable interface

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.