Author

Topic: Intersango API python example (Read 1725 times)

sr. member
Activity: 463
Merit: 252
September 30, 2011, 11:38:59 AM
#1
This is a very basic framework for the intersango API in python.

Code:
import httplib
import urllib
import json
import decimal
import threading
import socket

class intersango:
    version = "0.1"
   
    def __init__(self,api_key):
        self.api_key=api_key
        self.connection = None
   
    def connect(self):
        if self.connection is None:
            self.connection = httplib.HTTPSConnection('intersango.com')
   
    def make_request(self,call_name,params):
        params.append(('api_key',self.api_key))
        body = urllib.urlencode(params)
       
        self.connect()
       
        try:
            self.connection.putrequest('POST','/api/authenticated/v'+self.version+'/'+call_name+'.php')
            self.connection.putheader('Connection','Keep-Alive')
            self.connection.putheader('Keep-Alive','30')
            self.connection.putheader('Content-type','application/x-www-form-urlencoded')
            self.connection.putheader('Content-length',len(body))
            self.connection.endheaders()
           
            self.connection.send(body)
           
            response = self.connection.getresponse()
            return json.load(response)
        except httplib.HTTPException:
            self.connection = None
            return False
        except socket.error:
            self.connection = None
            return False
       
    def list_accounts(self):
        return self.make_request('listAccounts',dict())
   
    def list_orders(self,account_id,last_order_id=None,states=None):
        params = [('account_id',account_id)]
       
        if last_order_id is not None:
            params.append(('last_order_id',last_order_id))
       
        if states is not None:
            for state in states:
                params.append(('states[]',state))
       
        return self.make_request('listOrders',params)
   
    def list_deposits(self,account_id):
        return self.make_request('listDeposits',[('account_id',account_id)])
   
    def list_withdrawal_requests(self,account_id):
        return self.make_request('listWithdrawalRequests',[('account_id',account_id)])
   
    def place_limit_order(self,quantity,rate,selling,base_account_id,quote_account_id):
        params = []
        assert type(quantity) == decimal.Decimal
        assert type(rate) == decimal.Decimal
       
        params.append(('quantity',quantity))
        params.append(('rate',rate))
        params.append(('selling',str(selling).lower()))
        params.append(('base_account_id',base_account_id))
        params.append(('quote_account_id',quote_account_id))
       
        return self.make_request('placeLimitOrder',params)
   
    def request_cancel_order(self,account_id,order_id):
        return self.make_request('requestCancelOrder',[('account_id',account_id),('order_id',order_id)])

b = intersango('your api key goes here')
Jump to: