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

Python example script is not compatible with Python 3 #12

Open
NightSkySK opened this issue Sep 12, 2020 · 0 comments
Open

Python example script is not compatible with Python 3 #12

NightSkySK opened this issue Sep 12, 2020 · 0 comments

Comments

@NightSkySK
Copy link

As Python 2 is officially EOL there should be updated script available for Python3.

This is my simply conversion to Python 3 which seems to be working:


import collections, urllib, base64, hmac, hashlib, json, urllib.parse, urllib.request


def product_import_tme():
    # /product/product_import_tme/

    token = "<TOKEN>"
    app_secret = "<APP_KEY>"

    params = {
        "SymbolList[0]": "NE555D",
        "SymbolList[1]": "1N4007-DC",
        "Country": "PL",
        "Currency": "PLN",
        "Language": "EN",
    }

    response = api_call("Products/GetPrices", params, token, app_secret, True)
    response = json.loads(response)
    print(response)


def api_call(action, params, token, app_secret, show_header=False):
    api_url = "https://api.tme.eu/" + action + ".json"
    params["Token"] = token

    params = collections.OrderedDict(sorted(params.items()))

    encoded_params = urllib.parse.urlencode(params, "")
    signature_base = (
        "POST"
        + "&"
        + urllib.parse.quote(api_url, "")
        + "&"
        + urllib.parse.quote(encoded_params, "")
    )

    api_signature = base64.encodestring(
        hmac.new(
            app_secret.encode("utf-8"), signature_base.encode("utf-8"), hashlib.sha1
        ).digest()
    ).rstrip()
    params["ApiSignature"] = api_signature

    opts = {
        "http": {
            "method": "POST",
            "header": "Content-type: application/x-www-form-urlencoded",
            "content": urllib.parse.urlencode(params).encode("utf-8"),
        }
    }

    http_header = {
        "Content-type": "application/x-www-form-urlencoded",
    }

    # create your HTTP request
    req = urllib.request.Request(
        api_url, urllib.parse.urlencode(params).encode("utf-8"), http_header
    )

    # submit your request
    res = urllib.request.urlopen(req)
    # res = urllib.request.urlopen(req)
    html = res.read()

    return html


print(product_import_tme())
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

No branches or pull requests

1 participant