In [1]:
import http.client
import urllib.parse
import time
import json

# Request headers
headers = {
    'Ocp-Apim-Subscription-Key': 'a9a9efa851b44d5bbd6c841215a99e00',
    'Content-Type': 'application/json'
}

# Request parameters
params = json.dumps()

def query_graph(query):
    
    params = json.dumps(query)
    request_url = "/academic/v1.0/graph/search?mode=lambda"
    conn = http.client.HTTPSConnection('westus.api.cognitive.microsoft.com')

    # Wait for response, this can take a while...
    response_code = None
    while response_code == 503 or response_code is None:
        if response_code is not None:
            print("Got",response_code,"...")
            time.sleep(600)
        # Try again, make the POST request
        conn.request("POST", request_url, params, headers)
        response = conn.getresponse()
        response_code = response.code

    assert response_code == 200, response.read()

    data = response.read().decode('utf-8')
    conn.close()
    return json.loads(data)

#
query = {
    "path": "/author/PaperIDs/paper",
    "author": {
        "type": "Author",
        "select": [ "DisplayAuthorName","Name","Aliases"],
        "match": { "Name": "John Smith" }
    },
    "paper": {
        "type": "Paper",
        "select": [ "NormalizedTitle", "FieldOfStudyIDs" ]#,
    #"return": { "NormalizedTitle": "checking cache coherence protocols with tla" }
    }
}
results = query_graph(query)
print(json.dumps(results["Results"][0],indent=4))


Got 503 ...
Got 503 ...
Got 503 ...
Got 503 ...
Got 503 ...
Got 503 ...
Got 503 ...
Got 503 ...
Got 503 ...
Got 503 ...
Got 503 ...
Got 503 ...
Got 503 ...
Got 503 ...
Got 503 ...
Got 503 ...
Got 503 ...
Got 503 ...
Got 503 ...
Got 503 ...
Got 503 ...
Got 503 ...
Got 503 ...
---------------------------------------------------------------------------
gaierror                                  Traceback (most recent call last)
<ipython-input-1-5de4c4d0bc1a> in <module>()
     36         print("Got",response_code,"...")
     37         time.sleep(600)
---> 38     conn.request("POST", request_url, params, headers)
     39     response = conn.getresponse()
     40     response_code = response.code

//anaconda/envs/py35/lib/python3.5/http/client.py in request(self, method, url, body, headers)
   1104     def request(self, method, url, body=None, headers={}):
   1105         """Send a complete request to the server."""
-> 1106         self._send_request(method, url, body, headers)
   1107 
   1108     def _set_content_length(self, body, method):

//anaconda/envs/py35/lib/python3.5/http/client.py in _send_request(self, method, url, body, headers)
   1149             # default charset of iso-8859-1.
   1150             body = _encode(body, 'body')
-> 1151         self.endheaders(body)
   1152 
   1153     def getresponse(self):

//anaconda/envs/py35/lib/python3.5/http/client.py in endheaders(self, message_body)
   1100         else:
   1101             raise CannotSendHeader()
-> 1102         self._send_output(message_body)
   1103 
   1104     def request(self, method, url, body=None, headers={}):

//anaconda/envs/py35/lib/python3.5/http/client.py in _send_output(self, message_body)
    932         del self._buffer[:]
    933 
--> 934         self.send(msg)
    935         if message_body is not None:
    936             self.send(message_body)

//anaconda/envs/py35/lib/python3.5/http/client.py in send(self, data)
    875         if self.sock is None:
    876             if self.auto_open:
--> 877                 self.connect()
    878             else:
    879                 raise NotConnected()

//anaconda/envs/py35/lib/python3.5/http/client.py in connect(self)
   1250             "Connect to a host on a given (SSL) port."
   1251 
-> 1252             super().connect()
   1253 
   1254             if self._tunnel_host:

//anaconda/envs/py35/lib/python3.5/http/client.py in connect(self)
    847         """Connect to the host and port specified in __init__."""
    848         self.sock = self._create_connection(
--> 849             (self.host,self.port), self.timeout, self.source_address)
    850         self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
    851 

//anaconda/envs/py35/lib/python3.5/socket.py in create_connection(address, timeout, source_address)
    691     host, port = address
    692     err = None
--> 693     for res in getaddrinfo(host, port, 0, SOCK_STREAM):
    694         af, socktype, proto, canonname, sa = res
    695         sock = None

//anaconda/envs/py35/lib/python3.5/socket.py in getaddrinfo(host, port, family, type, proto, flags)
    730     # and socket type values to enum constants.
    731     addrlist = []
--> 732     for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
    733         af, socktype, proto, canonname, sa = res
    734         addrlist.append((_intenum_converter(af, AddressFamily),

gaierror: [Errno 8] nodename nor servname provided, or not known

In [34]:
type(data)


Out[34]:
str

In [ ]: