Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

DOWNLOAD

...

AS

...

A

...

FILE

#!/ usr/bin/env

...

python
# By:

...

Kelcey

...

Damage,

...

2012

import hashlib,

...

hmac,

...

string.

...

base64,

...

urllib

# Reference GET/POST

...

sample

...

request
#
# 1. http://10.10.0.1:8080/client/api

...


# 2.

...

?apiKey=

...

API_KEY

...


# 3.

...

&command=

...

API_COMMAND

...


# 4.

...

&response=

...

JSON/XML

...


# 5.

...

&zoneid=

...

ZONE_ID

...


# 6.

...

&signature=

...

SIGNATURE

# Reference required signature elements
#
# 1. request
# 2. apiKey
# 3. private_key

api_url = 'http://10.10.0.1:8080/client/api'

...

secret = Secret Key

apiKey = API Key

request =  {'command':

...

'restartNetwork',

...

'id':

...

'214',

...

'response':

...

'json'}

...

class SignedAPICall(Object):

...


    def _init_(self,

...

api_url,

...

apikey,,

...

secret):

...


        self.api_url

...

=

...

api_url

...


        self.apikey = apiKey
        self.secret = secret

Wiki Markup
{{    def request(self, args):}}
{{        args\['apiKey'\] = self.apiKey}}

...

Wiki Markup
{{        self.params = \[\]}}
{{        self._sort_request(args)}}
{{        self._creat_signature()}}
{{        self._build_post_request()}}

...

    def _sort_request(self,

...

args):

...


        keys = sorted(args.keys())

...

Wiki Markup
{{        for key in keys:}}
{{            self.params.append(key + '=' + urllib.quote_plus(args\[key\]))}}

...

    def _create_signature(self):

...


        self.query = '&'.join(self.params)

...


        digest = hmac.new(
            self.secret,
            msg=self.query.lower(),

...


            digestmod=hashlib.sha1).digest()

...

        self.signature = base64.b64encode(digest)

...

    def _build_post_request(self):

...


        self.query += '&signature='

...

+

...

urllib.quote_plus(self.signature)

...


        self.value = self.api_url

...

+

...

'?'

...

+

...

self.query

...

api = SignedAPICall(api_url,

...

apiKey,,

...

secret)

...


api.request(request)

...

print api.value

...