def test_post(): url = 'httpbin.org/post' post_data = {'str': '%in fo+', 'number': 135.5, 'nil': None} res = r.http(url, method='POST', data=post_data).run(conn) post_data['number'] = str(post_data['number']) post_data['nil'] = '' expect_eq(res['form'], post_data) post_data = {'str': '%in fo+', 'number': 135.5, 'nil': None} res = r.http(url, method='POST', data=r.expr(post_data).coerce_to('string'), header={ 'Content-Type': 'application/json' }).run(conn) expect_eq(res['json'], post_data) res = r.http(url, method='POST', data=r.expr(post_data).coerce_to('string')).run(conn) post_data[ 'str'] = '%in fo ' # Default content type is x-www-form-encoded, which changes the '+' to a space expect_eq(res['json'], post_data) post_data = 'a=b&b=c' res = r.http(url, method='POST', data=post_data).run(conn) expect_eq(res['form'], {'a': 'b', 'b': 'c'}) post_data = '<arbitrary>data</arbitrary>' res = r.http(url, method='POST', data=post_data).run(conn) expect_eq(res['data'], post_data)
def test_post(): url = 'httpbin.org/post' post_data = {'str': '%in fo+', 'number': 135.5, 'nil': None} res = r.http(url, method='POST', data=post_data).run(conn) post_data['number'] = str(post_data['number']) post_data['nil'] = '' expect_eq(res['form'], post_data) post_data = {'str': '%in fo+', 'number': 135.5, 'nil': None} res = r.http(url, method='POST', data=r.expr(post_data).coerce_to('string'), header={ 'Content-Type': 'application/json' }).run(conn) expect_eq(res['json'], post_data) post_data = 'a=b&b=c' res = r.http(url, method='POST', data=post_data).run(conn) expect_eq(res['form'], {'a': 'b', 'b': 'c'}) post_data = '<arbitrary>data</arbitrary>' res = r.http(url, method='POST', data=post_data, header={ 'Content-Type': 'application/text/' }).run(conn) expect_eq(res['data'], post_data)
def test_delete(): url = 'httpbin.org/delete' delete_data = { 'nested': { 'arr': [123.45, ['a', 555], 0.123], 'str': 'info', 'number': 135 }, 'time': r.epoch_time(1000), 'nil': None } res = r.http(url, method='DELETE', data=delete_data).run(conn) expect_eq(res['json']['nested'], delete_data['nested']) expect_eq( res['json']['time'], datetime.datetime(1970, 1, 1, 0, 16, 40, tzinfo=res['json']['time'].tzinfo)) delete_data = '<arbitrary> +%data!$%^</arbitrary>' res = r.http(url, method='DELETE', data=delete_data).run(conn) expect_eq(res['data'], delete_data)
def test_head(): url = 'httpbin.org/get' res = r.http(url, method='HEAD', result_format='text').run(conn) expect_eq(res, None) res = r.http(url, method='HEAD').run(conn) expect_eq(res, None)
def test_redirects(): url = 'httpbin.org/redirect/2' expect_error(r.http(url), r.RqlRuntimeError, err_string('GET', url, 'status code 302')) expect_error(r.http(url, redirects=1), r.RqlRuntimeError, err_string('GET', url, 'Number of redirects hit maximum amount')) res = r.http(url, redirects=2).run(conn) expect_eq(res['headers']['Host'], 'httpbin.org')
def test_redirects(): url = 'httpbin.org/redirect/2' expect_error(r.http(url), r.RqlRuntimeError, err_string('GET', url, 'status code 302')) expect_error( r.http(url, redirects=1), r.RqlRuntimeError, err_string('GET', url, 'Number of redirects hit maximum amount')) res = r.http(url, redirects=2).run(conn) expect_eq(res['headers']['Host'], 'httpbin.org')
def test_part(url): expect_error( r.http(url, verify=True, redirects=5), r.RqlRuntimeError, err_string( 'GET', url, 'Peer certificate cannot be authenticated with given CA certificates' )) res = r.http(url, verify=False, redirects=5).split()[0].run(conn) expect_eq(res, '<html>')
def test_put(): url = 'httpbin.org/put' put_data = {'nested':{'arr':[123.45, ['a', 555], 0.123], 'str':'info','number':135,'nil':None},'time':r.epoch_time(1000)} res = r.http(url, method='PUT', data=put_data).run(conn) expect_eq(res['json']['nested'], put_data['nested']) expect_eq(res['json']['time'], datetime.datetime(1970, 1, 1, 0, 16, 40, tzinfo=res['json']['time'].tzinfo)) put_data = '<arbitrary> +%data!$%^</arbitrary>' res = r.http(url, method='PUT', data=put_data).run(conn) expect_eq(res['data'], put_data)
def test_headers(): url = 'httpbin.org/headers' res = r.http(url, header={'Test':'entry','Accept-Encoding':'override'}).run(conn) expect_eq(res['headers']['Test'], 'entry') expect_eq(res['headers']['Accept-Encoding'], 'override') res = r.http(url, header=['Test: entry','Accept-Encoding: override']).run(conn) expect_eq(res['headers']['Test'], 'entry') expect_eq(res['headers']['Accept-Encoding'], 'override')
def test_part(url): expect_error( r.http(url, method='HEAD', verify=True, redirects=5), r.RqlRuntimeError, err_string( 'HEAD', url, 'Peer certificate cannot be authenticated with given CA certificates' )) res = r.http(url, method='HEAD', verify=False, redirects=5).run(conn) expect_eq(res, None)
def test_params(): url = 'httpbin.org/get' res = r.http(url, params={'fake':123,'things':'stuff','nil':None}).run(conn) expect_eq(res['args']['fake'], '123') expect_eq(res['args']['things'], 'stuff') expect_eq(res['args']['nil'], '') res = r.http(url + '?dummy=true', params={'fake':123}).run(conn) expect_eq(res['args']['fake'], '123') expect_eq(res['args']['dummy'], 'true')
def test_headers(): url = 'httpbin.org/headers' res = r.http(url, header={ 'Test': 'entry', 'Accept-Encoding': 'override' }).run(conn) expect_eq(res['headers']['Test'], 'entry') expect_eq(res['headers']['Accept-Encoding'], 'override') res = r.http(url, header=['Test: entry', 'Accept-Encoding: override']).run(conn) expect_eq(res['headers']['Test'], 'entry') expect_eq(res['headers']['Accept-Encoding'], 'override')
def test_params(): url = 'httpbin.org/get' res = r.http(url, params={ 'fake': 123, 'things': 'stuff', 'nil': None }).run(conn) expect_eq(res['args']['fake'], '123') expect_eq(res['args']['things'], 'stuff') expect_eq(res['args']['nil'], '') res = r.http(url + '?dummy=true', params={'fake': 123}).run(conn) expect_eq(res['args']['fake'], '123') expect_eq(res['args']['dummy'], 'true')
def main(): # Setting-up the database: dbSetup() # Connecting to web-sites to reseive data: connection = r.connect("localhost", 28015) try: # Here, for demonstrative purposes, the REST API (v3) of GitHub web-site is used. # Reference: https://developer.github.com/v3/ r.table("stargazers").insert( r.http( 'https://api.github.com/repos/rethinkdb/rethinkdb/stargazers') ).run(connection) r.table("stargazers").insert( r.http( 'https://api.github.com/repos/rethinkdb/rethinkdb/stargazers', page='link-next', page_limit=10)).run(connection) stg_count = r.http( 'https://api.github.com/repos/rethinkdb/rethinkdb/stargazers' ).count().run(connection) name_id = r.http( 'https://api.github.com/repos/rethinkdb/rethinkdb/stargazers' ).pluck('login', 'id').order_by('id').run(connection) # r.table("rubrik").insert(r.http('https://www.rubrik.com/customers/', result_format='json')).run(connection) # r.table("google").insert(r.http('www.google.com')).run(connection) cursor = r.table("stargazers").run(connection) for _ in range(10): for document in cursor: print(document) print(stg_count) print(name_id) # cursor = r.table("google").run(connection) # for document in cursor: # print(document) except ReqlOpFailedError: print 'Table already exists in database (while reading) or is inaccessible (while writing).' finally: connection.close()
def test_get(): url = 'httpbin.org/get' res = r.http(url).run(conn) expect_eq(res['args'], {}) expect_eq(res['headers']['Accept-Encoding'], 'deflate=1;gzip=0.5') expect_eq(res['headers']['User-Agent'].split('/')[0], 'RethinkDB')
def refresh_quakes(): conn = r.connect(host=RDB_HOST, port=RDB_PORT, db=RDB_DB) r.table("quakes").insert( r.http(url)["features"].merge({ "time": r.epoch_time(r.row["properties"]["time"] / 1000), "geometry": r.point( r.row["geometry"]["coordinates"][0], r.row["geometry"]["coordinates"][1])}), conflict="replace").run(conn) conn.close()
def test_basic_auth(): url = 'http://httpbin.org/basic-auth/azure/hunter2' # Wrong password expect_error( r.http(url, auth={ 'type': 'basic', 'user': '******', 'pass': '******' }), r.RqlRuntimeError, err_string('GET', url, 'status code 401')) # Wrong username expect_error( r.http(url, auth={ 'type': 'basic', 'user': '******', 'pass': '******' }), r.RqlRuntimeError, err_string('GET', url, 'status code 401')) # Wrong authentication type expect_error( r.http(url, auth={ 'type': 'digest', 'user': '******', 'pass': '******' }), r.RqlRuntimeError, err_string('GET', url, 'status code 401')) # Correct credentials res = r.http(url, auth={ 'type': 'basic', 'user': '******', 'pass': '******' }).run(conn) expect_eq(res, {'authenticated': True, 'user': '******'}) # Default auth type should be basic res = r.http(url, auth={'user': '******', 'pass': '******'}).run(conn) expect_eq(res, {'authenticated': True, 'user': '******'})
def test_post(): url = 'httpbin.org/post' post_data = {'str':'%in fo+','number':135.5,'nil':None} res = r.http(url, method='POST', data=post_data).run(conn) post_data['number'] = str(post_data['number']) post_data['nil'] = '' expect_eq(res['form'], post_data) post_data = {'str':'%in fo+','number':135.5,'nil':None} res = r.http(url, method='POST', data=r.expr(post_data).coerce_to('string'), header={'Content-Type':'application/json'}).run(conn) expect_eq(res['json'], post_data) post_data = 'a=b&b=c' res = r.http(url, method='POST', data=post_data).run(conn) expect_eq(res['form'], {'a':'b','b':'c'}) post_data = '<arbitrary>data</arbitrary>' res = r.http(url, method='POST', data=post_data, header={'Content-Type':'application/text/'}).run(conn) expect_eq(res['data'], post_data)
def test_basic_auth(): url = 'http://httpbin.org/basic-auth/azure/hunter2' # Wrong password expect_error(r.http(url, auth={'type':'basic','user':'******','pass':'******'}), r.RqlRuntimeError, err_string('GET', url, 'status code 401')) # Wrong username expect_error(r.http(url, auth={'type':'basic','user':'******','pass':'******'}), r.RqlRuntimeError, err_string('GET', url, 'status code 401')) # Wrong authentication type expect_error(r.http(url, auth={'type':'digest','user':'******','pass':'******'}), r.RqlRuntimeError, err_string('GET', url, 'status code 401')) # Correct credentials res = r.http(url, auth={'type':'basic','user':'******','pass':'******'}).run(conn) expect_eq(res, {'authenticated': True, 'user': '******'}) # Default auth type should be basic res = r.http(url, auth={'user':'******','pass':'******'}).run(conn) expect_eq(res, {'authenticated': True, 'user': '******'})
def test_digest_auth(): url = 'http://httpbin.org/digest-auth/auth/azure/hunter2' # Wrong password expect_error( r.http(url, header={'Cookie': 'dummy'}, redirects=5, auth={ 'type': 'digest', 'user': '******', 'pass': '******' }), r.RqlRuntimeError, err_string('GET', url, 'status code 401')) # httpbin apparently doesn't check the username, just the password # Wrong username #expect_error(r.http(url, header={'Cookie':'dummy'}, redirects=5, # auth={'type':'digest','user':'******','pass':'******'}), # r.RqlRuntimeError, err_string('GET', url, 'status code 401')) # httpbin has a 500 error on this # Wrong authentication type #expect_error(r.http(url, header={'Cookie':'dummy'}, redirects=5, # auth={'type':'basic','user':'******','pass':'******'}), # r.RqlRuntimeError, err_string('GET', url, 'status code 401')) # Correct credentials res = r.http(url, header={ 'Cookie': 'dummy' }, redirects=5, auth={ 'type': 'digest', 'user': '******', 'pass': '******' }).run(conn) expect_eq(res, {'authenticated': True, 'user': '******'})
def test_digest_auth(): url = 'http://httpbin.org/digest-auth/auth/azure/hunter2' # Wrong password expect_error(r.http(url, header={'Cookie':'dummy'}, redirects=5, auth={'type':'digest','user':'******','pass':'******'}), r.RqlRuntimeError, err_string('GET', url, 'status code 401')) # httpbin apparently doesn't check the username, just the password # Wrong username #expect_error(r.http(url, header={'Cookie':'dummy'}, redirects=5, # auth={'type':'digest','user':'******','pass':'******'}), # r.RqlRuntimeError, err_string('GET', url, 'status code 401')) # httpbin has a 500 error on this # Wrong authentication type #expect_error(r.http(url, header={'Cookie':'dummy'}, redirects=5, # auth={'type':'basic','user':'******','pass':'******'}), # r.RqlRuntimeError, err_string('GET', url, 'status code 401')) # Correct credentials res = r.http(url, header={'Cookie':'dummy'}, redirects=5, auth={'type':'digest','user':'******','pass':'******'}).run(conn) expect_eq(res, {'authenticated': True, 'user': '******'})
def test_failed_json_parse(): url = 'httpbin.org/html' expect_error(r.http(url, result_format='json'), r.RqlRuntimeError, err_string('GET', url, 'failed to parse JSON response'))
import json, StringIO config = { "port": 8096, "host": "0.0.0.0", "database": { "host": "localhost", "port": 28015, "db": "quake" } } app = Flask(__name__) feedUrl = "earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_month.geojson" refresh = r.table("quakes").insert(r.http(feedUrl)["features"], conflict="replace") try: conn = r.connect(**config["database"]) r.db_create(config["database"]["db"]).run(conn) r.table_create("quakes").run(conn) refresh.run(conn) except Exception as e: if "already exists" not in e.message: print e finally: conn.close() @app.route("/quakes") def quakesJSON():
import rethinkdb as r # Auth as non-privileged account c = r.connect(user='******', password='******') # Permissions check print( "Proving that we don\'t have permissions for executing `r.db('rethinkdb').table('permissions')`" ) try: r.db('rethinkdb').table('permissions').run(c) raise Exception( 'WTF: user has permissions for `rethinkdb.permissions` table') except r.errors.ReqlPermissionError as e: print('OK: {}'.format(e), end='\n\n') print('Open admin connection via Administration Console API') token = r.http('http://127.0.0.1:8080/ajax/reql/open-new-connection', method='POST').run(c) print('Connected with token `{}`'.format(token), end='\n\n') print("Executing `r.db('rethinkdb').table('permissions')` as admin...") result = r.http( 'http://127.0.0.1:8080/ajax/reql/?conn_id={}'.format(token), method='POST', data='\x01\x00\x00\x00\x00\x00\x00\x00' '[1,[15,[[14,["rethinkdb"]],"permissions"]],{"binary_format":"raw","time_format":"raw","profile":false}]' ).run(c) print(bytes(result))
def test_gzip(): res = r.http('httpbin.org/gzip').run(conn) expect_eq(res['gzipped'], True)
def test_part(url): expect_error(r.http(url, method='HEAD', verify=True, redirects=5), r.RqlRuntimeError, err_string('HEAD', url, 'Peer certificate cannot be authenticated with given CA certificates')) res = r.http(url, method='HEAD', verify=False, redirects=5).run(conn) expect_eq(res, None)