Count, find, list and delete script_tags using Python API!

Solved
Highlighted
Tourist
6 1 1

In the API docs, the following are possible actions you can do with script tags:

 

 

But using Python API, I am unsure how I would go about replicating these calls. I have managed to create a script_tag given an URL, but how can I count them, list them, find one by URL and delete them?

 

My current working "create_script_tag" function looks like this:

 

    def upsert_script_tag(self, src):
        script_reference={"event": "onload", "src": f"{src}"}
        print(f"UPSERTING SCRIPT TAG WITH '{script_reference}'")
        st=shopify.ScriptTag(script_reference)
        ret = st.save()
        if st.errors:
            print(f"ERROR: {st.errors.full_messages()}")
        print(f" + Return was :{ret}")

But my naive attempt at finding a script tag fails:

 

    def find_script_tags(self, src):
        print(f"FINDING SCRIPT TAGS WITH src='{src}'")
        st=shopify.ScriptTag(src)
        # WARNING DOES NOT WORK!!! DO NOT COPY
        ret = st.find(src=src)
        if st.errors:
            print(f"ERROR: {st.errors.full_messages()}")
        print(f" + Return was :{ret}")

All hints welcome!

0 Likes
Community Manager
Community Manager
618 45 76

Hi @devdude,

 

I'd recommend taking a look at the pyactiveresource documentation, as it explains the find method. Essentially, you need to make sure you're searching by the object's id. So if you have a script tag with id of 123456789, you would want the following:

 

st = shopify.ScriptTag.find(123456789)
0 Likes
Tourist
6 1 1

Busfox,

 

thanks for the answer! But the original REST api accepts a filter on src. The way I understood it any named args I put will be forwarded as url parameters, so my src=should really work in this case!

 

Further after some testing I found that It worked perfectly a few times then stopped working without changes to the code. I then found what I think is a bug in activeresource. If I do the following it works but returns 401:

 

            cls=shopify.ScriptTag
            print(f" + headers: {cls.headers}")
            if not cls.headers:
                cls.headers={'bob':'lol'}
            ret=cls.find(src=src)

The bug is that activeresources crashes with headers that has value "None" here:

File "/usr/local/lib/python3.7/site-packages/pyactiveresource/connection.py", line 267, in <listcomp>
header_string = '\n'.join([':'.join((k, v)) for k, v in
TypeError: sequence item 1: expected str instance, NoneType found

And after further testing I found that the header that was missing was actually the token header (from my print statement above):

{'User-Agent': 'ShopifyPythonAPI/5.1.2 Python/3.7.4', 'X-Shopify-Access-Token': None}

So now the original question was answered but a new one arises, why did my token not propegate to the ScriptTag query even if I had created and activated a session using the token?

 

If you can answer this I would be overjoyed!

 

 

 

0 Likes

Success.

Community Manager
Community Manager
618 45 76

Hey @devdude,

 

You're absolutely right. I was looking for a find_by method in pyactiveresource, but really the query params at the bottom of the readme is what was needed. Thanks for sharing.

 

In terms of your question about the session not getting created and activated properly, did the session get reset somehow? Each time your app server starts you would need to recreate and activate your session. Also, is it possible your access token is an online one? Online access tokens expire with admin sessions, so you may need to generate a new one if you specified that you wanted an online token initially.

 

 

0 Likes
Tourist
6 1 1

I found an error in my code that did not set the session up properly. It works now :)

0 Likes