deployer icon indicating copy to clipboard operation
deployer copied to clipboard

Httpie refactor

Open joanhey opened this issue 3 years ago • 3 comments

  • [x] Bug fix #…?

  • [x] New feature?

  • [x] BC breaks?

  • [ ] Tests added?

  • [ ] Docs added?

    Please, regenerate docs by running next command:
    $ php bin/docgen
    

Refactor Httpie Class

New features

public static function request(string $method, string $url, array $headers = []): Httpie

public function body(string $body, string $type = 'application/json'): Httpie

Now we can pass the Content-Type. By default application/json, like before. So it's possible to send: xml, csv, html, ...

public function getJson(bool $associative = true)

Now automatically add header Acept: application/json. And We can choose the return type. By default associative array, like before.

Catch errors in json_encode() and json_decode()

New way to catch errors. Before return an HttpieException in json_decode() errors. Now also with json_encode().

Add encoding to request

Fix

Duplicate query()

https://github.com/deployphp/deployer/pull/3098#issuecomment-1099370073

Extra bytes in request

BC breaks

Changed __construct() to private

It's a very small break, that will affect no code. This also permit to change later the __construct() without BC.

All the attributes are private. So this is impossible:

$http = new Httpie();
$http->method = 'POST';
$http->url = '.....';

Need to be always instanced by a static method. (Factory Method)

Happy coding !!! This info is in progress...

joanhey avatar Apr 14 '22 09:04 joanhey

The others PRs will need that you approve it or not.

joanhey avatar Apr 14 '22 09:04 joanhey

Fix query() before always add to the url. And it can be repeated the query. Example of fails:

$http = Htppie::get(....)->query(['hola']);

$http = $http->query[('bye']);

The URL will repeat the query ~'...?hola?bye'. Is the same if it's cloned or not.

joanhey avatar Apr 14 '22 16:04 joanhey

@antonmedv I'm sending the changes in small commits, so you can review it and comment in any change. Is It OK for now ? Because some commits depend from previous commits.

joanhey avatar Apr 16 '22 12:04 joanhey