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

Remove redundant api call for retreiving metadata #204

Merged
merged 4 commits into from
Jun 7, 2023

Conversation

Casmo
Copy link
Contributor

@Casmo Casmo commented Mar 28, 2023

Why are we calling here $this->api->getMetadata() instead of directly the $this->inner->getMetadata()? It makes a unnecessary api call to the API.

Description

When I use the following code it will make an api request for each file:

$response = $this->api->file()->listFiles(1000);

foreach ($response->getResults() as $file) {
  dump($file->getMetadata()); // Results in an additional api call, while information is already available
}

Checklist

Why are we calling here `$this->api->getMetadata()` instead of directly the `$this->inner->getMetadata()`?
It makes a unnecessary api call to the API.
@rsedykh rsedykh requested a review from andrew72ru March 28, 2023 14:53
@Casmo
Copy link
Contributor Author

Casmo commented Apr 25, 2023

Bump, is there something I could do?

@rsedykh
Copy link
Member

rsedykh commented Apr 25, 2023

Hi! Sorry, we've missed this PR.

We keep API call because data can be change by some other actor. It's not your case, probably, but still it is the case for someone else.

Add getInner() function.
@Casmo
Copy link
Contributor Author

Casmo commented Apr 26, 2023

Thanks for the reply!

What about 6f6dbb1 so I could use the following code to retrieve the meta data without additional requests:

$extraMetadata = $file->getInner()->getMetadata();

@rsedykh
Copy link
Member

rsedykh commented Apr 26, 2023

In this case we think it's better to create a method that returns existing data. And additional method (not in FileInfoInterface, but in FileApi) that updates it.

We can't merge this change because it goes against current architecture and will confuse other library users. But it looks that it works for you.

@Casmo
Copy link
Contributor Author

Casmo commented Apr 26, 2023

I understand that using a getInner() is a bit strange in the current code. I however do not understand what you mean with adding an additional method in the FileApi. You you mean like the one in 3dea3ce? I do not like the name of the function (but won't mind using it like this), maybe you have a suggestion?

To add a little more context:
I want to use this in vormkracht10/flysystem-uploadcare that will be used quite a lot of packages. I don't want to overuse extend the api. See https://github.com/vormkracht10/flysystem-uploadcare/blob/main/src/UploadcareAdapter.php#L335 here.

@rsedykh
Copy link
Member

rsedykh commented Apr 27, 2023

Now I see where this question is come from. Thanks for the flysystem, btw!

Our PHP dev tries to protect current architecture and not introduces other approaches when it's possible not to. Or, even if we explain it in the docs, people will stumble upon it every time. If we accept your PR, we will ruin consistency. :-(

Why can't you use $file->getMetadata()? Or, if that is that must have, why not to implement these feature inside flysystem-uploadcare?

I'm sorry that we still can't really understand what stays in your way.

@Casmo
Copy link
Contributor Author

Casmo commented Apr 28, 2023

Now I see where this question is come from. Thanks for the flysystem, btw!

Our PHP dev tries to protect current architecture and not introduces other approaches when it's possible not to. Or, even if we explain it in the docs, people will stumble upon it every time. If we accept your PR, we will ruin consistency. :-(

Why can't you use $file->getMetadata()? Or, if that is that must have, why not to implement these feature inside flysystem-uploadcare?

I'm sorry that we still can't really understand what stays in your way.

Thanks for the reply!

FYI, $file->getMetadata() works fine and we could keep it the way it is. I just foresee problems when people start using this package in Laravel for example. If I want to list files and show for example the filesize, the Flysystem is requesting the getMetadata() attribute and thus requesting an additional API call. (This is a n+1 problem.)

@rsedykh
Copy link
Member

rsedykh commented Apr 28, 2023

When you get the list of files, you also get metadata right away, aren't you? https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/File/operation/filesList

(I'm sorry, I'm not a dev myself and don't know any of PHP, and I pass your messages to our PHP dev. But this I want to understand for myself. Because maybe we should improve our API).

@Casmo
Copy link
Contributor Author

Casmo commented Apr 28, 2023

When you get the list of files, you also get metadata right away, aren't you? https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/File/operation/filesList

Yes. When you get files (or a specific file) you will get the metadata, that is available in $file->inner. However, requesting the metadata with $file->getMetadata() will get the data from the api, instead of the inner object.

Here is an example that only used the official Uploadcare package:

$configuration = \Uploadcare\Configuration::create('<public_key>', '<secret_key>');
$api = new \Uploadcare\Api($configuration);

$fileInfo = $api->file()->fileInfo('<uuid>');

dump($fileInfo); // This variable already includes the metadata, see screenshot.

The result will be:
image

If I add the following code:

dump($fileInfo->getMetadata());

The result will be:
image

However. This is requested through an (extra) API call, instead of just accessing it from $this->inner->getMetadata(); 0da2c0e fixes that.

@andrew72ru
Copy link
Collaborator

Hi @Casmo

It sounds reasonable, but my concern is consistency. What if the metadata has been changed in the meantime? But still, it sounds very reasonable.

I think we should do this:

  • revert this PR to the first commit;
  • add the documentation chapter about how to get the fresh metadata for the file (it's simple; you just have to call getMetadata method from the MetadataApiInterface);

And then we can release it.

@markvaneijk
Copy link
Contributor

What can we do to release this? We're actually waiting for this, because we have 100.000 files and don't want to do 100.000 API calls (because we need the metadata per file).

@rsedykh
Copy link
Member

rsedykh commented May 23, 2023

Hi Mark. But you've already saw the answer three weeks ago.. We were waiting for your commits to release it. Or do you want us to do it? We thought it would be impolite to take it from you.

@Casmo
Copy link
Contributor Author

Casmo commented May 23, 2023

I'll fix it, soon!

@markvaneijk
Copy link
Contributor

I misunderstood the situation @rsedykh, @Casmo will fix this.

Use inner for metadata.
@Casmo
Copy link
Contributor Author

Casmo commented Jun 7, 2023

@rsedykh I changed the code to the original PR. Could you give me a direction on how you want me to update the documentation? The current documentation already gives the current metadata.

This is in the documentation (https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/File-metadata):

$configuration = Uploadcare\Configuration::create((string) $_ENV['UPLOADCARE_PUBLIC_KEY'], (string) $_ENV['UPLOADCARE_SECRET_KEY']);

$api = (new Uploadcare\Api($configuration))->file();
$fileInfo = $api->fileInfo('1bac376c-aa7e-4356-861b-dd2657b5bfd2');
echo \sprintf("File %s metadata:\n", $fileInfo->getUuid());
foreach ($fileInfo->getMetadata() as $metaKey => $metaItem) {
    echo \sprintf("%s: %s\n", $metaKey, $metaItem);
}

@rsedykh
Copy link
Member

rsedykh commented Jun 7, 2023

Hi. We've checked the code, it works.

I wasn't talking about that documentation, I was just wondering if we have to change something in README.MD. But probably not.

I'll accept this PR today and finish everything else (e.g. CHANGELOG.MD) and bump the lib version.

@rsedykh rsedykh merged commit 9249f6d into uploadcare:master Jun 7, 2023
@rsedykh
Copy link
Member

rsedykh commented Jun 8, 2023

https://github.com/uploadcare/uploadcare-php/releases/tag/v4.1.1

@Casmo Casmo deleted the patch-1 branch June 9, 2023 08:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants