google-ads-python icon indicating copy to clipboard operation
google-ads-python copied to clipboard

DismissRecommendation Example

Open AdamWalt opened this issue 3 years ago • 5 comments

Describe the bug: When using the DismissRecommendation example the below error is raised:

    operation = request.DismissRecommendationOperation()
AttributeError: DismissRecommendationOperation

Steps to Reproduce: Run the example

Expected behavior: Recommendations provided are dismissed

Client library version and API version: Client library version: Google Ads API version: v10

Request/Response Logs:

Anything else we should know about your project / environment:

AdamWalt avatar May 26 '22 17:05 AdamWalt

I can't reproduce this error.

The error means the 'request' does not contain the function DismissRecommendationOperation. To verify that your request object is valid, place a breakpoint() in your code the line before ' operation = request.DismissRecommendationOperation()'. Execute the example, and on the debug command line execute 'dir(request)' and send the output ensuring to sanitize any PII. If the request object is valid you will see DismissRecommendationOperation in the list.

-> operation = request.DismissRecommendationOperation() (Pdb) dir(request) ['DismissRecommendationOperation', 'bool', 'class', 'contains', 'delattr', 'dict', 'dir', 'doc', 'eq', 'format', 'ge', 'getattr', 'getattribute', 'gt', 'hash', 'init', 'init_subclass', 'le', 'lt', 'module', 'ne', 'new', 'reduce', 'reduce_ex', 'repr', 'setattr', 'sizeof', 'str', 'subclasshook', 'weakref', '_meta', '_pb']

bobhancock avatar May 26 '22 18:05 bobhancock

Hi Bob,

Thank you for responding. Below is my code and the debugger output. Not sure what could be making this so different than the expected output.

['ByteSize', 'Clear', 'ClearExtension', 'ClearField', 'CopyFrom', 'DESCRIPTOR', 'DiscardUnknownFields', 'Extensions', 'FindInitializationErrors', 'FromString', 'HasExtension', 'HasField', 'IsInitialized', 'ListFields', 'MergeFrom', 'MergeFromString', 'ParseFromString', 'RegisterExtension', 'SerializePartialToString', 'SerializeToString', 'SetInParent', 'UnknownFields', 'WhichOneof', '_CheckCalledFromGeneratedFile', '_SetListener', 'class', 'deepcopy', 'delattr', 'dir', 'doc', 'eq', 'format', 'ge', 'getattribute', 'getstate', 'gt', 'hash', 'init', 'init_subclass', 'le', 'lt', 'module', 'ne', 'new', 'reduce', 'reduce_ex', 'repr', 'setattr', 'setstate', 'sizeof', 'slots', 'str', 'subclasshook', 'unicode', '_extensions_by_name', '_extensions_by_number', 'customer_id', 'operations', 'partial_failure']

def main(client, customer_id, recommendation_id): recommendation_service = google_client.get_service("RecommendationService") request = client.get_type("DismissRecommendationRequest") breakpoint() operation = request.DismissRecommendationOperation() operation.resource_name = recommendation_service.recommendation_path( customer_id, recommendation_id ) request.customer_id = customer_id request.operations.append(operation)

response = recommendation_service.dismiss_recommendation(request=request)

print(
    "Dismissed recommendation with resource name: "
    f"'{response.results[0].resource_name}'."
)

http://www.digible.com

https://www.facebook.com/digibleinc/ https://www.instagram.com/digibleinc/ https://www.linkedin.com/in/adam-walters-digital-marketing/ https://www.youtube.com/channel/UCGfz7t5SQp3BMoQMEoxFuRQ/featured

Adam Walters

Director of Paid Media

AUTHENTICITY | FOCUS | CURIOSITY | HUMILITY | HAPPINESS

Phone: (317) 840-1645

Email: @.***

https://www.denverpost.com/2021/05/05/digible-top-workplaces-2021/

This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If the reader of this message is not the intended recipient, or an employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited.

On Thu, May 26, 2022 at 2:01 PM Bob Hancock @.***> wrote:

I can't reproduce this error.

The error means the 'request' does not contain the function DismissRecommendationOperation. To verify that your request object is valid, place a breakpoint() in your code the line before ' operation = request.DismissRecommendationOperation()'. Execute the example, and on the debug command line execute 'dir(request)' and send the output ensuring to sanitize any PII. If the request object is valid you will see DismissRecommendationOperation in the list.

-> operation = request.DismissRecommendationOperation() (Pdb) dir(request) ['DismissRecommendationOperation', 'bool', 'class', 'contains', ' delattr', 'dict', 'dir', 'doc', 'eq', 'format', 'ge', ' getattr', 'getattribute', 'gt', 'hash', 'init', 'init_subclass', 'le', 'lt', 'module', 'ne', 'new', 'reduce', 'reduce_ex', ' repr', 'setattr', 'sizeof', 'str', 'subclasshook', 'weakref', '_meta', '_pb']

— Reply to this email directly, view it on GitHub https://github.com/googleads/google-ads-python/issues/640#issuecomment-1138863803, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOJLM3BIFWMISIRWPHF52ITVL6377ANCNFSM5XBYJK2Q . You are receiving this because you authored the thread.Message ID: @.***>

AdamWalt avatar May 26 '22 19:05 AdamWalt

@AdamWalt which version of the library are you using? The DismissRecommendationRequest message has had DismissRecommendationOperation defined on it as a nested message for as far back as I can see (at least v7), see here for v9. Also, can you try running type(request) from your breakpoint to double-check what object the request variable is assigned to?

BenRKarl avatar Jul 27 '22 15:07 BenRKarl

@AdamWalt I'm going to close this issue as stale for now, but please reopen if you have more questions or details based on my last comment.

BenRKarl avatar Aug 05 '22 14:08 BenRKarl

@BenRKarl Currently running v11. Below is a sample of the code, type (request), and output/error.

    try:
        recommendation_service = client.get_service("RecommendationService")
        request = client.get_type("DismissRecommendationRequest")
        print(type(request))
        operation = request.DismissRecommendationOperation()
        operation.resource_name = recommendation_service.recommendation_path(
            customer_id, recommendation_id
        )
        request.customer_id = customer_id
        request.operations.append(operation)

        response = recommendation_service.dismiss_recommendation(request=request)

        print(
            "Dismissed recommendation with resource name: "
            f"'{response.results[0].resource_name}'."
        )
    except GoogleAdsException as ex:
        print(
            f'Request with ID "{ex.request_id}" failed with status '
            f'"{ex.error.code().name}" and includes the following errors:'
        )
        for error in ex.failure.errors:
            print(f'\tError with message "{error.message}".')
            if error.location:
                for field_path_element in error.location.field_path_elements:
                    print(f"\t\tOn field: {field_path_element.field_name}")
        sys.exit(1)

google_client = GoogleAdsClient.load_from_storage("google-ads.yaml", version="v11")

main(google_client, "1658560527", "NzM2MjY3MzI4LTEwMC0xNjYwMDkzNDA5ODgyLSs5MDc4OTg3NTUw")```

and the output/error:

```Traceback (most recent call last):
  File "/Users/adamwalters/digible_repos/Paid_Social_Optimizations/adhoc_projects/google_recommendations/recommendations.py", line 126, in <module>
    main(google_client, "1658560527", "NzM2MjY3MzI4LTEwMC0xNjYwMDkzNDA5ODgyLSs5MDc4OTg3NTUw")
  File "/Users/adamwalters/digible_repos/Paid_Social_Optimizations/adhoc_projects/google_recommendations/recommendations.py", line 99, in main
    operation = request.DismissRecommendationOperation()
AttributeError: DismissRecommendationOperation
<class 'DismissRecommendationRequest'>

Process finished with exit code 1```


AdamWalt avatar Aug 11 '22 19:08 AdamWalt

@AdamWalt my suspicion here is that you have use_proto_plus set to False, in which case there's a slightly different workflow here. You can resolve this by setting use_proto_plus to True, or if you need to continue with it disabled, you can retrieve an operation instance like this:

request = client.get_type("DismissRecommendationRequest")
operation = request.operations.add()
print(type(operation))
# <class 'DismissRecommendationOperation'>

BenRKarl avatar Aug 17 '22 18:08 BenRKarl