Coder Social home page Coder Social logo

Dynamic Headers about retrofit-php HOT 6 CLOSED

mloberg avatar mloberg commented on August 30, 2024
Dynamic Headers

from retrofit-php.

Comments (6)

natebrunette avatar natebrunette commented on August 30, 2024

Is it possible to use the event system for this?

from retrofit-php.

mloberg avatar mloberg commented on August 30, 2024

That solves the issue.

from retrofit-php.

MDXDave avatar MDXDave commented on August 30, 2024

Can someone please provide an example? @natebrunette @mloberg

from retrofit-php.

mloberg avatar mloberg commented on August 30, 2024

@MDXDave If you're using Guzzle, you would use the event system (v5) or middleware (v6) to modify the request directly. Check out the Guzzle docs.

See @natebrunette comment below. We ended up using Retrofit's event system because we didn't have to worry about what version of Guzzle we were using.

from retrofit-php.

natebrunette avatar natebrunette commented on August 30, 2024

@MDXDave I really need to update the docs, but you can also use the event system. Set a Symfony event dispatcher to the builder, then add a subscriber that listens for retrofit.beforeSend. You'll receive a PSR-7 request object on the event. Modify it, then set the new request object (because it's immutable) back to the event.

from retrofit-php.

MDXDave avatar MDXDave commented on August 30, 2024

Thanks for your answers. And yes, the docs are not completely 😼

Working solution for others with the same problem below.

$dispatcher = new EventDispatcher();
$dispatcher->addListener('retrofit.beforeSend', function (BeforeSendEvent $event) use(&$accessToken) {
      $request = $event->getRequest()->withAddedHeader("Authorization", "Bearer ".$accessToken);
      $event->setRequest($request);
});

return RestAdapter::builder()->setEventDispatcher($dispatcher)->setBaseUrl(....)->build()->create(...::class);

from retrofit-php.

Related Issues (20)

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.