import json
from urllib.request import *

headers = {}
with open('apikey.txt', 'r') as f:
    apikey = f.readline().strip()
    headers['Authorization'] = 'Token token=' + apikey

rooturl = 'https://www.wanikani.com/api/v2'

request = Request(rooturl + '/user', headers=headers)
response = urlopen(request)
print(response.getcode())
print(response.info())
user = json.loads(response.read().decode())
print(user)

import json
from urllib.error import *
from urllib.request import *

headers = {}
with open('apikey.txt', 'r') as f:
    apikey = f.readline().strip()
    headers['Authorization'] = 'Token token=' + apikey

rooturl = 'https://www.wanikani.com/api/v2'

request = Request(rooturl + '/user', headers=headers)
try: response = urlopen(request)
except URLError as error:
    print('Error while fetching user data: ' + error.reason)
    exit()
# print(response.getcode())
# print(response.info())
user = json.loads(response.read().decode())
# print(user)
print("""Fetching information for
  user:  {username}
  level: {level}
""".format(**user['data']))

Add error handling for GET request.
Add error handling for GET request.

  import json
+ from urllib.error import *
  from urllib.request import *
  
  headers = {}
  with open('apikey.txt', 'r') as f:
      apikey = f.readline().strip()
      headers['Authorization'] = 'Token token=' + apikey
  
  rooturl = 'https://www.wanikani.com/api/v2'
  
  request = Request(rooturl + '/user', headers=headers)
- response = urlopen(request)
+ try: response = urlopen(request)
+ except URLError as error:
+     print('Error while fetching user data: ' + error.reason)
+     exit()
- print(response.getcode())
+ # print(response.getcode())
- print(response.info())
+ # print(response.info())
  user = json.loads(response.read().decode())
- print(user)
+ # print(user)
+ print("""Fetching information for
+   user:  {username}
+   level: {level}
+ """.format(**user['data']))
  
Add error handling for GET request.
## Code Before:
import json
from urllib.request import *

headers = {}
with open('apikey.txt', 'r') as f:
    apikey = f.readline().strip()
    headers['Authorization'] = 'Token token=' + apikey

rooturl = 'https://www.wanikani.com/api/v2'

request = Request(rooturl + '/user', headers=headers)
response = urlopen(request)
print(response.getcode())
print(response.info())
user = json.loads(response.read().decode())
print(user)

## Instruction:
Add error handling for GET request.
## Code After:
import json
from urllib.error import *
from urllib.request import *

headers = {}
with open('apikey.txt', 'r') as f:
    apikey = f.readline().strip()
    headers['Authorization'] = 'Token token=' + apikey

rooturl = 'https://www.wanikani.com/api/v2'

request = Request(rooturl + '/user', headers=headers)
try: response = urlopen(request)
except URLError as error:
    print('Error while fetching user data: ' + error.reason)
    exit()
# print(response.getcode())
# print(response.info())
user = json.loads(response.read().decode())
# print(user)
print("""Fetching information for
  user:  {username}
  level: {level}
""".format(**user['data']))
