Skip to content

Instantly share code, notes, and snippets.

@vwillcox
Created January 7, 2019 22:09
Show Gist options
  • Save vwillcox/6fa0f8ac0dcd991f24413385cc521ebb to your computer and use it in GitHub Desktop.
Save vwillcox/6fa0f8ac0dcd991f24413385cc521ebb to your computer and use it in GitHub Desktop.
Tesco.com Product Grocery Search API Python3 quick demo script
########### Python 3.2 #############
import http.client, urllib.request, urllib.parse, urllib.error, base64, json, jsonify
import sys, getopt
def main(argv):
query = ''
offset = 0
limit = 10
try:
opts, args = getopt.getopt(argv, "hq:", ["query="])
except getopt.GetoptError:
print("[Errno {0}] {1}".format(getopt.GetoptError.errno, getopt.strerror))
sys.exit(2)
for opt, arg in opts:
if opt == '-q':
query=arg
api_token = '{subscription key}'
url = 'dev.tescolabs.com'
headers = {
# Request headers
'Ocp-Apim-Subscription-Key': format(api_token),
"content-type": 'application/json'
}
try:
conn = http.client.HTTPSConnection(url)
conn.request("GET", "/grocery/products/?query="+query+"&offset=0&limit=10","{bpdy}", headers)
response = conn.getresponse()
data = response.read().decode()
conn.close()
print(data)
except Exception as e:
print("[Errno {0}] {1}".format(e.errno, e.strerror))
####################################
if __name__ == "__main__":
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment