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 #12

Open
gwsampso opened this issue Sep 20, 2019 · 0 comments
Open

Python Example #12

gwsampso opened this issue Sep 20, 2019 · 0 comments

Comments

@gwsampso
Copy link
Contributor

gwsampso commented Sep 20, 2019

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