<s> __author__ = '<STR_LIT>' <EOL> import json <EOL> import os <EOL> import obelisk <EOL> import nacl . encoding <EOL> from binascii import unhexlify <EOL> from collections import OrderedDict <EOL> from functools import wraps <EOL> from txrestapi . resource import APIResource <EOL> from txrestapi . methods import GET , POST , DELETE <EOL> from twisted . web import server <EOL> from twisted . web . resource import NoResource <EOL> from twisted . web import http <EOL> from twisted . web . server import Site <EOL> from twisted . internet import defer , reactor , task <EOL> from twisted . protocols . basic import FileSender <EOL> from config import DATA_FOLDER , RESOLVER , delete_value , set_value , get_value , str_to_bool , TRANSACTION_FEE <EOL> from protos . countries import CountryCode <EOL> from protos import objects <EOL> from keys import blockchainid <EOL> from keys . keychain import KeyChain <EOL> from dht . utils import digest <EOL> from market . profile import Profile <EOL> from market . contracts import Contract , check_order_for_payment <EOL> from market . btcprice import BtcPrice <EOL> from net . upnp import PortMapper <EOL> from api . utils import sanitize_html <EOL> DEFAULT_RECORDS_COUNT = <NUM_LIT:20> <EOL> DEFAULT_RECORDS_OFFSET = <NUM_LIT:0> <EOL> class OpenBazaarAPI ( APIResource ) : <EOL> """<STR_LIT>""" <EOL> def authenticated ( func ) : <EOL> def _authenticate ( self , request ) : <EOL> session = request . getSession ( ) <EOL> if session not in self . authenticated_sessions : <EOL> session . expire ( ) <EOL> request . setResponseCode ( <NUM_LIT> ) <EOL> request . write ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> else : <EOL> if request . getHeader ( "<STR_LIT:Content-Type>" ) == "<STR_LIT:application/json>" : <EOL> request . args = json . loads ( request . content . read ( ) ) <EOL> func ( self , request ) <EOL> return server . NOT_DONE_YET <EOL> return wraps ( func ) ( _authenticate ) <EOL> def __init__ ( self , mserver , kserver , protocol , username , password , authenticated_sessions ) : <EOL> self . mserver = mserver <EOL> self . kserver = kserver <EOL> self . protocol = protocol <EOL> self . db = mserver . db <EOL> self . keychain = KeyChain ( self . db ) <EOL> self . username = username <EOL> self . password = password <EOL> self . authenticated_sessions = authenticated_sessions <EOL> self . failed_login_attempts = { } <EOL> task . LoopingCall ( self . _keep_sessions_alive ) . start ( <NUM_LIT> , False ) <EOL> APIResource . __init__ ( self ) <EOL> def _keep_sessions_alive ( self ) : <EOL> for session in self . authenticated_sessions : <EOL> session . touch ( ) <EOL> def _failed_login ( self , host ) : <EOL> def remove_ban ( host ) : <EOL> del self . failed_login_attempts [ host ] <EOL> if host in self . failed_login_attempts : <EOL> self . failed_login_attempts [ host ] += <NUM_LIT:1> <EOL> reactor . callLater ( <NUM_LIT> , remove_ban , host ) <EOL> else : <EOL> self . failed_login_attempts [ host ] = <NUM_LIT:1> <EOL> @ POST ( '<STR_LIT>' ) <EOL> def login ( self , request ) : <EOL> request . setHeader ( '<STR_LIT>' , "<STR_LIT:application/json>" ) <EOL> if request . getHost ( ) . host in self . failed_login_attempts and self . failed_login_attempts [ request . getHost ( ) . host ] >= <NUM_LIT:7> : <EOL> return json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT>" : "<STR_LIT>" } ) <EOL> try : <EOL> if request . args [ "<STR_LIT:username>" ] [ <NUM_LIT:0> ] == self . username and request . args [ "<STR_LIT:password>" ] [ <NUM_LIT:0> ] == self . password : <EOL> self . authenticated_sessions . append ( request . getSession ( ) ) <EOL> if request . getHost ( ) . host in self . failed_login_attempts : <EOL> del self . failed_login_attempts [ request . getHost ( ) . host ] <EOL> return json . dumps ( { "<STR_LIT:success>" : True } ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> except Exception : <EOL> self . _failed_login ( request . getHost ( ) . host ) <EOL> return json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT>" : "<STR_LIT>" } ) <EOL> @ GET ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def get_image ( self , request ) : <EOL> @ defer . inlineCallbacks <EOL> def _showImage ( resp = None ) : <EOL> @ defer . inlineCallbacks <EOL> def _setContentDispositionAndSend ( file_path , extension , content_type ) : <EOL> request . setHeader ( '<STR_LIT>' , '<STR_LIT>' % ( file_path , extension ) ) <EOL> request . setHeader ( '<STR_LIT>' , content_type ) <EOL> request . setHeader ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> f = open ( file_path , "<STR_LIT:rb>" ) <EOL> yield FileSender ( ) . beginFileTransfer ( f , request ) <EOL> f . close ( ) <EOL> defer . returnValue ( <NUM_LIT:0> ) <EOL> if os . path . exists ( image_path ) : <EOL> yield _setContentDispositionAndSend ( image_path , "<STR_LIT>" , "<STR_LIT>" ) <EOL> else : <EOL> request . setResponseCode ( http . NOT_FOUND ) <EOL> request . write ( "<STR_LIT>" % request . path ) <EOL> request . finish ( ) <EOL> if "<STR_LIT>" in request . args and len ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) == <NUM_LIT> : <EOL> if self . db . filemap . get_file ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) is not None : <EOL> image_path = self . db . filemap . get_file ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) <EOL> else : <EOL> image_path = os . path . join ( DATA_FOLDER , "<STR_LIT>" , request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) <EOL> if not os . path . exists ( image_path ) and "<STR_LIT>" in request . args : <EOL> node = None <EOL> for connection in self . protocol . values ( ) : <EOL> if connection . handler . node is not None and connection . handler . node . id == unhexlify ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) : <EOL> node = connection . handler . node <EOL> self . mserver . get_image ( node , unhexlify ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) ) . addCallback ( _showImage ) <EOL> if node is None : <EOL> _showImage ( ) <EOL> else : <EOL> _showImage ( ) <EOL> else : <EOL> request . write ( NoResource ( ) . render ( request ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> @ GET ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def get_profile ( self , request ) : <EOL> def parse_profile ( profile , temp_handle = None ) : <EOL> if profile is not None : <EOL> profile_json = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:name>" : profile . name , <EOL> "<STR_LIT:location>" : str ( CountryCode . Name ( profile . location ) ) , <EOL> "<STR_LIT>" : profile . guid_key . public_key . encode ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : profile . nsfw , <EOL> "<STR_LIT>" : profile . vendor , <EOL> "<STR_LIT>" : profile . moderator , <EOL> "<STR_LIT>" : round ( profile . moderation_fee , <NUM_LIT:2> ) , <EOL> "<STR_LIT>" : profile . handle , <EOL> "<STR_LIT>" : profile . about , <EOL> "<STR_LIT>" : profile . short_description , <EOL> "<STR_LIT>" : profile . website , <EOL> "<STR_LIT:email>" : profile . email , <EOL> "<STR_LIT>" : profile . primary_color , <EOL> "<STR_LIT>" : profile . secondary_color , <EOL> "<STR_LIT>" : profile . background_color , <EOL> "<STR_LIT>" : profile . text_color , <EOL> "<STR_LIT>" : profile . pgp_key . public_key , <EOL> "<STR_LIT>" : profile . avatar_hash . encode ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : profile . header_hash . encode ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : { } <EOL> } <EOL> } <EOL> if temp_handle : <EOL> profile_json [ "<STR_LIT>" ] [ "<STR_LIT>" ] = temp_handle <EOL> if "<STR_LIT>" in request . args : <EOL> profile_json [ "<STR_LIT>" ] [ "<STR_LIT>" ] = request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> else : <EOL> profile_json [ "<STR_LIT>" ] [ "<STR_LIT>" ] = self . keychain . guid . encode ( "<STR_LIT>" ) <EOL> for account in profile . social : <EOL> profile_json [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ str ( <EOL> objects . Profile . SocialAccount . SocialType . Name ( account . type ) ) . lower ( ) ] = { <EOL> "<STR_LIT:username>" : account . username , <EOL> "<STR_LIT>" : account . proof_url <EOL> } <EOL> if ( profile . handle is not "<STR_LIT>" and "<STR_LIT>" not in profile . handle and <EOL> not blockchainid . validate ( profile . handle , profile_json [ "<STR_LIT>" ] [ "<STR_LIT>" ] ) ) : <EOL> profile_json [ "<STR_LIT>" ] [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> request . setHeader ( '<STR_LIT>' , "<STR_LIT:application/json>" ) <EOL> request . write ( json . dumps ( sanitize_html ( profile_json ) , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> else : <EOL> request . write ( json . dumps ( { } ) ) <EOL> request . finish ( ) <EOL> if "<STR_LIT>" in request . args : <EOL> def get_node ( node ) : <EOL> if node is not None : <EOL> self . mserver . get_profile ( node ) . addCallback ( parse_profile ) <EOL> else : <EOL> request . write ( json . dumps ( { } ) ) <EOL> request . finish ( ) <EOL> self . kserver . resolve ( unhexlify ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) ) . addCallback ( get_node ) <EOL> else : <EOL> p = Profile ( self . db ) . get ( ) <EOL> if not p . HasField ( "<STR_LIT>" ) : <EOL> request . write ( json . dumps ( { } ) ) <EOL> request . finish ( ) <EOL> else : <EOL> temp_handle = self . db . profile . get_temp_handle ( ) <EOL> parse_profile ( p , None if temp_handle == "<STR_LIT>" else temp_handle ) <EOL> return server . NOT_DONE_YET <EOL> @ GET ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def get_listings ( self , request ) : <EOL> def parse_listings ( listings ) : <EOL> if listings is not None : <EOL> response = { "<STR_LIT>" : [ ] } <EOL> for l in listings . listing : <EOL> listing_json = { <EOL> "<STR_LIT:title>" : l . title , <EOL> "<STR_LIT>" : l . contract_hash . encode ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : l . thumbnail_hash . encode ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : l . category , <EOL> "<STR_LIT>" : l . price , <EOL> "<STR_LIT>" : l . currency_code , <EOL> "<STR_LIT>" : l . nsfw , <EOL> "<STR_LIT>" : str ( CountryCode . Name ( l . origin ) ) , <EOL> "<STR_LIT>" : [ ] <EOL> } <EOL> for country in l . ships_to : <EOL> listing_json [ "<STR_LIT>" ] . append ( str ( CountryCode . Name ( country ) ) ) <EOL> response [ "<STR_LIT>" ] . append ( listing_json ) <EOL> request . setHeader ( '<STR_LIT>' , "<STR_LIT:application/json>" ) <EOL> request . write ( json . dumps ( sanitize_html ( response ) , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> else : <EOL> request . write ( json . dumps ( { } ) ) <EOL> request . finish ( ) <EOL> if "<STR_LIT>" in request . args : <EOL> def get_node ( node ) : <EOL> if node is not None : <EOL> self . mserver . get_listings ( node ) . addCallback ( parse_listings ) <EOL> else : <EOL> request . write ( json . dumps ( { } ) ) <EOL> request . finish ( ) <EOL> self . kserver . resolve ( unhexlify ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) ) . addCallback ( get_node ) <EOL> else : <EOL> ser = self . db . listings . get_proto ( ) <EOL> if ser is not None : <EOL> l = objects . Listings ( ) <EOL> l . ParseFromString ( ser ) <EOL> parse_listings ( l ) <EOL> else : <EOL> parse_listings ( None ) <EOL> return server . NOT_DONE_YET <EOL> @ GET ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def get_followers ( self , request ) : <EOL> def parse_followers ( followers ) : <EOL> if followers is not None : <EOL> response = { "<STR_LIT>" : [ ] } <EOL> for f in followers . followers : <EOL> follower_json = { <EOL> "<STR_LIT>" : f . guid . encode ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : f . metadata . handle , <EOL> "<STR_LIT:name>" : f . metadata . name , <EOL> "<STR_LIT>" : f . metadata . avatar_hash . encode ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : f . metadata . short_description , <EOL> "<STR_LIT>" : f . metadata . nsfw <EOL> } <EOL> response [ "<STR_LIT>" ] . append ( follower_json ) <EOL> request . setHeader ( '<STR_LIT>' , "<STR_LIT:application/json>" ) <EOL> request . write ( json . dumps ( sanitize_html ( response ) , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> else : <EOL> request . write ( json . dumps ( { } ) ) <EOL> request . finish ( ) <EOL> if "<STR_LIT>" in request . args : <EOL> def get_node ( node ) : <EOL> if node is not None : <EOL> self . mserver . get_followers ( node ) . addCallback ( parse_followers ) <EOL> else : <EOL> request . write ( json . dumps ( { } ) ) <EOL> request . finish ( ) <EOL> self . kserver . resolve ( unhexlify ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) ) . addCallback ( get_node ) <EOL> else : <EOL> ser = self . db . follow . get_followers ( ) <EOL> if ser is not None : <EOL> f = objects . Followers ( ) <EOL> f . ParseFromString ( ser ) <EOL> parse_followers ( f ) <EOL> else : <EOL> parse_followers ( None ) <EOL> return server . NOT_DONE_YET <EOL> @ GET ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def get_following ( self , request ) : <EOL> def parse_following ( following ) : <EOL> if following is not None : <EOL> response = { "<STR_LIT>" : [ ] } <EOL> for f in following . users : <EOL> user_json = { <EOL> "<STR_LIT>" : f . guid . encode ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : f . metadata . handle , <EOL> "<STR_LIT:name>" : f . metadata . name , <EOL> "<STR_LIT>" : f . metadata . avatar_hash . encode ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : f . metadata . short_description , <EOL> "<STR_LIT>" : f . metadata . nsfw <EOL> } <EOL> response [ "<STR_LIT>" ] . append ( user_json ) <EOL> request . setHeader ( '<STR_LIT>' , "<STR_LIT:application/json>" ) <EOL> request . write ( json . dumps ( sanitize_html ( response ) , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> else : <EOL> request . write ( json . dumps ( { } ) ) <EOL> request . finish ( ) <EOL> if "<STR_LIT>" in request . args : <EOL> def get_node ( node ) : <EOL> if node is not None : <EOL> self . mserver . get_following ( node ) . addCallback ( parse_following ) <EOL> else : <EOL> request . write ( json . dumps ( { } ) ) <EOL> request . finish ( ) <EOL> self . kserver . resolve ( unhexlify ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) ) . addCallback ( get_node ) <EOL> else : <EOL> ser = self . db . follow . get_following ( ) <EOL> if ser is not None : <EOL> f = objects . Following ( ) <EOL> f . ParseFromString ( ser ) <EOL> parse_following ( f ) <EOL> else : <EOL> parse_following ( None ) <EOL> return server . NOT_DONE_YET <EOL> @ POST ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def follow ( self , request ) : <EOL> if "<STR_LIT>" in request . args : <EOL> def get_node ( node ) : <EOL> if node is not None : <EOL> self . mserver . follow ( node ) <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : True } ) ) <EOL> request . finish ( ) <EOL> else : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT>" : "<STR_LIT>" } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> self . kserver . resolve ( unhexlify ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) ) . addCallback ( get_node ) <EOL> return server . NOT_DONE_YET <EOL> @ POST ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def unfollow ( self , request ) : <EOL> if "<STR_LIT>" in request . args : <EOL> def get_node ( node ) : <EOL> if node is not None : <EOL> self . mserver . unfollow ( node ) <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : True } ) ) <EOL> request . finish ( ) <EOL> else : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT>" : "<STR_LIT>" } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> self . kserver . resolve ( unhexlify ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) ) . addCallback ( get_node ) <EOL> return server . NOT_DONE_YET <EOL> @ POST ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def update_profile ( self , request ) : <EOL> try : <EOL> p = Profile ( self . db ) <EOL> can_update_profile = ( p . get ( ) . HasField ( "<STR_LIT>" ) or <EOL> ( "<STR_LIT:name>" in request . args and <EOL> "<STR_LIT:location>" in request . args ) ) <EOL> if not can_update_profile : <EOL> request_dict = { <EOL> "<STR_LIT:success>" : False , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> request . write ( json . dumps ( request_dict , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return False <EOL> u = objects . Profile ( ) <EOL> if "<STR_LIT:name>" in request . args : <EOL> u . name = request . args [ "<STR_LIT:name>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) <EOL> if "<STR_LIT:location>" in request . args : <EOL> u . location = CountryCode . Value ( request . args [ "<STR_LIT:location>" ] [ <NUM_LIT:0> ] . upper ( ) ) <EOL> if "<STR_LIT>" in request . args : <EOL> if blockchainid . validate ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] , self . keychain . guid . encode ( "<STR_LIT>" ) ) : <EOL> u . handle = request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) <EOL> self . db . profile . set_temp_handle ( "<STR_LIT>" ) <EOL> else : <EOL> u . handle = "<STR_LIT>" <EOL> self . db . profile . set_temp_handle ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) ) <EOL> if "<STR_LIT>" in request . args : <EOL> u . about = request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) <EOL> if "<STR_LIT>" in request . args : <EOL> u . short_description = request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) <EOL> if "<STR_LIT>" in request . args : <EOL> p . profile . nsfw = str_to_bool ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) <EOL> if "<STR_LIT>" in request . args : <EOL> p . profile . vendor = str_to_bool ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) <EOL> if "<STR_LIT>" in request . args : <EOL> p . profile . moderator = str_to_bool ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) <EOL> if "<STR_LIT>" in request . args : <EOL> p . profile . moderation_fee = round ( float ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) , <NUM_LIT:2> ) <EOL> if "<STR_LIT>" in request . args : <EOL> u . website = request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) <EOL> if "<STR_LIT:email>" in request . args : <EOL> u . email = request . args [ "<STR_LIT:email>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) <EOL> if "<STR_LIT>" in request . args : <EOL> p . profile . primary_color = int ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) <EOL> if "<STR_LIT>" in request . args : <EOL> p . profile . secondary_color = int ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) <EOL> if "<STR_LIT>" in request . args : <EOL> p . profile . background_color = int ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) <EOL> if "<STR_LIT>" in request . args : <EOL> p . profile . text_color = int ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) <EOL> if "<STR_LIT>" in request . args : <EOL> u . avatar_hash = unhexlify ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) <EOL> if "<STR_LIT>" in request . args : <EOL> u . header_hash = unhexlify ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) <EOL> if "<STR_LIT>" in request . args and "<STR_LIT>" in request . args : <EOL> p . add_pgp_key ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] , request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] , <EOL> self . keychain . guid . encode ( "<STR_LIT>" ) ) <EOL> if not p . get ( ) . HasField ( "<STR_LIT>" ) : <EOL> key = u . PublicKey ( ) <EOL> key . public_key = self . keychain . verify_key . encode ( ) <EOL> key . signature = self . keychain . signing_key . sign ( key . public_key ) [ : <NUM_LIT:64> ] <EOL> u . guid_key . MergeFrom ( key ) <EOL> p . update ( u ) <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : True } ) ) <EOL> request . finish ( ) <EOL> self . kserver . node . vendor = p . get ( ) . vendor <EOL> return server . NOT_DONE_YET <EOL> except Exception , e : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT>" : e . message } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> @ POST ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def add_social_account ( self , request ) : <EOL> try : <EOL> p = Profile ( self . db ) <EOL> if "<STR_LIT>" in request . args and "<STR_LIT:username>" in request . args : <EOL> p . add_social_account ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) , <EOL> request . args [ "<STR_LIT:username>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) , <EOL> request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) if <EOL> "<STR_LIT>" in request . args else None ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : True } ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> except Exception , e : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT>" : e . message } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> @ DELETE ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def delete_social_account ( self , request ) : <EOL> try : <EOL> p = Profile ( self . db ) <EOL> if "<STR_LIT>" in request . args : <EOL> p . remove_social_account ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : True } ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> except Exception , e : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT>" : e . message } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> @ GET ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def get_contract ( self , request ) : <EOL> def parse_contract ( contract ) : <EOL> if contract is not None : <EOL> request . setHeader ( '<STR_LIT>' , "<STR_LIT:application/json>" ) <EOL> request . write ( json . dumps ( sanitize_html ( contract ) , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> else : <EOL> request . write ( json . dumps ( { } ) ) <EOL> request . finish ( ) <EOL> if "<STR_LIT:id>" in request . args and len ( request . args [ "<STR_LIT:id>" ] [ <NUM_LIT:0> ] ) == <NUM_LIT> : <EOL> if "<STR_LIT>" in request . args and len ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) == <NUM_LIT> : <EOL> def get_node ( node ) : <EOL> if node is not None : <EOL> self . mserver . get_contract ( node , unhexlify ( request . args [ "<STR_LIT:id>" ] [ <NUM_LIT:0> ] ) ) . addCallback ( parse_contract ) <EOL> else : <EOL> request . write ( json . dumps ( { } ) ) <EOL> request . finish ( ) <EOL> self . kserver . resolve ( unhexlify ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) ) . addCallback ( get_node ) <EOL> else : <EOL> try : <EOL> with open ( self . db . filemap . get_file ( request . args [ "<STR_LIT:id>" ] [ <NUM_LIT:0> ] ) , "<STR_LIT:r>" ) as filename : <EOL> contract = json . loads ( filename . read ( ) , object_pairs_hook = OrderedDict ) <EOL> parse_contract ( contract ) <EOL> except Exception : <EOL> parse_contract ( None ) <EOL> else : <EOL> request . write ( json . dumps ( { } ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> @ POST ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def set_contract ( self , request ) : <EOL> try : <EOL> if "<STR_LIT>" in request . args : <EOL> options = { } <EOL> for option in request . args [ "<STR_LIT>" ] : <EOL> options [ option . decode ( "<STR_LIT:utf8>" ) ] = request . args [ option . decode ( "<STR_LIT:utf8>" ) ] <EOL> keywords = None <EOL> if "<STR_LIT>" in request . args : <EOL> keywords = [ ] <EOL> for keyword in request . args [ "<STR_LIT>" ] : <EOL> keywords . append ( keyword . decode ( "<STR_LIT:utf8>" ) ) <EOL> if len ( keywords ) > <NUM_LIT:10> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if "<STR_LIT>" in request . args : <EOL> c = Contract ( self . db , hash_value = unhexlify ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) , <EOL> testnet = self . protocol . testnet ) <EOL> else : <EOL> c = Contract ( self . db , testnet = self . protocol . testnet ) <EOL> c . create ( <EOL> str ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) , <EOL> request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] , <EOL> request . args [ "<STR_LIT:title>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) , <EOL> request . args [ "<STR_LIT:description>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) , <EOL> request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] , <EOL> request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] , <EOL> request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) , <EOL> str_to_bool ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) , <EOL> shipping_origin = request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] if "<STR_LIT>" in request . args else None , <EOL> shipping_regions = request . args [ "<STR_LIT>" ] if "<STR_LIT>" in request . args else None , <EOL> est_delivery_domestic = request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) <EOL> if "<STR_LIT>" in request . args else None , <EOL> est_delivery_international = request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) <EOL> if "<STR_LIT>" in request . args else None , <EOL> terms_conditions = request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) <EOL> if request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] is not "<STR_LIT>" else None , <EOL> returns = request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) <EOL> if request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] is not "<STR_LIT>" else None , <EOL> shipping_currency_code = request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] , <EOL> shipping_domestic = request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] , <EOL> shipping_international = request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] , <EOL> keywords = keywords , <EOL> category = request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) <EOL> if request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] is not "<STR_LIT>" else None , <EOL> condition = request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) <EOL> if request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] is not "<STR_LIT>" else None , <EOL> sku = request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) if request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] is not "<STR_LIT>" else None , <EOL> images = request . args [ "<STR_LIT>" ] , <EOL> free_shipping = str_to_bool ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) , <EOL> options = options if "<STR_LIT>" in request . args else None , <EOL> moderators = request . args [ "<STR_LIT>" ] if "<STR_LIT>" in request . args else None , <EOL> contract_id = request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] if "<STR_LIT>" in request . args else None ) <EOL> for keyword in request . args [ "<STR_LIT>" ] : <EOL> if keyword != "<STR_LIT>" : <EOL> self . kserver . set ( digest ( keyword . lower ( ) ) , unhexlify ( c . get_contract_id ( ) ) , <EOL> self . kserver . node . getProto ( ) . SerializeToString ( ) ) <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : True , "<STR_LIT:id>" : c . get_contract_id ( ) } ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> except Exception , e : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT>" : e . message } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> @ DELETE ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def delete_contract ( self , request ) : <EOL> try : <EOL> if "<STR_LIT:id>" in request . args : <EOL> file_path = self . db . filemap . get_file ( request . args [ "<STR_LIT:id>" ] [ <NUM_LIT:0> ] ) <EOL> with open ( file_path , '<STR_LIT:r>' ) as filename : <EOL> contract = json . load ( filename , object_pairs_hook = OrderedDict ) <EOL> c = Contract ( self . db , contract = contract ) <EOL> if "<STR_LIT>" in c . contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] : <EOL> for keyword in c . contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] : <EOL> if keyword != "<STR_LIT>" : <EOL> if isinstance ( keyword , unicode ) : <EOL> keyword = keyword . encode ( '<STR_LIT:utf8>' ) <EOL> self . kserver . delete ( keyword . lower ( ) , unhexlify ( c . get_contract_id ( ) ) , <EOL> self . keychain . signing_key . sign ( <EOL> unhexlify ( c . get_contract_id ( ) ) ) [ : <NUM_LIT:64> ] ) <EOL> if "<STR_LIT>" in request . args : <EOL> c . delete ( delete_images = True ) <EOL> else : <EOL> c . delete ( ) <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : True } ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> except Exception , e : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT>" : e . message } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> @ GET ( '<STR_LIT>' ) <EOL> def shutdown ( self , request ) : <EOL> session = request . getSession ( ) <EOL> if session not in self . authenticated_sessions and request . getHost ( ) . host != "<STR_LIT:127.0.0.1>" : <EOL> session . expire ( ) <EOL> request . setResponseCode ( <NUM_LIT> ) <EOL> request . write ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> else : <EOL> for vendor in self . protocol . vendors . values ( ) : <EOL> self . db . vendors . save_vendor ( vendor . id . encode ( "<STR_LIT>" ) , vendor . getProto ( ) . SerializeToString ( ) ) <EOL> PortMapper ( ) . clean_my_mappings ( self . kserver . node . port ) <EOL> self . protocol . shutdown ( ) <EOL> reactor . stop ( ) <EOL> return <EOL> @ POST ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def make_moderator ( self , request ) : <EOL> try : <EOL> self . mserver . make_moderator ( ) <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : True } ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> except Exception , e : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT>" : e . message } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> @ POST ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def unmake_moderator ( self , request ) : <EOL> try : <EOL> self . mserver . unmake_moderator ( ) <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : True } ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> except Exception , e : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT>" : e . message } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> @ POST ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def purchase_contract ( self , request ) : <EOL> try : <EOL> def handle_response ( resp , contract ) : <EOL> if resp : <EOL> contract . await_funding ( self . mserver . protocol . get_notification_listener ( ) , <EOL> self . protocol . blockchain , resp ) <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : True , "<STR_LIT>" : payment [ <NUM_LIT:0> ] , <EOL> "<STR_LIT>" : payment [ <NUM_LIT:1> ] , <EOL> "<STR_LIT>" : c . get_order_id ( ) } , <EOL> indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> else : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT>" : "<STR_LIT>" } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> options = None <EOL> if "<STR_LIT>" in request . args : <EOL> options = { } <EOL> for option in request . args [ "<STR_LIT>" ] : <EOL> options [ option ] = request . args [ option ] [ <NUM_LIT:0> ] <EOL> c = Contract ( self . db , hash_value = unhexlify ( request . args [ "<STR_LIT:id>" ] [ <NUM_LIT:0> ] ) , testnet = self . protocol . testnet ) <EOL> payment = c . add_purchase_info ( int ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) , <EOL> request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] , <EOL> request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) <EOL> if "<STR_LIT>" in request . args else None , <EOL> request . args [ "<STR_LIT:address>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) <EOL> if "<STR_LIT:address>" in request . args else None , <EOL> request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) <EOL> if "<STR_LIT>" in request . args else None , <EOL> request . args [ "<STR_LIT:state>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) <EOL> if "<STR_LIT:state>" in request . args else None , <EOL> request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) <EOL> if "<STR_LIT>" in request . args else None , <EOL> request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) <EOL> if "<STR_LIT>" in request . args else None , <EOL> request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] if "<STR_LIT>" in request . args else None , <EOL> options ) <EOL> def get_node ( node ) : <EOL> if node is not None : <EOL> self . mserver . purchase ( node , c ) . addCallback ( handle_response , c ) <EOL> else : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT>" : "<STR_LIT>" } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> vendor_guid = unhexlify ( c . contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT:id>" ] [ "<STR_LIT>" ] ) <EOL> self . kserver . resolve ( vendor_guid ) . addCallback ( get_node ) <EOL> return server . NOT_DONE_YET <EOL> except Exception , e : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT>" : e . message } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> @ POST ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def confirm_order ( self , request ) : <EOL> try : <EOL> def respond ( success ) : <EOL> if success is True : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : True } ) ) <EOL> request . finish ( ) <EOL> else : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT>" : success } ) ) <EOL> request . finish ( ) <EOL> file_name = request . args [ "<STR_LIT:id>" ] [ <NUM_LIT:0> ] + "<STR_LIT>" <EOL> file_path = os . path . join ( DATA_FOLDER , "<STR_LIT:store>" , "<STR_LIT>" , "<STR_LIT>" , file_name ) <EOL> with open ( file_path , '<STR_LIT:r>' ) as filename : <EOL> order = json . load ( filename , object_pairs_hook = OrderedDict ) <EOL> c = Contract ( self . db , contract = order , testnet = self . protocol . testnet ) <EOL> if "<STR_LIT>" not in c . contract : <EOL> c . add_order_confirmation ( self . protocol . blockchain , <EOL> request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] , <EOL> comments = request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) <EOL> if "<STR_LIT>" in request . args else None , <EOL> shipper = request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) <EOL> if "<STR_LIT>" in request . args else None , <EOL> tracking_number = request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) <EOL> if "<STR_LIT>" in request . args else None , <EOL> est_delivery = request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) <EOL> if "<STR_LIT>" in request . args else None , <EOL> url = request . args [ "<STR_LIT:url>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) <EOL> if "<STR_LIT:url>" in request . args else None , <EOL> password = request . args [ "<STR_LIT:password>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) <EOL> if "<STR_LIT:password>" in request . args else None ) <EOL> guid = c . contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT:id>" ] [ "<STR_LIT>" ] <EOL> self . mserver . confirm_order ( guid , c ) . addCallback ( respond ) <EOL> return server . NOT_DONE_YET <EOL> except Exception , e : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT>" : e . message } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> @ POST ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def upload_image ( self , request ) : <EOL> try : <EOL> ret = [ ] <EOL> if "<STR_LIT:image>" in request . args : <EOL> for image in request . args [ "<STR_LIT:image>" ] : <EOL> img = image . decode ( '<STR_LIT>' ) <EOL> hash_value = digest ( img ) . encode ( "<STR_LIT>" ) <EOL> with open ( os . path . join ( DATA_FOLDER , "<STR_LIT:store>" , "<STR_LIT>" , hash_value ) , '<STR_LIT:wb>' ) as outfile : <EOL> outfile . write ( img ) <EOL> self . db . filemap . insert ( hash_value , os . path . join ( "<STR_LIT:store>" , "<STR_LIT>" , hash_value ) ) <EOL> ret . append ( hash_value ) <EOL> elif "<STR_LIT>" in request . args : <EOL> avi = request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT>" ) <EOL> hash_value = digest ( avi ) . encode ( "<STR_LIT>" ) <EOL> with open ( os . path . join ( DATA_FOLDER , "<STR_LIT:store>" , "<STR_LIT>" ) , '<STR_LIT:wb>' ) as outfile : <EOL> outfile . write ( avi ) <EOL> self . db . filemap . insert ( hash_value , os . path . join ( "<STR_LIT:store>" , "<STR_LIT>" ) ) <EOL> ret . append ( hash_value ) <EOL> elif "<STR_LIT>" in request . args : <EOL> hdr = request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT>" ) <EOL> hash_value = digest ( hdr ) . encode ( "<STR_LIT>" ) <EOL> with open ( os . path . join ( DATA_FOLDER , "<STR_LIT:store>" , "<STR_LIT>" ) , '<STR_LIT:wb>' ) as outfile : <EOL> outfile . write ( hdr ) <EOL> self . db . filemap . insert ( hash_value , os . path . join ( "<STR_LIT:store>" , "<STR_LIT>" ) ) <EOL> ret . append ( hash_value ) <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : True , "<STR_LIT>" : ret } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> except Exception , e : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT>" : e . message } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> @ POST ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def complete_order ( self , request ) : <EOL> def respond ( success ) : <EOL> if success is True : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : True } ) ) <EOL> request . finish ( ) <EOL> else : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT>" : success } ) ) <EOL> request . finish ( ) <EOL> file_path = os . path . join ( DATA_FOLDER , "<STR_LIT>" , "<STR_LIT>" , request . args [ "<STR_LIT:id>" ] [ <NUM_LIT:0> ] + "<STR_LIT>" ) <EOL> if not os . path . exists ( file_path ) : <EOL> file_path = os . path . join ( DATA_FOLDER , "<STR_LIT>" , "<STR_LIT>" , request . args [ "<STR_LIT:id>" ] [ <NUM_LIT:0> ] + "<STR_LIT>" ) <EOL> with open ( file_path , '<STR_LIT:r>' ) as filename : <EOL> order = json . load ( filename , object_pairs_hook = OrderedDict ) <EOL> c = Contract ( self . db , contract = order , testnet = self . protocol . testnet ) <EOL> if "<STR_LIT>" not in c . contract : <EOL> c . add_receipt ( True , <EOL> self . protocol . blockchain , <EOL> feedback = request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] if "<STR_LIT>" in request . args else None , <EOL> quality = request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] if "<STR_LIT>" in request . args else None , <EOL> description = request . args [ "<STR_LIT:description>" ] [ <NUM_LIT:0> ] if "<STR_LIT:description>" in request . args else None , <EOL> delivery_time = request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> if "<STR_LIT>" in request . args else None , <EOL> customer_service = request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> if "<STR_LIT>" in request . args else None , <EOL> review = request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) if "<STR_LIT>" in request . args else "<STR_LIT>" , <EOL> anonymous = str_to_bool ( request . args [ "<STR_LIT>" ] ) if "<STR_LIT>" in request . args else True ) <EOL> guid = c . contract [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT:id>" ] [ "<STR_LIT>" ] <EOL> self . mserver . complete_order ( guid , c ) . addCallback ( respond ) <EOL> return server . NOT_DONE_YET <EOL> @ POST ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def set_settings ( self , request ) : <EOL> try : <EOL> settings = self . db . settings <EOL> resolver = RESOLVER if "<STR_LIT>" not in request . args or request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] == "<STR_LIT>" else request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> if "<STR_LIT>" in request . args and request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] != "<STR_LIT>" and request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] != "<STR_LIT:null>" : <EOL> if self . protocol . testnet : <EOL> set_value ( "<STR_LIT>" , "<STR_LIT>" , <EOL> request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) <EOL> else : <EOL> set_value ( "<STR_LIT>" , "<STR_LIT>" , <EOL> request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) <EOL> else : <EOL> if self . protocol . testnet : <EOL> if get_value ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> delete_value ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> else : <EOL> if get_value ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> delete_value ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if resolver != get_value ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> set_value ( "<STR_LIT>" , "<STR_LIT>" , resolver ) <EOL> settings_list = settings . get ( ) <EOL> if "<STR_LIT>" in request . args and settings_list is not None : <EOL> mod_json = settings_list [ <NUM_LIT:11> ] <EOL> if mod_json != "<STR_LIT>" : <EOL> prev_mods = json . loads ( mod_json ) <EOL> current_mods = request . args [ "<STR_LIT>" ] <EOL> to_add = list ( set ( current_mods ) - set ( prev_mods ) ) <EOL> to_remove = list ( set ( prev_mods ) - set ( current_mods ) ) <EOL> if len ( to_remove ) > <NUM_LIT:0> or len ( to_add ) > <NUM_LIT:0> : <EOL> self . mserver . update_moderators_on_listings ( request . args [ "<STR_LIT>" ] ) <EOL> settings . update ( <EOL> request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] , <EOL> request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] , <EOL> request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] , <EOL> request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] , <EOL> request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] , <EOL> <NUM_LIT:1> if str_to_bool ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) else <NUM_LIT:0> , <EOL> json . dumps ( request . args [ "<STR_LIT>" ] if request . args [ "<STR_LIT>" ] != "<STR_LIT>" else [ ] ) , <EOL> json . dumps ( request . args [ "<STR_LIT>" ] if request . args [ "<STR_LIT>" ] != "<STR_LIT>" else [ ] ) , <EOL> request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] , <EOL> request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] , <EOL> json . dumps ( request . args [ "<STR_LIT>" ] if request . args [ "<STR_LIT>" ] != "<STR_LIT>" else [ ] ) <EOL> ) <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : True } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> except Exception , e : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT>" : e . message } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> @ GET ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def get_settings ( self , request ) : <EOL> settings = self . db . settings . get ( ) <EOL> if settings is None : <EOL> request . write ( json . dumps ( { } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> else : <EOL> if self . protocol . nat_type == objects . FULL_CONE : <EOL> nat_type = "<STR_LIT>" <EOL> elif self . protocol . nat_type == objects . RESTRICTED : <EOL> nat_type = "<STR_LIT>" <EOL> else : <EOL> nat_type = "<STR_LIT>" <EOL> settings_json = { <EOL> "<STR_LIT>" : settings [ <NUM_LIT:1> ] , <EOL> "<STR_LIT>" : settings [ <NUM_LIT:2> ] , <EOL> "<STR_LIT>" : settings [ <NUM_LIT:3> ] , <EOL> "<STR_LIT>" : settings [ <NUM_LIT:4> ] , <EOL> "<STR_LIT>" : settings [ <NUM_LIT:5> ] , <EOL> "<STR_LIT>" : True if settings [ <NUM_LIT:6> ] == <NUM_LIT:1> else False , <EOL> "<STR_LIT>" : json . loads ( settings [ <NUM_LIT:7> ] ) , <EOL> "<STR_LIT>" : json . loads ( settings [ <NUM_LIT:8> ] ) , <EOL> "<STR_LIT>" : get_value ( <EOL> "<STR_LIT>" , "<STR_LIT>" ) if self . protocol . testnet else get_value ( <EOL> "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : KeyChain ( self . db ) . signing_key . encode ( encoder = nacl . encoding . HexEncoder ) , <EOL> "<STR_LIT>" : "<STR_LIT>" if settings [ <NUM_LIT:9> ] is None else settings [ <NUM_LIT:9> ] , <EOL> "<STR_LIT>" : "<STR_LIT>" if settings [ <NUM_LIT:10> ] is None else settings [ <NUM_LIT:10> ] , <EOL> "<STR_LIT>" : get_value ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : nat_type , <EOL> "<STR_LIT>" : TRANSACTION_FEE <EOL> } <EOL> mods = [ ] <EOL> try : <EOL> for guid in json . loads ( settings [ <NUM_LIT:11> ] ) : <EOL> info = self . db . moderators . get_moderator ( guid ) <EOL> if info is not None : <EOL> m = { <EOL> "<STR_LIT>" : guid , <EOL> "<STR_LIT>" : info [ <NUM_LIT:4> ] , <EOL> "<STR_LIT:name>" : info [ <NUM_LIT:5> ] , <EOL> "<STR_LIT>" : info [ <NUM_LIT:7> ] . encode ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : info [ <NUM_LIT:6> ] , <EOL> "<STR_LIT>" : info [ <NUM_LIT:8> ] <EOL> } <EOL> mods . append ( m ) <EOL> except Exception : <EOL> pass <EOL> settings_json [ "<STR_LIT>" ] = mods <EOL> request . setHeader ( '<STR_LIT>' , "<STR_LIT:application/json>" ) <EOL> request . write ( json . dumps ( sanitize_html ( settings_json ) , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> @ GET ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def get_connected_peers ( self , request ) : <EOL> request . setHeader ( '<STR_LIT>' , "<STR_LIT:application/json>" ) <EOL> peers = self . protocol . keys ( ) <EOL> resp = { <EOL> "<STR_LIT>" : len ( peers ) , <EOL> "<STR_LIT>" : peers <EOL> } <EOL> request . write ( json . dumps ( sanitize_html ( resp ) , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> @ GET ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def get_routing_table ( self , request ) : <EOL> nodes = [ ] <EOL> for bucket in self . kserver . protocol . router . buckets : <EOL> for node in bucket . nodes . values ( ) : <EOL> n = { <EOL> "<STR_LIT>" : node . id . encode ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : node . ip , <EOL> "<STR_LIT:port>" : node . port , <EOL> "<STR_LIT>" : node . vendor , <EOL> "<STR_LIT>" : objects . NATType . Name ( node . nat_type ) <EOL> } <EOL> nodes . append ( n ) <EOL> request . setHeader ( '<STR_LIT>' , "<STR_LIT:application/json>" ) <EOL> request . write ( json . dumps ( sanitize_html ( nodes ) , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> @ GET ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def get_notifications ( self , request ) : <EOL> limit = int ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) if "<STR_LIT>" in request . args else <NUM_LIT:20> <EOL> start = request . args [ "<STR_LIT:start>" ] [ <NUM_LIT:0> ] if "<STR_LIT:start>" in request . args else "<STR_LIT>" <EOL> notifications = self . db . notifications . get_notifications ( start , limit ) <EOL> notification_dict = { <EOL> "<STR_LIT>" : self . db . notifications . get_unread_count ( ) , <EOL> "<STR_LIT>" : [ ] <EOL> } <EOL> for n in notifications [ : : - <NUM_LIT:1> ] : <EOL> notification_json = { <EOL> "<STR_LIT:id>" : n [ <NUM_LIT:0> ] , <EOL> "<STR_LIT>" : n [ <NUM_LIT:1> ] , <EOL> "<STR_LIT>" : n [ <NUM_LIT:2> ] , <EOL> "<STR_LIT:type>" : n [ <NUM_LIT:3> ] , <EOL> "<STR_LIT>" : n [ <NUM_LIT:4> ] , <EOL> "<STR_LIT:title>" : n [ <NUM_LIT:5> ] , <EOL> "<STR_LIT>" : n [ <NUM_LIT:6> ] , <EOL> "<STR_LIT>" : n [ <NUM_LIT:7> ] . encode ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : False if n [ <NUM_LIT:8> ] == <NUM_LIT:0> else True <EOL> } <EOL> notification_dict [ "<STR_LIT>" ] . append ( notification_json ) <EOL> request . setHeader ( '<STR_LIT>' , "<STR_LIT:application/json>" ) <EOL> request . write ( json . dumps ( sanitize_html ( notification_dict ) , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> @ POST ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def mark_notification_as_read ( self , request ) : <EOL> try : <EOL> for notif_id in request . args [ "<STR_LIT:id>" ] : <EOL> self . db . notifications . mark_as_read ( notif_id ) <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : True } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> except Exception , e : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT>" : e . message } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> @ POST ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def broadcast ( self , request ) : <EOL> try : <EOL> def get_response ( num ) : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : True , "<STR_LIT>" : num } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> self . mserver . broadcast ( request . args [ "<STR_LIT:message>" ] [ <NUM_LIT:0> ] ) . addCallback ( get_response ) <EOL> return server . NOT_DONE_YET <EOL> except Exception , e : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT>" : e . message } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> @ GET ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def get_chat_messages ( self , request ) : <EOL> start = request . args [ "<STR_LIT:start>" ] [ <NUM_LIT:0> ] if "<STR_LIT:start>" in request . args else None <EOL> messages = self . db . messages . get_messages ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] , "<STR_LIT>" , start ) <EOL> message_list = [ ] <EOL> for m in messages [ : : - <NUM_LIT:1> ] : <EOL> message_json = { <EOL> "<STR_LIT:id>" : m [ <NUM_LIT:11> ] , <EOL> "<STR_LIT>" : m [ <NUM_LIT:0> ] , <EOL> "<STR_LIT>" : m [ <NUM_LIT:1> ] , <EOL> "<STR_LIT:message>" : m [ <NUM_LIT:5> ] , <EOL> "<STR_LIT>" : m [ <NUM_LIT:6> ] , <EOL> "<STR_LIT>" : m [ <NUM_LIT:7> ] . encode ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : False if m [ <NUM_LIT:9> ] == <NUM_LIT:0> else True , <EOL> "<STR_LIT>" : False if m [ <NUM_LIT:10> ] == <NUM_LIT:0> else True <EOL> } <EOL> message_list . append ( message_json ) <EOL> request . setHeader ( '<STR_LIT>' , "<STR_LIT:application/json>" ) <EOL> request . write ( json . dumps ( sanitize_html ( message_list ) , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> @ GET ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def get_chat_conversations ( self , request ) : <EOL> messages = self . db . messages . get_conversations ( ) <EOL> request . setHeader ( '<STR_LIT>' , "<STR_LIT:application/json>" ) <EOL> request . write ( json . dumps ( messages , indent = <NUM_LIT:4> ) . encode ( "<STR_LIT:utf-8>" ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> @ DELETE ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def delete_conversations ( self , request ) : <EOL> try : <EOL> self . db . messages . delete_messages ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : True } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> except Exception , e : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT>" : e . message } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> @ POST ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def mark_chat_message_as_read ( self , request ) : <EOL> try : <EOL> self . db . messages . mark_as_read ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : True } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> except Exception , e : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT>" : e . message } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> @ GET ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def get_sales ( self , request ) : <EOL> sales = self . db . sales . get_all ( ) <EOL> sales_list = [ ] <EOL> for sale in sales : <EOL> sale_json = { <EOL> "<STR_LIT>" : sale [ <NUM_LIT:0> ] , <EOL> "<STR_LIT:title>" : sale [ <NUM_LIT:1> ] , <EOL> "<STR_LIT:description>" : sale [ <NUM_LIT:2> ] , <EOL> "<STR_LIT>" : sale [ <NUM_LIT:3> ] , <EOL> "<STR_LIT>" : sale [ <NUM_LIT:4> ] , <EOL> "<STR_LIT:status>" : sale [ <NUM_LIT:5> ] , <EOL> "<STR_LIT>" : sale [ <NUM_LIT:6> ] , <EOL> "<STR_LIT>" : sale [ <NUM_LIT:7> ] , <EOL> "<STR_LIT>" : sale [ <NUM_LIT:8> ] <EOL> } <EOL> sales_list . append ( sale_json ) <EOL> request . setHeader ( '<STR_LIT>' , "<STR_LIT:application/json>" ) <EOL> request . write ( json . dumps ( sanitize_html ( sales_list ) , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> @ GET ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def get_purchases ( self , request ) : <EOL> purchases = self . db . purchases . get_all ( ) <EOL> purchases_list = [ ] <EOL> for purchase in purchases : <EOL> purchase_json = { <EOL> "<STR_LIT>" : purchase [ <NUM_LIT:0> ] , <EOL> "<STR_LIT:title>" : purchase [ <NUM_LIT:1> ] , <EOL> "<STR_LIT:description>" : purchase [ <NUM_LIT:2> ] , <EOL> "<STR_LIT>" : purchase [ <NUM_LIT:3> ] , <EOL> "<STR_LIT>" : purchase [ <NUM_LIT:4> ] , <EOL> "<STR_LIT:status>" : purchase [ <NUM_LIT:5> ] , <EOL> "<STR_LIT>" : purchase [ <NUM_LIT:6> ] , <EOL> "<STR_LIT>" : purchase [ <NUM_LIT:7> ] , <EOL> "<STR_LIT>" : purchase [ <NUM_LIT:8> ] <EOL> } <EOL> purchases_list . append ( purchase_json ) <EOL> request . setHeader ( '<STR_LIT>' , "<STR_LIT:application/json>" ) <EOL> request . write ( json . dumps ( sanitize_html ( purchases_list ) , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> @ POST ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def check_for_payment ( self , request ) : <EOL> if not self . protocol . blockchain . connected : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT>" : "<STR_LIT>" } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> try : <EOL> check_order_for_payment ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] , self . db , <EOL> self . protocol . blockchain , <EOL> self . mserver . protocol . get_notification_listener ( ) , <EOL> self . protocol . testnet ) <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : True } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> except Exception , e : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT>" : e . message } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> @ GET ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def get_order ( self , request ) : <EOL> filename = request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] + "<STR_LIT>" <EOL> if os . path . exists ( os . path . join ( DATA_FOLDER , "<STR_LIT>" , "<STR_LIT>" , filename ) ) : <EOL> file_path = os . path . join ( DATA_FOLDER , "<STR_LIT>" , "<STR_LIT>" , filename ) <EOL> status = self . db . purchases . get_status ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) <EOL> elif os . path . exists ( os . path . join ( DATA_FOLDER , "<STR_LIT>" , "<STR_LIT>" , filename ) ) : <EOL> file_path = os . path . join ( DATA_FOLDER , "<STR_LIT>" , "<STR_LIT>" , filename ) <EOL> status = self . db . purchases . get_status ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) <EOL> elif os . path . exists ( os . path . join ( DATA_FOLDER , "<STR_LIT>" , "<STR_LIT>" , filename ) ) : <EOL> file_path = os . path . join ( DATA_FOLDER , "<STR_LIT>" , "<STR_LIT>" , filename ) <EOL> status = self . db . purchases . get_status ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) <EOL> elif os . path . exists ( os . path . join ( DATA_FOLDER , "<STR_LIT:store>" , "<STR_LIT>" , "<STR_LIT>" , filename ) ) : <EOL> file_path = os . path . join ( DATA_FOLDER , "<STR_LIT:store>" , "<STR_LIT>" , "<STR_LIT>" , filename ) <EOL> status = self . db . sales . get_status ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) <EOL> elif os . path . exists ( os . path . join ( DATA_FOLDER , "<STR_LIT:store>" , "<STR_LIT>" , "<STR_LIT>" , filename ) ) : <EOL> file_path = os . path . join ( DATA_FOLDER , "<STR_LIT:store>" , "<STR_LIT>" , "<STR_LIT>" , filename ) <EOL> status = self . db . sales . get_status ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) <EOL> elif os . path . exists ( os . path . join ( DATA_FOLDER , "<STR_LIT:store>" , "<STR_LIT>" , "<STR_LIT>" , filename ) ) : <EOL> file_path = os . path . join ( DATA_FOLDER , "<STR_LIT:store>" , "<STR_LIT>" , "<STR_LIT>" , filename ) <EOL> status = self . db . sales . get_status ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) <EOL> elif os . path . exists ( os . path . join ( DATA_FOLDER , "<STR_LIT>" , filename ) ) : <EOL> file_path = os . path . join ( DATA_FOLDER , "<STR_LIT>" , filename ) <EOL> status = <NUM_LIT:4> <EOL> else : <EOL> request . write ( json . dumps ( { } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> with open ( file_path , '<STR_LIT:r>' ) as filename : <EOL> order = json . load ( filename , object_pairs_hook = OrderedDict ) <EOL> if status == <NUM_LIT:0> or status == <NUM_LIT:2> : <EOL> check_order_for_payment ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] , self . db , self . protocol . blockchain , <EOL> self . mserver . protocol . get_notification_listener ( ) , <EOL> self . protocol . testnet ) <EOL> def return_order ( ) : <EOL> request . setHeader ( '<STR_LIT>' , "<STR_LIT:application/json>" ) <EOL> request . write ( json . dumps ( sanitize_html ( order ) , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> def height_fetched ( ec , chain_height ) : <EOL> payment_address = order [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT:address>" ] <EOL> txs = [ ] <EOL> def history_fetched ( ec , history ) : <EOL> if ec : <EOL> return_order ( ) <EOL> elif timeout . active ( ) : <EOL> timeout . cancel ( ) <EOL> for tx_type , txid , i , height , value in history : <EOL> tx = { <EOL> "<STR_LIT>" : txid . encode ( "<STR_LIT>" ) , <EOL> "<STR_LIT:value>" : round ( float ( value ) / <NUM_LIT> , <NUM_LIT:8> ) , <EOL> "<STR_LIT>" : chain_height - height + <NUM_LIT:1> if height != <NUM_LIT:0> else <NUM_LIT:0> <EOL> } <EOL> if tx_type == obelisk . PointIdent . Output : <EOL> tx [ "<STR_LIT:type>" ] = "<STR_LIT>" <EOL> else : <EOL> tx [ "<STR_LIT:type>" ] = "<STR_LIT>" <EOL> txs . append ( tx ) <EOL> order [ "<STR_LIT>" ] = txs <EOL> request . setHeader ( '<STR_LIT>' , "<STR_LIT:application/json>" ) <EOL> request . write ( json . dumps ( order , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> self . protocol . blockchain . fetch_history2 ( payment_address , history_fetched ) <EOL> if self . protocol . blockchain . connected : <EOL> self . protocol . blockchain . fetch_last_height ( height_fetched ) <EOL> timeout = reactor . callLater ( <NUM_LIT:4> , return_order ) <EOL> else : <EOL> return_order ( ) <EOL> return server . NOT_DONE_YET <EOL> @ POST ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def dispute_contract ( self , request ) : <EOL> try : <EOL> self . mserver . open_dispute ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] , <EOL> request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) if "<STR_LIT>" in request . args else None ) <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : True } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> except Exception , e : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT>" : e . message } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> @ POST ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def close_dispute ( self , request ) : <EOL> try : <EOL> def cb ( resp ) : <EOL> if resp : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : True } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> else : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT>" : resp } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> d = self . mserver . close_dispute ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] , <EOL> request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] . decode ( "<STR_LIT:utf8>" ) <EOL> if "<STR_LIT>" in request . args else None , <EOL> request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> if "<STR_LIT>" in request . args else None , <EOL> request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> if "<STR_LIT>" in request . args else None , <EOL> request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> if "<STR_LIT>" in request . args else None , <EOL> request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> if "<STR_LIT>" in request . args else None ) <EOL> d . addCallback ( cb ) <EOL> return server . NOT_DONE_YET <EOL> except Exception , e : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT>" : e . message } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> @ POST ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def release_funds ( self , request ) : <EOL> try : <EOL> self . mserver . release_funds ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : True } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> except Exception , e : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT>" : e . message } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> @ GET ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def get_cases ( self , request ) : <EOL> cases = self . db . cases . get_all ( ) <EOL> cases_list = [ ] <EOL> for case in cases : <EOL> purchase_json = { <EOL> "<STR_LIT>" : case [ <NUM_LIT:0> ] , <EOL> "<STR_LIT:title>" : case [ <NUM_LIT:1> ] , <EOL> "<STR_LIT>" : case [ <NUM_LIT:2> ] , <EOL> "<STR_LIT>" : case [ <NUM_LIT:3> ] , <EOL> "<STR_LIT>" : case [ <NUM_LIT:4> ] , <EOL> "<STR_LIT>" : case [ <NUM_LIT:5> ] , <EOL> "<STR_LIT>" : case [ <NUM_LIT:6> ] , <EOL> "<STR_LIT>" : case [ <NUM_LIT:7> ] , <EOL> "<STR_LIT>" : json . loads ( case [ <NUM_LIT:8> ] ) , <EOL> "<STR_LIT:status>" : "<STR_LIT>" if case [ <NUM_LIT:10> ] == <NUM_LIT:1> else "<STR_LIT>" <EOL> } <EOL> cases_list . append ( purchase_json ) <EOL> request . setHeader ( '<STR_LIT>' , "<STR_LIT:application/json>" ) <EOL> request . write ( json . dumps ( sanitize_html ( cases_list ) , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> @ GET ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def order_messages ( self , request ) : <EOL> message_list = [ ] <EOL> messages = self . db . messages . get_order_messages ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) <EOL> for m in messages : <EOL> if m [ <NUM_LIT:0> ] is not None : <EOL> message_json = { <EOL> "<STR_LIT>" : m [ <NUM_LIT:0> ] , <EOL> "<STR_LIT>" : m [ <NUM_LIT:1> ] , <EOL> "<STR_LIT:message>" : m [ <NUM_LIT:5> ] , <EOL> "<STR_LIT>" : m [ <NUM_LIT:6> ] , <EOL> "<STR_LIT>" : m [ <NUM_LIT:7> ] . encode ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : m [ <NUM_LIT:4> ] , <EOL> "<STR_LIT>" : False if m [ <NUM_LIT:9> ] == <NUM_LIT:0> else True <EOL> } <EOL> message_list . append ( message_json ) <EOL> request . setHeader ( '<STR_LIT>' , "<STR_LIT:application/json>" ) <EOL> request . write ( json . dumps ( sanitize_html ( message_list ) , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> @ GET ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def get_ratings ( self , request ) : <EOL> def parse_response ( ratings ) : <EOL> if ratings is not None : <EOL> request . setHeader ( '<STR_LIT>' , "<STR_LIT:application/json>" ) <EOL> request . write ( json . dumps ( sanitize_html ( ratings ) , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> else : <EOL> request . write ( json . dumps ( { } ) ) <EOL> request . finish ( ) <EOL> if "<STR_LIT>" in request . args : <EOL> def get_node ( node ) : <EOL> if node is not None : <EOL> if "<STR_LIT>" in request . args and request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] != "<STR_LIT>" : <EOL> self . mserver . get_ratings ( node , unhexlify ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) ) . addCallback ( parse_response ) <EOL> else : <EOL> self . mserver . get_ratings ( node ) . addCallback ( parse_response ) <EOL> else : <EOL> request . write ( json . dumps ( { } ) ) <EOL> request . finish ( ) <EOL> self . kserver . resolve ( unhexlify ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) ) . addCallback ( get_node ) <EOL> else : <EOL> ratings = [ ] <EOL> if "<STR_LIT>" in request . args and request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] != "<STR_LIT>" : <EOL> for rating in self . db . ratings . get_listing_ratings ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) : <EOL> ratings . append ( json . loads ( rating [ <NUM_LIT:0> ] ) ) <EOL> else : <EOL> for rating in self . db . ratings . get_all_ratings ( ) : <EOL> ratings . append ( json . loads ( rating [ <NUM_LIT:0> ] ) ) <EOL> request . setHeader ( '<STR_LIT>' , "<STR_LIT:application/json>" ) <EOL> request . write ( json . dumps ( sanitize_html ( ratings ) , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> @ GET ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def btc_price ( self , request ) : <EOL> request . setHeader ( '<STR_LIT>' , "<STR_LIT:application/json>" ) <EOL> if "<STR_LIT>" in request . args : <EOL> try : <EOL> result = BtcPrice . instance ( ) . get ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] . upper ( ) , False ) <EOL> request . write ( json . dumps ( { "<STR_LIT>" : result , "<STR_LIT>" : BtcPrice . instance ( ) . prices } ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> except KeyError : <EOL> pass <EOL> request . write ( json . dumps ( { "<STR_LIT>" : BtcPrice . instance ( ) . prices } ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> @ POST ( '<STR_LIT>' ) <EOL> @ authenticated <EOL> def refund ( self , request ) : <EOL> try : <EOL> def respond ( success ) : <EOL> if success is True : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : True } ) ) <EOL> request . finish ( ) <EOL> else : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT>" : success } ) ) <EOL> request . finish ( ) <EOL> self . mserver . refund ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) . addCallback ( respond ) <EOL> return server . NOT_DONE_YET <EOL> except Exception , e : <EOL> request . write ( json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT>" : e . message } , indent = <NUM_LIT:4> ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET <EOL> class RestAPI ( Site ) : <EOL> def __init__ ( self , mserver , kserver , openbazaar_protocol , username , password , <EOL> authenticated_sessions , only_ip = None , timeout = <NUM_LIT> * <NUM_LIT> * <NUM_LIT:1> ) : <EOL> if only_ip == None : <EOL> only_ip = [ "<STR_LIT:127.0.0.1>" ] <EOL> self . only_ip = only_ip <EOL> api_resource = OpenBazaarAPI ( mserver , kserver , openbazaar_protocol , <EOL> username , password , authenticated_sessions ) <EOL> Site . __init__ ( self , api_resource , timeout = timeout ) <EOL> def buildProtocol ( self , addr ) : <EOL> if addr . host not in self . only_ip and "<STR_LIT>" not in self . only_ip : <EOL> return <EOL> return Site . buildProtocol ( self , addr ) </s>
