import urllib.request
import http.cookiejar
import DeployUtil.toolsession as session

#TODO: give an indicator of success
#TODO: handle errors a bit better.

def do_pair(ip, pin, **_args):
	# IF YOU DON'T DO THIS OVER HTTPS YOU WILL GET 308s to goto HTTPS
	scheme = 'https://'
	port = ''
	api = '/api/authorize/pair?pin={pin}&persistent=0'
	verb = 'POST'

	request_url = scheme + ip + port + api.format_map({'pin':pin})

	https_handler = session.create_toolsess_httpsHandler()
	request = urllib.request.Request(url=request_url, method=verb)

	cookies = urllib.request.HTTPCookieProcessor(http.cookiejar.MozillaCookieJar("deployUtil.cookies"))
	opener = urllib.request.build_opener(https_handler, cookies)
	resp = opener.open(request)
	cookies.cookiejar.save(ignore_discard=True)

import requests
import json

#TODO: give an indicator of success
#TODO: handle errors a bit better.

def do_pair(ip, pin, **_args):
	# IF YOU DON'T DO THIS OVER HTTPS YOU WILL GET 308s to goto HTTPS
	# But we cannot verify our HTTPS cert yet because we cannot get it off
	# of all devices.
	# If the tooling gets smarter about what its talking to, then we can
	# make an educated choice.
	scheme = 'https://'
	port = ''
	api = '/api/authorize/pair?pin={pin}&persistent=0'
	request_url = scheme + ip + port + api.format_map({'pin':pin})

	with requests.Session() as session:
		response = session.post(request_url, verify=False)
		cookie_filename = 'deployUtil.cookies'
		cookies = requests.utils.dict_from_cookiejar(response.cookies)
		with open(cookie_filename,'w') as cookie_file:
			json.dump(cookies, cookie_file)

Add dependency on the requests module and refactor
Add dependency on the requests module and refactor

Since I didn't want to have to write my own Multipart POST handler when I start
writing the install command, I started doing research on the options and came
across "requests". I added the dependency and tried it out by reimplementing
the pair command using that library instead of urllib.request and its
supporting types.

On the good side, the library is very easy to use. On the other hand, I have
picked up a nice warning telling to verify my certificate chains which isn't
possible yet for all the devices the WDP runs on.
