Skip to content

Instantly share code, notes, and snippets.

@lucadealfaro
Last active January 5, 2017 20:45
Show Gist options
  • Save lucadealfaro/ebc567ac68997170fb8a22bfe07c5b68 to your computer and use it in GitHub Desktop.
Save lucadealfaro/ebc567ac68997170fb8a22bfe07c5b68 to your computer and use it in GitHub Desktop.
SimiCheck API
import os
import requests
import time
import yaml
api_info = yaml.load(open('api_info.yaml'))
# Files to upload to a comparison.
FILE_LIST = map(lambda x: os.path.join('test_files', x),
['p1.py', 'p2.py', 'p3.py', 'p4.py', 's1.py', 's2.py', 't1.py', 't4.py'])
API_URL = api_info['api_url']
API_KEY = api_info['api_key']
API_HEADERS = dict(simicheck_api_key=API_KEY)
# Creates a comparison.
r = requests.put(API_URL + 'comparison', headers=API_HEADERS)
comparison_data = r.json()
comparison_id = comparison_data['id']
print "Created comparison", comparison_id, r.status_code
# Renames it.
r = requests.post(API_URL + 'comparison/' + comparison_id,
data=dict(comparison_name='Test comparison'),
headers=API_HEADERS)
print "Renamed it:", r.status_code
# Gets the data.
r = requests.get(API_URL + 'comparison/' + comparison_id, headers=API_HEADERS)
print "Data:", r.json(), r.status_code
# Adds the files to the comparison.
def upload_filename(fn):
# We don't want the name to contain the subdirectory.
file_name = os.path.split(fn)[-1]
return requests.put(
API_URL + 'upload_file/' + comparison_id,
files={'file': (file_name, open(fn, 'rb'))},
headers=API_HEADERS
)
for fn in FILE_LIST[:6]:
r = upload_filename(fn)
print "Uploaded", fn, ":", r.status_code, r.json()
# Asks for the output of the 1xn comparison for the last file.
r = requests.post(API_URL + 'similarity_1xn/' + comparison_id,
data=dict(filename='p4.py'),
headers=API_HEADERS)
print "Requested 1xn:", r.json(), r.status_code
# Gets the request id from the request.
request_id = r.json().get('request_id')
# And gets the results. It would be better to define a callback URL.
while True:
r = requests.get(API_URL + 'similarity_1xn/' + comparison_id,
params=dict(filename='p4.py'),
headers=API_HEADERS)
# I want to check that I get the results for the last one I asked.
if r.status_code == 200 and r.json().get('request_id') == request_id:
break
print "Trying later...", r.status_code
time.sleep(1)
print "Similarity for p4.py:", r.json()
# Deletes a list of files.
r = requests.post(API_URL + 'delete_files/' + comparison_id,
data=dict(filenames=['p2.py', 'p3.py']), headers=API_HEADERS)
print "Deleted files:", r.status_code
# Uploads two more files.
inserted_files = []
for fn in FILE_LIST[6:]:
r = upload_filename(fn)
inserted_files.append(r.json()) # To have it available later.
print "Uploaded", fn, ":", r.status_code, r.json()
# Asks for a N x N comparison. Again it would be better to specify a callback URL.
r = requests.post(API_URL + 'similarity_nxn/' + comparison_id, headers=API_HEADERS)
print "Started N x N comparison:", r.status_code
# And gets the results. Note that here there is no request_id to track.
while True:
r = requests.get(API_URL + 'similarity_nxn/' + comparison_id, headers=API_HEADERS)
if r.status_code == 200:
break
print "Trying later...", r.status_code
time.sleep(1)
print "N x N similarities:", r.json()
# Same, but uses a callback URL.
r = requests.post(API_URL + 'similarity_nxn/' + comparison_id,
data=dict(callback_url=API_URL + 'noop'),
headers=API_HEADERS)
print "Started N x N comparison:", r.status_code
# In this testing code, we still wait till it succeeds.
while True:
r = requests.get(API_URL + 'similarity_nxn/' + comparison_id, headers=API_HEADERS)
if r.status_code == 200:
break
print "Trying later...", r.status_code
time.sleep(1)
print "Ok, done the callback"
# Gets the URL for visualizing the similarity.
r = requests.get(API_URL + 'visualize_similarity/' + comparison_id, headers=API_HEADERS)
print "Similarity URL:", r.status_code, r.content
# Same for the comparison between two files.
id0 = inserted_files[0]['id']
id1 = inserted_files[1]['id']
r = requests.get(API_URL + 'visualize_comparison/' + comparison_id + '/' + id0 + '/' + id1,
headers=API_HEADERS)
print "Comparison URL:", r.status_code, r.content
# Let's ask for the list of comparisons.
r = requests.get(API_URL + 'comparisons',
params=dict(pagination=100),
headers=API_HEADERS)
print "List of comparisons:", r.status_code, r.json()
# Let's delete a bunch.
d = r.json()
for c in d['comparisons'][:2]:
r = requests.delete(API_URL + 'comparison/' + c['id'], headers=API_HEADERS)
print "Deleted comparison:", r.status_code
# Deletes the comparison, along with all its data.
# Generally done by call above.
r = requests.delete(API_URL + 'comparison/' + comparison_id, headers=API_HEADERS)
print "Deleted comparison:", r.status_code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment