Skip to content

Instantly share code, notes, and snippets.

View christeredvartsen's full-sized avatar

Christer Edvartsen christeredvartsen

View GitHub Profile
@christeredvartsen
christeredvartsen / README.md
Created September 25, 2020 09:12
Simple retry strategy for Guzzle 7 when encountering "429 Too Many Requests" errors

Simple retry strategy for Guzzle 7

APIs might implement rate limiting, and if they do your clients might experience 429 Too Many Requests responses with a Retry-After header, informing your client how long it should wait before making the next request. And being good internet citizens we should all implement support for this in our API clients.

Guzzle includes a retry middleware class that can be used to handle this.

The implementation in this gist is a PoC, so feel free to build upon it, and comment if you think something should be added / removed.

@christeredvartsen
christeredvartsen / action.php
Created November 29, 2019 07:49
Use output in a workflow step
<?= sprintf('::set-output name=var::%s', json_encode([
[
'id' => 'bar',
'name' => 'foo',
],
[
'id' => 'bar2',
'name' => 'foo2',
],
]));
@christeredvartsen
christeredvartsen / master.yml
Created November 21, 2019 18:33
Commit back to the repository from a workflow
name: Commit from workflow
on:
push:
branches:
- master
paths-ignore:
- file.txt
jobs:
build:
runs-on: ubuntu-latest
@christeredvartsen
christeredvartsen / master.yml
Last active November 14, 2019 14:17
Check rate limit in an action
name: Do some stuff
on: [push]
jobs:
test:
name: Do some stuff
runs-on: ubuntu-latest
steps:
- name: Check API rate limit
run: |
curl -s "https://api.github.com/rate_limit" -H "Authorization: token $GITHUB_TOKEN"
@christeredvartsen
christeredvartsen / app.php
Created September 13, 2019 10:35
Custom response class with `withCookies` helper method for Slim 4
<?php declare(strict_types=1);
namespace App;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\Psr7\Response as BaseResponse;
use Slim\Psr7\Cookies;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
@christeredvartsen
christeredvartsen / FeatureContext.php
Created April 4, 2018 13:52
Collect code coverage with imbo/behat-api-extension
<?php
use Imbo\BehatApiExtension\Context\ApiContext;
use Behat\Testwork\Hook\Scope\BeforeSuiteScope;
use Behat\Testwork\Hook\Scope\AfterSuiteScope;
use SebastianBergmann\CodeCoverage;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Middleware;
use Psr\Http\Message\RequestInterface;
class FeatureContext extends ApiContext {
@christeredvartsen
christeredvartsen / test.php
Last active April 3, 2017 17:03
MongoDuplicateKeyException from MongoClient
<?php
$client = new MongoClient();
$client->selectDB('test')->drop();
$collection = $client->selectCollection('test', 'image');
$collection->createIndex(
['user' => 1, 'imageIdentifier' => 1],
['unique' => true]
);
$collection->insert([
'user' => 'christer',
@christeredvartsen
christeredvartsen / config.php
Last active February 14, 2017 11:55
Build a free-form config definition with default values with Symfony\Component\Config
<?php
// $builder is an instance of Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition
$builder
->children()
->arrayNode('params')
->info('Arbitrary key => value configuration')
->useAttributeAsKey('name')
->prototype('scalar')->end()
->end()
->end();
@christeredvartsen
christeredvartsen / test.php
Created January 3, 2017 07:15
Use a callback for return values with a mock in PHPUnit
<?php
class SomeClass { public function someMethod() {} }
class SomeClassTest extends PHPUnit_Framework_TestCase {
public function testSomeMethod() {
$someValue = ' ... ';
$mock = $this->createMock('SomeClass');
$mock
->expects($this->once())
@christeredvartsen
christeredvartsen / guzzle.php
Created November 30, 2016 16:10
Add middleware to Guzzle-6 that removes itself after the first run
<?php
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Client;
use Psr\Http\Message\RequestInterface;
// Create a default stack
$stack = HandlerStack::create();
// Create the middleware