import requests
import io
import time


def get_docx_file(fp: str):
    if fp:
        with open(fp, 'rb') as f:
            file = f.read()
    else:
        # here's a sample file if you don't have one on hand
        r = requests.get('https://digitaliserstorage.blob.core.windows.net/static/worksheets/getmarked-practice-0.docx')
        file = io.BytesIO(r.content)
        file.name = 'getmarked-practice-0.docx'

    return file


# =================================================================================
#                               CONFIGURATION
# --------------------------------------------------------------------------------

# api keys, please create one if you haven't
api_key = ''
create_job_endpoint = 'https://digitaliser.getmarked.ai/api/v1.1/job/import/'

# your own docx file, if you have one. If left blank a sample will be provided
docx_file_path = ''

# setup webhook url to receive extracted questions once job is completed instead of polling
# You may use https://typedwebhook.tools to generate a temporary webhook url for testing purpose
data = {'webhook': ''}
# =================================================================================


# =================================================================================
# Step 1: Upload file to GETMARKED Quiz Import API
# --------------------------------------------------------------------------------
file = get_docx_file(docx_file_path)
files = {"file": file}  # binary data of docx sample file

# upload file to API via HTTP POST
headers = {"AUTHORIZATION": f"Api-Key {api_key}"}
response = requests.post(create_job_endpoint, files=files, data=data, headers=headers)
data = response.json()
result_endpoint = data.get('data', {}).get('result_endpoint')

print('response:')
print(data)
print(f'Questions data can be obtained at {result_endpoint}')
print('\n\n')
# =================================================================================

# =================================================================================
# Step 2: Retrieve extracted questions data
# --------------------------------------------------------------------------------
# this endpoint would be given to you on successful upload to the server
# you should poll this endpoint at 15s interval since conversion takes a while. Usually one min.
print(f'Polling {result_endpoint}')
while True and result_endpoint:
    time.sleep(15)  # please do not DDOS our endpoint, poll at a minimum of 15 second interval

    headers = {"AUTHORIZATION": f"Api-Key {api_key}"}
    response = requests.get(result_endpoint, headers=headers)
    data = response.json()

    if 'questions' in data['data']:
        for qns in data['data']['questions']:
            print(qns)

        break

    else:
        print('File not ready yet, retying in 15 seconds....')


# =================================================================================
# Step 3: Store the extracted questions data in your own database
# --------------------------------------------------------------------------------

# This is deliberately left blank. Moving the data into your
# own database should not take more than an afternoon to code and complete.

# =================================================================================

# END.
