Skip to content

Python Example #12

Description

@gwsampso

Do you happen to have python example?

I've had good success connecting but cant figure out how to query

I'm getting a 401 error

import requests
import hashlib
import hmac
import time
import uuid
import json

sqlRestURL = "http://localhost:5050"

apiKey = bytes('sqlrestTestKey', 'utf-8')

def createHmac(message):
	hash = hmac.new(apiKey, message, hashlib.sha256)
	return hash.hexdigest()

def setAuthHeader(options, message=bytes('', encoding='utf8')):
	realm = "testing-func"
	hmac = createHmac(message)
	nonce = uuid.uuid4().hex
	timestamp = int(round(time.time() * 1000))
	options['Authorization'] = f"{realm}:{hmac}:{nonce}:{timestamp}"
	return options

def connect():
	url = sqlRestURL + "/connect"
	options = {}
	auth_headers = setAuthHeader(options)
	r=requests.get(url, headers=auth_headers)
	print(r.status_code)
	print(r.json())

def query():
	url = sqlRestURL + "/v1/query"
	options = {"Content-Type": "application/json"}
	payload = {'query': 'SELECT TOP 3 * FROM Flights.dbo.Airlines'}
	auth_headers = setAuthHeader(options, bytes(json.dumps(payload), encoding='utf8'))
	r=requests.post(url, headers=auth_headers, data=payload)
	print(r.status_code)
	print(r.json())

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions