Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Added toInstance method to Http client to convert response to class object #52553

Closed

Conversation

andrey-helldar
Copy link
Contributor

@andrey-helldar andrey-helldar commented Aug 22, 2024

When using an Http client, you often need to generate an object from the response, so you have to do something like this:

public function some(): Some
{
    $data = Http::post($url)->json();
    
    return new Some($data);
}

This PR adds a new method to the response object - “toInstance” (I couldn't think of a better name 😅). This way we can improve the data return:

public function some(): Some
{
    return Http::post($url)->toInstance(Some::class);
}

In addition, it is possible to specify not only the name of the key, from where the data for translation will be taken, for example:

public function some(): Some
{
    return Http::post($url)->toInstance(Some::class, 'foo.bar');
}

But also use callback function for manual control if object formation is a non-standard process. This will allow the developer to choose how the transformation should take place. For example:

public function some($custom): Some
{
    return Http::post($url)->toInstance(
        fn (array $data) => new Some($data['foo'], $data['bar'], $custom)
    );
}

And, of course, you can specify the name of the key from which this data will be taken:

public function some($custom): Some
{
    return Http::post($url)->toInstance(
        fn (array $data) => new Some($data['foo'], $data['bar'], $custom),
        'foo.bar'
    );
}

@andrey-helldar andrey-helldar changed the title Added method to convert response to class object [11.x] Added method to convert response to class object Aug 22, 2024
@andrey-helldar andrey-helldar force-pushed the patch/2024-08-22/10-40 branch from 70eef85 to 0b31b5a Compare August 22, 2024 08:24
@andrey-helldar andrey-helldar force-pushed the patch/2024-08-22/10-40 branch from 0b31b5a to 80209da Compare August 22, 2024 08:25
@andrey-helldar andrey-helldar changed the title [11.x] Added method to convert response to class object [11.x] Added toInstance method to Http client to convert response to class object Aug 22, 2024
@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

@andrey-helldar andrey-helldar deleted the patch/2024-08-22/10-40 branch August 22, 2024 13:24
@andrey-helldar
Copy link
Contributor Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants