At present, the module supports the Label Detection for a given image. The function establishing a connection with the API resides in a class Connector, which is being statically called inside the module.
Also, the present function uses curl.
Task:
1. Create service for the connecting functions, and use of containers.
2. Use of Drupal httpClient over curl.
3. Add functions for the remaining functionalities which the API supports, including Landmark Detection, Logo Detection, Text Detection, Safe Search Detection, Face Detection and Image Attributes.
(This is the first task of my GSOC project)
The last patch posted in the issue implements the following points:
1. Services have been created and implemented instead of static calls to the functions in the module.
2. curl has been replaced by httpClient service.
3. Functions for the remaining functionalities supported by the API have been added to the class.
4. Instead of concatenating strings in the data (which is to be sent to the API), we now use arrays.
5. Implementation of Json utility class and its static functions.
6. Service Connector has been replaced by service GoogleVisionAPI.
Comment | File | Size | Author |
---|---|---|---|
#39 | interdiff-2731801-31-39.txt | 802 bytes | ajalan065 |
#39 | Use-statement-and-static-keyword-added-2731801-8-39.patch | 11.2 KB | ajalan065 |
#31 | interdiff-2731801-25-31.txt | 3.8 KB | ajalan065 |
#31 | Moved-duplicate-code-to-another-function-2731801-8-31.patch | 11.16 KB | ajalan065 |
#29 | screenshot_317.png | 72.87 KB | eugene.ilyin |
Comments
Comment #2
ajalan065 CreditAttribution: ajalan065 at Google Summer of Code commentedHere's the patch of the progress.
Comment #3
penyaskitoI'm not super happy with the name of the service. Should we use
google_vision.api
?Neither with the name of the class. Please create another issue for renaming that one (to the same name we decide to use in the service)
No need to remove this comment for now.
Instead of concatening strings, let's use an array and use json_encode and json_decode.
We should use Drupal http_request service instead of curl natively.
That service should be injected in the constructor. See https://www.drupal.org/node/2133171
Please create a follow-up issue for injecting the configuration. Also, we shouldn't use
getEditable()
, that's only for config editing forms.Don't concatenate strings, let's use arrays and json_decode and json_encode. It's easier to read and less error prone.
Use http_client here again :)
Another one.
Use http client.
Again JSON by concatening string.
Use http_client.
There are more issues that I didn't comment on, but that had the same pattern that the issues above.
Comment #4
ajalan065 CreditAttribution: ajalan065 at Google Summer of Code commentedAs mentioned in the first point above, let us keep the name of the service as google_vision.api
And also renaming the class as Api.
I will create another issue for renaming the class name.
Comment #5
ajalan065 CreditAttribution: ajalan065 at Google Summer of Code commentedComment #6
naveenvalechaInterdiff please next time https://www.drupal.org/documentation/git/interdiff
Make the class name to be GooglevisionAPI.
Pass the @config.factory as arguments here so that you can inject it in constructor of the class.
Remove extra lines b/w use statements.
add a new line after protected property.
Use drupal httpClient over curl
Comment #7
ajalan065 CreditAttribution: ajalan065 at Google Summer of Code commentedThe patch with services used through container.
And also tried using httpClient() but error showing up
Comment #8
ajalan065 CreditAttribution: ajalan065 at Google Summer of Code commentedSorry for the extra lines and spaces above.
That was kept for clear view. I will remove them in subsequent patches.
Comment #9
penyaskito@ajalan065 Your patch doesn't apply to 8.x-1.x. Did you built this patch in top of other patches?
Comment #10
ajalan065 CreditAttribution: ajalan065 at Google Summer of Code commentedI built the patch on two commits. May be that is the reason. I am posting a new one.
Comment #11
ajalan065 CreditAttribution: ajalan065 at Google Summer of Code commentedComment #12
ajalan065 CreditAttribution: ajalan065 at Google Summer of Code commentedAs the past patches were not working, meaning there was something on my local which has not been reflected, so I have created a new patch entirely from scratch. So please reroll the module to its initial commit, and then apply the patch which I am uploading again.
Comment #13
ajalan065 CreditAttribution: ajalan065 at Google Summer of Code commentedAttached is the complete patch with the functions moved to services. And also the suggestions in #3 and #6 has been implemented.
An interdiff of the further progress of the work from the last patch in #12 is also attached.
Kindly review the work.
Comment #14
naveenvalechadocument the parameter in function. using @param https://www.drupal.org/node/1354#param
same here document the parameter and document the return value.
same as above.
$request is better name.same at other places as well.
Remove @file
Why editable config ? why not get b/c you are not updating/saving the configuration later.
Use Json utility class and use its static function.
same as above. Use Json utility class.
Comment #15
ajalan065 CreditAttribution: ajalan065 at Google Summer of Code commentedThe changes have been made as per the comments in #14.
Here is the new patch along with the interdiff.
Comment #16
naveenvalechaRemove this whole with comments as well b/c we don't need this basic description.
This should be @var \Drupal\google_vision\GoogleVisionAPI
better name of this property is $googleVisionAPI
I believe
Remove this description.
This line should be "Construct an GoogleVisionAPI object."
file URI is not return type so it should be "@param string $filepath"
We have this functionality common in every function. So add a new property to the class named $apiKey.
In constructor set the value of $apiKey after getting it from configFactory and only do the check in the function.
Also add a @todo we will throw exception later when we will add about the api key not set on status page.
Create a new issue for i.
Now there's lot of cleanup has done. so its time to inject the httpClient service as well.
Comment #17
ajalan065 CreditAttribution: ajalan065 at Google Summer of Code commentedAs per the suggestions in #16, here is the new patch along with the interdiff.
Comment #18
naveenvalechanice improvement. its about to go, only few nitpicks needed.
The variable name can be more properly named as $google_vision
extra line.not needed.
the return will be "@return Array|bool" Update at other places as well.
instead of keeping the url hard coded. Move it to a constant in the class.
Add a new const
const APIEndpoint = 'https://vision.googleapis.com/v1/images:annotate?key='; and use constant in place of hard coded value.
Where are we handing the errors in response ? Create a new issue for handing errors from Vision api.
Comment #19
ajalan065 CreditAttribution: ajalan065 at Google Summer of Code commentedAll the points as mentioned in #18 has been implemented in the new patch, which is uploaded here along with the interdiff.
Comment #20
naveenvalechaNow looks good to be in.
Comment #21
ajalan065 CreditAttribution: ajalan065 at Google Summer of Code commentedThanks :)
Comment #22
penyaskitoWhy do we need to extend ControllerBase if this is not a controller?
Also, we need phpdoc documenting this class.
Why do we need this service, if we forward all method calls to the API directly? Wondering if we only need one service.
Let's handle errors here.
Comment #23
ajalan065 CreditAttribution: ajalan065 at Google Summer of Code commentedAs in 2nd point, I'll remove the class Connector and keep only one service GoogleVisionAPI.
Comment #24
penyaskitoLet's do that, and let's update the issue summary. We should add tests ASAP, but I won't block this one on that.
Comment #25
ajalan065 CreditAttribution: ajalan065 at Google Summer of Code commentedHere is the patch and the interdiff, implementing the suggestions of #22.
Comment #26
penyaskitoI'm happy with this at this stage, we can improve it further in new issues. Can we update the issue summary and title for having an updated description of what we've done here?
Comment #27
ajalan065 CreditAttribution: ajalan065 at Google Summer of Code commentedComment #28
penyaskitoThis looks RTBC for me.
Comment #29
eugene.ilyin CreditAttribution: eugene.ilyin as a volunteer and at DrupalJedi commentedHi all
I have reviewed the patch. Good job but I think that you have a lot of duplicated strings. It's great that we have separate method for each kind of request, but inside of each method we have almost the same code. Would be nice to have few private methods to call them inside of method for each kind of request and avoid duplicating.
What do you think?
Comment #30
penyaskitoI agree, but wanted to move the ball forward and not having an infinite issue. Actually, I'm wondering if we should use https://github.com/ThijsFeryn/google-cloud-vision-api instead, that's why I didn't bother that much about the code duplication.
Comment #31
ajalan065 CreditAttribution: ajalan065 at Google Summer of Code commentedHere is the patch as per #29.
Comment #32
eugene.ilyin CreditAttribution: eugene.ilyin as a volunteer and at DrupalJedi commentedWhy not, but maybe in future? Now this repository looks pretty young. It can be changed unexpectedly. Let's wait a little.
@ajalan065 could you create separate issue for this idea to do not loose it?
Yes, I agree.
I have trouble with module file entity. Give me a little time to resolve it and test the patch.
Comment #33
eugene.ilyin CreditAttribution: eugene.ilyin as a volunteer and at DrupalJedi commentedFatal error: Class 'Drupal\google_vision\RequestOptions' not found in /home/eugene/prj/home/test8/modules/contrib/google_vision/src/GoogleVisionAPI.php on line 67
Where can I obtain this class?
Comment #34
ajalan065 CreditAttribution: ajalan065 at Google Summer of Code commentedStrange!!
The code runs for me without any such error.
Comment #35
penyaskitoYou definitely need a usage statement:
use GuzzleHttp\RequestOptions;
.Where does
APIEndpoint
come from?So I'm sure (and sad) that you didn't test this.
Comment #36
ajalan065 CreditAttribution: ajalan065 at Google Summer of Code commentedAPIEndpoint is a constant which I had defined in the file.
const APIEndpoint = 'https://vision.googleapis.com/v1/images:annotate?key=';
And I had tested it. But was not receiving any such error.
Comment #37
penyaskitoLet's explicitly use
static::APIEndpoint
then.Comment #38
ajalan065 CreditAttribution: ajalan065 at Google Summer of Code commentedOk. I'll post the new patch implementing both of your points. :)
Comment #39
ajalan065 CreditAttribution: ajalan065 at Google Summer of Code commentedHere is the new patch as per #35 and #37.
Comment #40
eugene.ilyin CreditAttribution: eugene.ilyin as a volunteer and at DrupalJedi commentedGreat! Committed.
Comment #41
ajalan065 CreditAttribution: ajalan065 at Google Summer of Code commentedSo, should this issue be marked as Closed(fixed)?
Comment #42
eugene.ilyin CreditAttribution: eugene.ilyin as a volunteer and at DrupalJedi commentedYes, I just want to see that commit is displayed on site.
Comment #43
eugene.ilyin CreditAttribution: eugene.ilyin as a volunteer and at DrupalJedi commentedI see them in repository. So let's close issue.
Comment #44
naveenvalecha@eugene.ilyin,
have you pushed to the d.o. git repo ?
@ajalan065
the issue status should be fixed. read about issue statuses https://www.drupal.org/issue-queue/status
Comment #45
ajalan065 CreditAttribution: ajalan065 at Google Summer of Code commentedI am sorry. I actually meant it as fixed. Just slip of mind to write it as closed :). I understand that it would automatically be marked as closed after two weeks of inactivity.
Comment #46
naveenvalecha