Exemplo n.º 1
0
def _set_flavor(attributes, vm_id, current_flavor, new_flavor):
    vm_status = _get_status(attributes)
    conn = Connection(attributes["cm_nova_url"], username="", password="")
    tenant_id, x_auth_token = _get_keystone_tokens(attributes)
    if (vm_status == 'ACTIVE' and current_flavor != new_flavor):
        body = '{"resize": {"flavorRef":"'+ new_flavor + '"}}'
        headers = {"Content-type": "application/json", "x-auth-token": x_auth_token.encode()}
        uri = tenant_id + "/servers/" + vm_id + "/action"
        resp = conn.request_post(uri, body=body, headers=headers)
        status = resp[u'headers']['status']
        if status == '200' or status == '304' or status == '202':
            return _get_flavor(attributes, vm_id)
        else:
            log.error("Bad HTTP return code: %s" % status)
    elif (vm_status == 'RESIZE'):
        log.error("Wait for VM resizing before confirming action")
    elif (vm_status == 'VERIFY_RESIZE'):
        body = '{"confirmResize": null}'
        headers = {"Content-type": "application/json", "x-auth-token": x_auth_token.encode()}
        uri = tenant_id + "/servers/" + vm_id + "/action"
        resp = conn.request_post(uri, body=body, headers=headers)
        status = resp[u'headers']['status']
        if status == '200' or status == '304' or status == '202':
            return _get_flavor(attributes, vm_id)
        else:
            log.error("_set_flavor: Bad HTTP return code: %s" % status)
    else:
        log.error("Wrong VM state or wring destination flavor")
Exemplo n.º 2
0
class LocalClient(object):
    '''
    classdocs
    '''


    def __init__(self):
        '''
        Constructor
        '''
        self.ip="localhost"
        self.port="81"
        self.protocol="http"
        self.path="/nes_server"
         
        
    def set_connection(self):
        self.base_url= self.protocol + "://" + self.ip +":"+ str(self.port) + self.path
        self.conn = Connection(self.base_url)
        
    def create_subscription(self, json_data):
        response = self.conn.request_post("/subs.json")
        response['headers']['location']="http://212.179.159.77/nesphase2/nes/subscriptions/1234567891321"
        return response['headers']['status'],response['headers']['location'], response['body']

    def delete_subscription(self, correlators_list):
        
        return 200, "{}"    
Exemplo n.º 3
0
class NESClient(object):
    '''
    classdocs
    '''


    def __init__(self, appId, servId):
        '''
        Constructor
        '''
        self.ip="pre.3rd.services.telefonica.es"
        self.port="444"
        self.protocol="https"
        self.path="/services/BA/REST/UCSS/UCSSServer/"
        self.appId = appId
        self.servId = servId 
        self.headers = {'appId' : self.appId, 'servId' : self.servId}
        
    def set_connection(self):
        self.base_url= self.protocol + "://" + self.ip +":"+ str(self.port) + self.path
        self.conn = Connection(self.base_url, username='******', password='******')
        
    def create_subscription(self, json_data):
        
        response = self.conn.request_post("nes/subscriptions/",body=json_data, headers=self.headers)
        print "create_subscription"
        
        return response['headers']['status'], response['headers']['location'], response['body']
    
    def delete_subscription(self, correlators_list):
        
        response = self.conn.request_delete("nes/subscriptions?correlators="+correlators_list, headers=self.headers)
        print "delete_subscription"
        
        return response['headers']['status'], response['body']
Exemplo n.º 4
0
def _resume(attributes):
    '''
    Pauses a VM.

    @param attributes: the dictionary of the attributes that will be used to
                        pause a virtual machine
    @type attributes: dict
    '''
    vm = _get_VM(attributes)

    if _get_status(attributes) == "PAUSED":
        conn = Connection(attributes["cm_nova_url"], username="", password="")
        tenant_id, x_auth_token = _get_keystone_tokens(attributes)
        body = '{"unpause": null}'
        headers = {"Content-type": "application/json", "x-auth-token": x_auth_token.encode()}
        uri = tenant_id + "/servers/" + vm['id'] + "/action"
        resp = conn.request_post(uri, body=body, headers=headers)
        status = resp[u'headers']['status']
        if status == '200' or status == '304' or status == '202':
            log.info("VM is unpaused and status is %s" % _get_status(attributes))
        else:
            log.error("_resume: Bad HTTP return code: %s" % status)

    else:
        raise ResourceException("The VM must be paused")

    return _get_status(attributes)
Exemplo n.º 5
0
    def handle(self, request, data):
        print "++++++++++++ ////////////// data = %s" % data
        uri = request.get_full_path()
        match = re.search('/project/ipsec/([^/]+)/preaddlink/', uri)
        vpn_id = match.group(1)
        print "++++++++++++ ////////////// RBA RBA vpn_id = %s" % vpn_id

        self.p_tk = request.user.token.id
        try:
            messages.success(
                request,
                _("Link in process of establishement... Pushing low level network configuration..."
                  ))
            pgsplit = re.split(r'\.', str(data['p_gw']))
            self.p_site = pgsplit[0] + '.' + pgsplit[1]
            egsplit = re.split(r'\.', str(data['e_gw']))
            self.e_site = egsplit[0] + '.' + egsplit[1]

            print "++++++ data to plugin : vpn=%s, psite=%s, pgw=%s, pnets=%s, ptk=%s, esite=%s, egw=%s, enets=%s, etk=%s, bw=%s" % (
                vpn_id, self.p_site, data['p_gw'], data['p_nets'], self.p_tk,
                self.e_site, data['e_gw'], data['e_nets'], self.e_tk,
                data['bw'])

            # should use a modular client below once it supports complex jsons:
            #api.elasticnet.elasticnet_add_link(request, vpn_id, self.p_site, str(data['p_gw']) , str(data['p_nets']), self.p_tk, self.e_site, str(data['e_gw']) , str(data['e_nets']), self.e_tk, str(data['bw']))
            if str(request.user.username).startswith("acme"):
                o = urlparse.urlparse(url_for(request, "ipsecvpn"))
            else:
                o = urlparse.urlparse(url_for(request, "vpn"))

            conn0 = Connection("http://" + str(o.hostname) + ":9797",
                               "ericsson", "ericsson")
            uri0 = "/v1.0/tenants/acme/networks/" + str(vpn_id) + "/links.json"
            LOG.debug("http://" + str(o.hostname) + ":9797")
            LOG.debug(uri0)
            header = {}
            header["Content-Type"] = "application/json"
            jsonbody='{"sites": [{"id":"'+str(self.p_site)+'", "gateway":"'+ str(data['p_gw']) +'", "network":"'+  str(data['p_nets']) +'", "token_id":"'+str(self.p_tk)+ '"}, {"id":"' \
+ str(self.e_site)+'", "gateway":"'+ str(data['e_gw']) +'", "network":"'+  str(data['e_nets']) +'", "token_id":"'+str(self.e_tk)+ '"}], "qos":{"bandwidth":"' \
+ str(data['bw'])+'", "eir":"'+ str(data['eir'])+ '", "cbs":"'+ str(data['cbs'])+ '", "pbs":"'+ str(data['pbs'])+ '"}}'
            print "+++ ewan result json body =%s" % jsonbody
            result = conn0.request_post(uri0, body=jsonbody, headers=header)
            print "+++ ewan result body =%s" % result["body"]
            body = json.loads(result["body"])
            print "+++ewan body=%s" % body
            linkid = str(body['link']['id'])
            print "+++ewan linkid=%s" % linkid

            messages.success(request, _("Link added successfully."))
            shortcuts.redirect("horizon:project:ipsec:index")
            return True
        except Exception as e:
            msg = _(
                'Failed to authorize Link from remote Enterprise Site crendentials : %s'
            ) % e.message
            LOG.info(msg)
            return shortcuts.redirect("horizon:project:ipsec:index")
Exemplo n.º 6
0
    def handle(self, request, data):
        print "++++++++++++ ////////////// data = %s" % data
        uri = request.get_full_path()
        match = re.search('/project/vpns/([^/]+)/autoburst/', uri)
        vpn_id = match.group(1)
        print "++++++++++++ ////////////// RBA RBA vpn_id = %s" % vpn_id

        self.p_tk = request.user.token.id
        try:
            messages.success(
                request,
                _("AutoBurst is enabled on the remote VMs using this elastic wan..."
                  ))
            pgsplit = re.split(r'\.', str(data['p_gw']))
            self.p_site = pgsplit[0] + '.' + pgsplit[1]
            egsplit = re.split(r'\.', str(data['e_gw']))
            self.e_site = egsplit[0] + '.' + egsplit[1]

            # should use a modular client below once it supports complex jsons:
            #api.elasticnet.elasticnet_add_link(request, vpn_id, self.p_site, str(data['p_gw']) , str(data['p_nets']), self.p_tk, self.e_site, str(data['e_gw']) , str(data['e_nets']), self.e_tk, str(data['bw']))
            if str(request.user.username).startswith("acme"):
                o = urlparse.urlparse(url_for(request, "ipsecvpn"))
            else:
                o = urlparse.urlparse(url_for(request, "vpn"))

            conn0 = Connection("http://" + str(o.hostname) + ":9797",
                               "ericsson", "ericsson")
            uri0 = "/v1.0/tenants/acme/networks/" + str(vpn_id) + "/links.json"
            LOG.debug("http://" + str(o.hostname) + ":9797")
            LOG.debug(uri0)
            bw = None
            header = {}
            header["Content-Type"] = "application/json"
            jsonbody='{"sites": [{"id":"'+str(self.p_site)+'", "gateway":"'+ str(data['p_gw']) +'", "network":"'+  str(data['p_nets']) +'", "token_id":"'+str(self.p_tk)+ '"}, {"id":"' \
              + str(self.e_site)+'", "gateway":"'+ str(data['e_gw']) +'", "network":"'+  str(data['e_nets']) +'", "token_id":"'+str(self.e_tk)+ '"}], "qos":{"bandwidth":"' \
              + str(bw)+'"}, "usecase":{"action":"autoburst", "vmuuid":"' \
+ str(data['e_servers'])+'", "vmtenantid":"'+str(self.vmtenantid)+'", "vmsla":"'+str(data['sla'])+'"}}'
            print "+++ ewan result json body =%s" % jsonbody
            result = conn0.request_post(uri0, body=jsonbody, headers=header)
            print "+++ ewan result body =%s" % result["body"]
            body = json.loads(result["body"])
            print "+++ewan body=%s" % body
            linkid = str(body['link']['id'])
            print "+++ewan linkid=%s" % linkid

            messages.success(request, _("Link added successfully."))
            shortcuts.redirect("horizon:project:vpns:index")
            return True
        except Exception as e:
            msg = _(
                'Failed to authorize Link from remote Enterprise Site crendentials : %s'
            ) % e.message
            LOG.info(msg)
            return shortcuts.redirect("horizon:project:vpns:index")
Exemplo n.º 7
0
def _get_keystone_tokens(attributes):
    conn = Connection(attributes["cm_keystone_url"])
    body = '{"auth": {"tenantName":"'+ attributes["cm_tenant_name"] + '", "passwordCredentials":{"username": "******"cm_username"] + '", "password": "******"cm_password"] + '"}}}'
    resp = conn.request_post("/tokens", body=body, headers={'Content-type':'application/json'})
    status = resp[u'headers']['status']
    if status == '200' or status == '304':
        data = json.loads(resp['body'])
        tenant_id = data['access']['token']['tenant']['id']
        x_auth_token = data['access']['token']['id']
        return tenant_id, x_auth_token
    else:
        log.error("_get_keystone_tokens: Bad HTTP return code: %s" % status)
Exemplo n.º 8
0
class DiffsClient(object):

    _logger = logging.getLogger('DiffsClient')
    _logger.addHandler(logging.StreamHandler(sys.stderr))

    def __init__(self, agent_url, verbose=False):
        self._logger.setLevel(logging.DEBUG if verbose else logging.NOTSET)
        if not agent_url.endswith('/'):
            agent_url += '/'
        self.agent_url = agent_url
        base_url = urljoin(agent_url, 'rest')
        self._conn = Connection(base_url)
        self._conn = Connection(self.get_session_url())

    def get_session_url(self):
        url = '/diffs/sessions'
        response = self._post(url)
        return response['headers']['location']

    def get_diffs(self, pair_key, range_start, range_end):
        url = '/?pairKey={0}&range-start={1}&range-end={2}'.format(
                pair_key,
                range_start.strftime(DATETIME_FORMAT),
                range_end.strftime(DATETIME_FORMAT))
        response = self._get(url)
        return json.loads(response['body'])

    def get_diffs_zoomed(self, range_start, range_end, bucketing):
        "A dictionary of pair keys mapped to lists of bucketed diffs"
        url = '/zoom?range-start={0}&range-end={1}&bucketing={2}'.format(
                range_start.strftime(DATETIME_FORMAT),
                range_end.strftime(DATETIME_FORMAT),
                bucketing)
        response = self._get(url)
        return json.loads(response['body'])

    def _get(self, url):
        self._logger.debug("GET %s", self._rebuild_url(url))
        response = self._conn.request_get(url)
        self._logger.debug(response)
        return response
    
    def _post(self, url):
        self._logger.debug("POST %s", self._rebuild_url(url))
        response = self._conn.request_post(url)
        self._logger.debug(response)
        return response

    def _rebuild_url(self, url):
        return self._conn.url.geturl() + url

    def __repr__(self):
        return "DiffsClient(%s)" % repr(self.agent_url)
Exemplo n.º 9
0
def _create_VM(res_id, attributes, dict_vm):
    conn_nova = Connection(attributes["cm_nova_url"], username="", password="")
    tenant_id, x_auth_token = _get_keystone_tokens(attributes)
    body = '{"server": {"name":"'+ dict_vm['name'].encode() + '", "imageRef":"' + dict_vm['image'].encode() + '", "key_name": "' + dict_vm['key'].encode() + '", "user_data":"' + dict_vm['user-data'] + '", "flavorRef":"' + dict_vm['flavor'] + '", "max_count": 1, "min_count": 1, "security_groups": [{"name": "default"}]}}'
    headers = {"Content-type": "application/json", "x-auth-token": x_auth_token.encode()}
    uri = tenant_id + "/servers"
    resp = conn_nova.request_post(uri, body=body, headers=headers)
    status = resp[u'headers']['status']
    if status == '200' or status == '304':
        data = json.loads(resp['body'])
        return _get_status(attributes)
    else:
        log.error("_create_VM: Bad HTTP return code: %s" % status)
Exemplo n.º 10
0
    def handle(self, request, data):
        print "++++++++++++ ////////////// data = %s"%data
        uri = request.get_full_path()
        match = re.search('/project/vpns/([^/]+)/autoburst/', uri)
        vpn_id = match.group(1)
        print "++++++++++++ ////////////// RBA RBA vpn_id = %s"%vpn_id


        self.p_tk=request.user.token.id
        try:
                messages.success(request, _("AutoBurst is enabled on the remote VMs using this elastic wan..."))
                pgsplit=re.split(r'\.',str(data['p_gw']))
                self.p_site=pgsplit[0]+'.'+pgsplit[1]
                egsplit=re.split(r'\.',str(data['e_gw']))
                self.e_site=egsplit[0]+'.'+egsplit[1]

                # should use a modular client below once it supports complex jsons:
                #api.elasticnet.elasticnet_add_link(request, vpn_id, self.p_site, str(data['p_gw']) , str(data['p_nets']), self.p_tk, self.e_site, str(data['e_gw']) , str(data['e_nets']), self.e_tk, str(data['bw']))
                if str(request.user.username).startswith("acme"):
                  o = urlparse.urlparse(url_for(request, "ipsecvpn"))
                else:
                  o = urlparse.urlparse(url_for(request, "vpn"))


                conn0 = Connection("http://"+str(o.hostname)+":9797", "ericsson", "ericsson")
                uri0 = "/v1.0/tenants/acme/networks/"+str(vpn_id)+"/links.json"
                LOG.debug("http://"+str(o.hostname)+":9797")
                LOG.debug(uri0)
		bw=None
                header = {}
                header["Content-Type"]= "application/json"
                jsonbody='{"sites": [{"id":"'+str(self.p_site)+'", "gateway":"'+ str(data['p_gw']) +'", "network":"'+  str(data['p_nets']) +'", "token_id":"'+str(self.p_tk)+ '"}, {"id":"' \
                  + str(self.e_site)+'", "gateway":"'+ str(data['e_gw']) +'", "network":"'+  str(data['e_nets']) +'", "token_id":"'+str(self.e_tk)+ '"}], "qos":{"bandwidth":"' \
                  + str(bw)+'"}, "usecase":{"action":"autoburst", "vmuuid":"' \
		  + str(data['e_servers'])+'", "vmtenantid":"'+str(self.vmtenantid)+'", "vmsla":"'+str(data['sla'])+'"}}'
                print "+++ ewan result json body =%s"%jsonbody
                result=conn0.request_post(uri0, body=jsonbody, headers=header)
                print "+++ ewan result body =%s"%result["body"]
                body=json.loads(result["body"])
                print "+++ewan body=%s"%body
                linkid=str(body['link']['id'])
                print "+++ewan linkid=%s"%linkid

                messages.success(request, _("Link added successfully."))
                shortcuts.redirect("horizon:project:vpns:index")
                return True
        except Exception as e:
            msg = _('Failed to authorize Link from remote Enterprise Site crendentials : %s') % e.message
            LOG.info(msg)
            return shortcuts.redirect("horizon:project:vpns:index")
Exemplo n.º 11
0
    def handle(self, request, data):
        print "++++++++++++ ////////////// data = %s"%data
        uri = request.get_full_path()
        match = re.search('/project/ipsec/([^/]+)/preaddlink/', uri)
        vpn_id = match.group(1)
        print "++++++++++++ ////////////// RBA RBA vpn_id = %s"%vpn_id

	self.p_tk=request.user.token.id
	try:
        	messages.success(request, _("Link in process of establishement... Pushing low level network configuration..."))
		pgsplit=re.split(r'\.',str(data['p_gw']))
		self.p_site=pgsplit[0]+'.'+pgsplit[1]
        	egsplit=re.split(r'\.',str(data['e_gw']))
        	self.e_site=egsplit[0]+'.'+egsplit[1]

	        print "++++++ data to plugin : vpn=%s, psite=%s, pgw=%s, pnets=%s, ptk=%s, esite=%s, egw=%s, enets=%s, etk=%s, bw=%s"%(vpn_id, self.p_site, data['p_gw'] , data['p_nets'], 
		   self.p_tk, self.e_site, data['e_gw'] , data['e_nets'], self.e_tk, data['bw'])
		
		# should use a modular client below once it supports complex jsons: 
		#api.elasticnet.elasticnet_add_link(request, vpn_id, self.p_site, str(data['p_gw']) , str(data['p_nets']), self.p_tk, self.e_site, str(data['e_gw']) , str(data['e_nets']), self.e_tk, str(data['bw']))
		if str(request.user.username).startswith("acme"):
		  o = urlparse.urlparse(url_for(request, "ipsecvpn"))
		else:
		  o = urlparse.urlparse(url_for(request, "vpn"))

		conn0 = Connection("http://"+str(o.hostname)+":9797", "ericsson", "ericsson")
	        uri0 = "/v1.0/tenants/acme/networks/"+str(vpn_id)+"/links.json"
                LOG.debug("http://"+str(o.hostname)+":9797")
                LOG.debug(uri0)
                header = {}
                header["Content-Type"]= "application/json"
                jsonbody='{"sites": [{"id":"'+str(self.p_site)+'", "gateway":"'+ str(data['p_gw']) +'", "network":"'+  str(data['p_nets']) +'", "token_id":"'+str(self.p_tk)+ '"}, {"id":"' \
		  + str(self.e_site)+'", "gateway":"'+ str(data['e_gw']) +'", "network":"'+  str(data['e_nets']) +'", "token_id":"'+str(self.e_tk)+ '"}], "qos":{"bandwidth":"' \
		  + str(data['bw'])+'", "eir":"'+ str(data['eir'])+ '", "cbs":"'+ str(data['cbs'])+ '", "pbs":"'+ str(data['pbs'])+ '"}}'
                print "+++ ewan result json body =%s"%jsonbody
                result=conn0.request_post(uri0, body=jsonbody, headers=header)
                print "+++ ewan result body =%s"%result["body"]
                body=json.loads(result["body"])
                print "+++ewan body=%s"%body
                linkid=str(body['link']['id'])
                print "+++ewan linkid=%s"%linkid

                messages.success(request, _("Link added successfully."))
		shortcuts.redirect("horizon:project:ipsec:index")
		return True
        except Exception as e:
	    msg = _('Failed to authorize Link from remote Enterprise Site crendentials : %s') % e.message
            LOG.info(msg)
	    return shortcuts.redirect("horizon:project:ipsec:index")
Exemplo n.º 12
0
def sendPost(serviceId, instanceId, monitoringEndpoint, kpiName, value):
    timestamp = time.mktime(datetime.now().timetuple()) #UTC-Seconds
    timestamp = long(timestamp) 

    conn = Connection(monitoringEndpoint)
    response = conn.request_post("/data/" + serviceId , args={"serviceId":serviceId, "instanceid":instanceId, "kpiName":kpiName, "value":value, "timestamp":timestamp})
    print "Response: ", response

    status = response.get('headers').get('status')
    if status not in ["200", 200, "204", 204]:
        print >> sys.stderr, "Call failed, status:", status 
        return False

    print "Call successful"
    return True
Exemplo n.º 13
0
class Chemcaster(object):
    def __init__(self, username='******',password='******'):
        self._conn = Connection(CHEMCASTER_ENDPOINT, username, password)
        
    def post(self, body):
        resp = self._conn.request_post("structures",body=body )
        status= resp.get('headers').get('status') 
        if status not in ["200", 200, "204", 204]:
            print resp
            if status in ["422", 422]:
                resp['body']
                foo=xml.dom.minidom.parseString(resp['body'])
                for e in foo.getElementsByTagName('error'):
                    print e.firstChild.wholeText
            raise FailError
        return resp
Exemplo n.º 14
0
class Server:

    def __init__(self, root_url="http://led-o-matic.appspot.com"):
    	self.root_url = root_url
    	self.conn = Connection(self.root_url)
    	self.name = ""

    def getPinStatus(self, pins_name, pin_id):
        request = self.name + '/' + pins_name + '/' + pin_id
        response = self.conn.request_get(request)
        return response['body']
  

    def login(self, name):
		self.name = name
		response = self.conn.request_post('/' + name)
		return self.root_url + '/' + self.name + response['body']
Exemplo n.º 15
0
def sendPost(serviceId, instanceId, monitoringEndpoint, kpiName, value):
    timestamp = time.mktime(datetime.now().timetuple())  #UTC-Seconds
    timestamp = long(timestamp)

    conn = Connection(monitoringEndpoint)
    response = conn.request_post("/data/" + serviceId,
                                 args={
                                     "serviceId": serviceId,
                                     "instanceid": instanceId,
                                     "kpiName": kpiName,
                                     "value": value,
                                     "timestamp": timestamp
                                 })
    print "Response: ", response

    status = response.get('headers').get('status')
    if status not in ["200", 200, "204", 204]:
        print >> sys.stderr, "Call failed, status:", status
        return False

    print "Call successful"
    return True
Exemplo n.º 16
0
class Odl:
    """Connect to ODL and do REST transactions."""
    def __init__(self, base_url):
        """Connect to ODL."""
        self.conn = Connection(base_url, username=v.username,
                      password=v.password)
        self.log = logging.getLogger('ODL')

    def clean_up(self):
        """Clean up ODL configuration."""
        params = {'node_id': v.rt1_node_id,
                  'name': v.auto_tunnel_name}
        self.post_remove_lsp(params)

    def read_file(self, filename, params=None):
        """Return the file contents after substituting the params if any."""
        f = open(filename, 'r')
        contents = f.read()
        f.close()
        if params:
            contents = Template(contents)
            contents = contents.substitute(params)

        return contents

    def get(self, path):
        """Request REST GET transaction with timeout."""
        return timeout(self, self.request, ('GET', path, {}))

    def post(self, path, body):
        """Request REST POST transaction with timeout."""
        return timeout(self, self.request, ('POST', path, body))

    def put(self, path, body):
        """Request REST PUT transaction with timeout."""
        return timeout(self, self.request, ('PUT', path, body))

    def request(self, operation, path, body):
        """Request REST transactions."""
        self.log.info("%s : %s", operation, path)

        if body != {}:
            self.log.debug("Body : %s", json.dumps(json.loads(body), indent=2))

        headers = {'content-type':'application/json', 'accept':'application/json'}

        if operation == 'GET':
            response = self.conn.request_get(path, args={}, headers=headers)
        elif operation == 'POST':
            response = self.conn.request_post(path, args={}, body=body, headers=headers)
        elif operation == 'PUT':
            response = self.conn.request_put(path, args={}, body=body, headers=headers)

        status = int(response['headers']['status'])

        self.log.info("Response : %s %s", status, httplib.responses[status])

        response_body = json.loads(response['body'])
        self.log.debug("Response Body : %s\n", json.dumps(response_body, indent=2))

        return status, response_body

    def get_pcep_topology(self, params=None):
        """Return the content of PCEP topology response."""
        return self.get("/operational/network-topology:network-topology/topology/pcep-topology")

    def post_add_lsp(self, params=None):
        """Add LSP and return the content of the response."""
        self.node_id = params['node_id']  # Required for auto undo
        if params.has_key('sid'):
            body = self.read_file(v.add_lsp_sr_file, params)
        elif params.has_key('sid2'):
            body = self.read_file(v.add_lsp_sr2_file, params)
        elif params.has_key('sid3'):
            body = self.read_file(v.add_lsp_sr3_file, params)
        else:
            body = self.read_file(v.add_lsp_file, params)
        return self.post("/operations/network-topology-pcep:add-lsp", body)

    def post_update_lsp(self, params=None):
        """Update LSP and return the content of the response."""
        if params.has_key('sid'):
            body = self.read_file(v.update_lsp_sr_file, params)
        elif params.has_key('sid2'):
            body = self.read_file(v.update_lsp_sr2_file, params)
        elif params.has_key('sid3'):
            body = self.read_file(v.update_lsp_sr3_file, params)
        else:
            body = self.read_file(v.update_lsp_file, params)
        return self.post("/operations/network-topology-pcep:update-lsp", body)

    def post_remove_lsp(self, params=None):
        """Remove LSP and return the content of the response."""
        if params is None:
            # Auto undo
            params = {}
            move_attr_to_params(self, params, 'node_id')

        if params.has_key("node_id"):
            body = self.read_file(v.remove_lsp_file, params)
            return self.post("/operations/network-topology-pcep:remove-lsp", body)

    def post_add_label(self, params=None):
        """Add label and return the content of the response."""
        if params.has_key('in_label') and params.has_key('out_label'):
            body = self.read_file(v.add_label_dwnld_in_out_file, params)
        elif params.has_key('in_label'):
            body = self.read_file(v.add_label_dwnld_in_file, params)
        elif params.has_key('out_label'):
            body = self.read_file(v.add_label_dwnld_out_file, params)
        elif params.has_key('node_label'):
            body = self.read_file(v.add_label_map_node_file, params)
        elif params.has_key('adj_label'):
            body = self.read_file(v.add_label_map_adj_file, params)
        else:
            body = self.read_file(v.add_label_db_sync_end_file, params)

        return self.post("/operations/network-topology-pcep:add-label", body)

    def post_remove_label(self, params=None):
        """Remove label and return the content of the response."""
        body = self.read_file(v.remove_label_file, params)
        return self.post("/operations/network-topology-pcep:remove-label", body)
Exemplo n.º 17
0
class RedmineRESTAPIWrapper(object):

    def __init__(self, settings):
        self.api_key = settings.redmine_api_key
        self.conn = Connection(settings.redmine_url)
    
    def request_put(self, path, payload):
        return self.conn.request_put(path, args= [ ('key', self.api_key) ], body=json.dumps(payload), headers={'content-type':'application/json', 'accept':'application/json'})

    def request_post(self, path, payload):
        return self.conn.request_post(path, args= [ ('key', self.api_key) ], body=json.dumps(payload), headers={'content-type':'application/json', 'accept':'application/json'})

    def request_get(self, path, payload):
        return self.conn.request_get(path, args= [ ('key', self.api_key) ] + payload, headers={'content-type':'application/json', 'accept':'application/json'})

    def put_items_with_payload(self, url, payload_name, items, payload):
        if not isinstance(items, set):
            items = set([items])
        for i in items:
            resp = self.request_put("/{}/".format(url)+str(i)+".json", { payload_name: payload})
            status = resp[u'headers']['status']
            print 'Item {} '.format(url), i, ', http status code: ', status
            if int(status) != 200:
                print resp

    def put_issues_with_payload(self, issues, payload):
        return self.put_items_with_payload("issues", "issue", issues, payload)

    def put_versions_with_payload(self, versions, payload):
        return self.put_items_with_payload("versions", "version", versions, payload)

    def post_time_entries_with_payload(self, payload):
        resp = self.request_post("/time_entries.json", {'time_entry': payload})
        status = resp[u'headers']['status']
        print 'Issue ', payload['issue_id'], ', http status code: ', status

    def get_items_as_json(self, endpoint, payload):
	resp = self.request_get("/"+endpoint+".json", payload)
        status = resp[u'headers']['status']
        return resp[u'body']

    def get_time_entries(self, payload):
        return self.get_items_as_json('time_entries', payload)

    def get_projects(self, payload):
        return self.get_items_as_json('projects', payload)

    def set_projects_parent(self, projects, parent_id):
        return self.put_items_with_payload("projects", "project", projects, { 'parent_id': parent_id})

    def add_issues_to_milestone(self, issues, version_id, milestone_name):
        self.put_issues_with_payload(issues, {'notes': 'Issue added to milestone: '+milestone_name, 'fixed_version_id': version_id})

    def add_issues_on_sprint(self, issues, sprint_id, sprint_name):
        self.put_issues_with_payload(issues, {'notes': 'Issue added to sprint "'+sprint_name+'" from REST API', 'easy_sprint_id': sprint_id})
                                         
    def set_issues_status(self, issues, status_id):
        self.put_issues_with_payload(issues, {'status_id': status_id})

    def set_issues_status_and_assigned(self, issues, status_id, assigned_id):
        self.put_issues_with_payload(issues, {'status_id': status_id, 'assigned_to_id': assigned_id})

    def set_issues_assigned(self, issues, assigned_id):
        self.put_issues_with_payload(issues, {'assigned_to_id': assigned_id})

    def set_parent_issue(self, issues, parent_id):
        self.put_issues_with_payload(issues,{'parent_issue_id': parent_id})

    def add_notes_on_issues(self, issues, notes):
        self.put_issues_with_payload(issues,{'notes': notes})

    def add_update_on_commit(self, issue, repo_name, branch_name, commit_hash, commit_msg):
        notes = "Repo <b>%s</b> branch <b>%s</b> commit <b>%s</b>: %s" % (repo_name, branch_name, commit_hash, commit_msg)
        return self.add_notes_on_issues(set([issue]), notes)

    def add_update_on_commit_from_line(self, line, repo_name, branch_name):
        (commit_hash, commit_msg) =line.split(' ',1)
        match = re.search("\#(\d+)", commit_msg)
        if match:
            issue = match.group(1)
            resp =self.add_update_on_commit(issue,repo_name, branch_name, commit_hash, commit_msg)

    def add_issues_to_milestone_1(self, issues):
        self.add_issues_to_milestone(issues, 61, "Milestone 1")

    def close_issues(self, issues):
        self.set_issues_status(issues, settings.statuses['closed'])

    def get_items_as_json_full(self, endpoint, params = None, process_cb = None):
        (offset, limit, read, total) = (0, 25, 0, 65535)
        if params is None: params = []
        result = []
        while read<total:
            _params = params + [('limit', limit), ('offset', offset)]
            resp = json.loads(self.get_items_as_json(endpoint, _params))
#            add_to_list(resp["time_entries"], label)
            result += resp[endpoint]
            if process_cb is not None:
                process_cb(resp[endpoint])

            total = resp["total_count"]
            read += limit if (limit+offset < total) else total - offset
            offset += limit
        return result
Exemplo n.º 18
0
from restful_lib import Connection
from bitcoinrpc.authproxy import AuthServiceProxy

# get bitcoin rpc config
bitcoinrpc = coinrpc("../../bitcoin-0.10.2/bitcoin/bitcoin.conf").rpccon()

# get litecoin rpc config
litecoinrpc = coinrpc("../../litcoin-xxx/bitcoin/bitcoin.conf").rpccon()

# make a new bitcoin address to receive the shifted funds
newaddress = bitcoinrpc.getnewaddress()

# connect to shapeshift API
base_url = "https://shapeshift.io"
conn = Connection(base_url)

# change litecoin to bitcoin
post_data = {"withdrawal":newaddress, "pair":"ltc_btc"}
btc_shift = conn.request_post("/shift/", post_data)

for item in btc_shift:
    if item == "body":
        response = json.loads(btc_shift[item])

for key, value in response.iteritems():
    if key == "deposit":
        depositaddr = value

# send to deposit address
litecoinrpc.sendtoaddress(depositaddr, 10)
Exemplo n.º 19
0
from restful_lib import Connection
from bitcoinrpc.authproxy import AuthServiceProxy

# get bitcoin rpc config
bitcoinrpc = coinrpc("../../bitcoin-0.10.2/bitcoin/bitcoin.conf").rpccon()

# get litecoin rpc config
litecoinrpc = coinrpc("../../litcoin-xxx/bitcoin/bitcoin.conf").rpccon()

# make a new bitcoin address to receive the shifted funds
newaddress = bitcoinrpc.getnewaddress()

# connect to shapeshift API
base_url = "https://shapeshift.io"
conn = Connection(base_url)

# change litecoin to bitcoin
post_data = {"withdrawal": newaddress, "pair": "ltc_btc"}
btc_shift = conn.request_post("/shift/", post_data)

for item in btc_shift:
    if item == "body":
        response = json.loads(btc_shift[item])

for key, value in response.iteritems():
    if key == "deposit":
        depositaddr = value

# send to deposit address
litecoinrpc.sendtoaddress(depositaddr, 10)
Exemplo n.º 20
0
class Account(object):
    """A vcast account.
    
    This class handles communication with Vcast FAUCET PVR Server
    """

    def __init__(self, username, password):
        """Set up a REST connection to Vcast Server
        
        Returns id_usr user id or raises exception"""
        self.username = username
        self.password = password

        url = 'http://www.vcast.it/faucetpvr/api/1.0/server_rest.php'
        self.connection = Connection(url)
        self.connection.add_rest_credentials(username, password)
        
        c = self.connection.request_get('/faucetid')
        if c['body'] == 'Access Denied':
            raise Exception('Wrong credentials')
        self.id_usr = simplejson.loads(c['body'])['id_usr']

    def get_channels(self):
        """Return channels.
        
        The function returns channel as a list of dictionaries.
        Each element of the list is a dictionary with two keys,
        - type, whose value is a string that can be "video" or "audio";
        - name, whose value is a string.

        """
        try:
            reply = self.connection.request_get('/channels')
            return simplejson.loads(reply['body'])
        except:
            print reply

    def get_recordings(self):
        """Return recordings.

        The function returns recordings as a list of dictionaries.
        Each element has the following keys whose value is a unicode
        string (type: unicode).
        - id_rec
        - title
        - channel
        - channel_type (can be 'audio' or 'video')
        - format
        - from_time
        - rec_time
        - to_time
        - retention
        - repeat
        - faucetacc (ignore it)

        """
        try:
            reply =  self.connection.request_get('/recordings')
            return simplejson.loads(reply['body'])
        except:
            print reply

    def get_download_urls(self):
        """Return the urls of avaible recordings"""
        feed = self.connection.request_get('/feed')['body']
        feed = re.sub(r'\\(.)', r'\1', feed)[13:-3]
        f = feedparser.parse(feed)
        urls = []
        for i in f.entries:
            # print i['enclosures'][0]['href']
            urls.append(i['enclosures'][0]['href'])
        return urls

    def new_recording(self, recording):
        """Invia al server una nuova programmazione"""
        json = recording.toJson()
        print json
        a = self.connection.request_post('/recordings', body=json)
        print a

    def delete_recording(self, id):
        repl = self.connection.request_get('/delete_recording',
                args={'id_rec':str(id)})
        print repl
Exemplo n.º 21
0
class Store():
    def __init__(self, base_store_url, username=None, password=None):
        """ Base URL for the store should be pretty self-explanatory. E.g. something like
            "http://api.talis.com/stores/store_name"
            Only needs to enter the username/password if this class is going to tinker
            with things."""
        if base_store_url.endswith('/'):
            base_store_url = base_store_url[:-1]

        self.base_store_url = base_store_url
        # Split the given URL
        if base_store_url:
            self.conn = Connection(base_store_url, username=username, password=password)

    def does_snapshot_exist(self, snapshot_filename):
        # Test to see if snapshot exists:
        snapshot_path = SNAPSHOT_TEMPLATE % snapshot_filename
        
        response = self.conn.request(snapshot_path, method = "HEAD")
        
        if response.get('headers') and response.get('headers').get('status'):
            status = response.get('headers').get('status')
        
            if status in ['200', '204']:
                return True
            elif status.startswith('4'):
                return False
            # else: raise Error?

        return False

    def schedule_reset_data(self, label, at_time=None):
        """Will request that the store is emptied, and label the request. 
           If a time is given as an ISO8601 formatted string, this will be 
           the scheduled time for the snapshot. Otherwise, it will use the current time."""
        if not at_time:
            at_time=datetime.utcnow().isoformat().split('.')[0]
        
        snapshot_request = RESET_STORE_TEMPLATE % (label, at_time)
        
        return self.conn.request_post(JOB_REQUESTS, body = snapshot_request, headers={'Content-Type':'application/rdf+xml'})

    def schedule_snapshot_data(self, label, at_time=None):
        """Will request a snapshot be made of the store. 
           If a time is given as an ISO8601 formatted string, this will be 
           the scheduled time for the snapshot. Otherwise, it will use the current time."""
        if not at_time:
            at_time=datetime.utcnow().isoformat().split('.')[0]
        
        snapshot_request = SNAPSHOT_STORE_TEMPLATE % (label, at_time)
        
        return self.conn.request_post(JOB_REQUESTS, body = snapshot_request, headers={'Content-Type':'application/rdf+xml'})
        
    def schedule_snapshot_restore(self, label, snapshot_filename, at_time=None):
        """Will request that the store is restored from a snapshot. If a time is given as
           an ISO8601 formatted string, this will be the scheduled time for
           the recovery. Otherwise, it will use the current time."""
        if not at_time:
            at_time=datetime.utcnow().isoformat().split('.')[0]
        
        # Test to see if snapshot exists:
        snapshot_path = SNAPSHOT_TEMPLATE % snapshot_filename
        
        if self.does_snapshot_exist(snapshot_filename):
            snapshot_uri = "%s%s" % (self.base_store_url, snapshot_path)
            snapshot_request = SNAPSHOT_RESTORE_TEMPLATE % (label, snapshot_uri, at_time)    
            return self.conn.request_post(JOB_REQUESTS, body = snapshot_request, headers={'Content-Type':'application/rdf+xml'})
            
    def submit_rdfxml(self, rdf_text):
        """Puts the given RDF/XML into the Talis Store"""
        return self._put_rdf(rdf_text, mimetype="application/rdf+xml")
        
    def _put_rdf(self, rdf_text, mimetype="application/rdf+xml"):
        """Placeholder for allowing other serialisation types to be put into a
           Talis store, whether the conversion takes place here, or if the Talis
           store starts to accept other formats."""
        if rdf_text:
            request_headers = {}
            if mimetype not in ['application/rdf+xml']:
                raise RDFFormatException("%s is not an allowed RDF serialisation format" % mimetype)
            request_headers['Content-Type'] = mimetype
            return self.conn.request_post(META_ENDPOINT, body=rdf_text, headers=request_headers)        
                 
    def _query_sparql_service(self, query, args={}):
        """Low-level SPARQL query - returns the message and response headers from the server.
           You may be looking for Store.sparql instead of this."""
        passed_args = {'query':query}
        passed_args.update(args)
        return self.conn.request_get(SPARQL_ENDPOINT, args=passed_args, headers={'Content-type':'application/x-www-form-urlencoded'})
        
    def _query_search_service(self, query, args={}):
        """Low-level content box query - returns the message and response headers from the server.
           You may be looking for Store.search instead of this."""
           
        passed_args = {'query':query}
        passed_args.update(args)
        
        return self.conn.request_get(CONTENT_ENDPOINT, args=passed_args, headers={'Content-type':'application/x-www-form-urlencoded'} )
        
    def _list_snapshots(self, passed_args={}):
        return self.conn.request_get(SNAPSHOTS, args=passed_args, headers={}) 
        
##############################################################################
# Convenience Functions
##############################################################################

    def submit_rdfxml_from_url(self, url_to_file, headers={"Accept":"application/rdf+xml"}):
        """Convenience method - downloads the file from a given url, and then pushes that
           into the meta store. Currently, it doesn't put it through a parse-> reserialise
           step, so that it could handle more than rdf/xml on the way it but it is a
           future possibility."""
        import_rdf_connection = Connection(url_to_file)
        response = import_rdf_connection.request_get("", headers=headers)
        
        if response.get('headers') and response.get('headers').get('status') in ['200', '204']:
            request_headers = {}
            
            # Lowercase all response header fields, to make matching easier. 
            # According to HTTP spec, they should be case-insensitive
            response_headers = response['headers']
            for header in response_headers:
                response_headers[header.lower()] = response_headers[header]
                
            # Set the body content
            body = response.get('body').encode('UTF-8')
            
            # Get the response mimetype
            rdf_type = response_headers.get('content-type', None)
            
            return self._put_rdf(body, mimetype=rdf_type)
            
    def sparql(self, query, args={}):
        """Performs a SPARQL query and simply returns the body of the response if successful
           - if there is an issue, such as a code 404 or 500, this method will return False. 
           
           Use the _query_sparql_service method to get hold of
           the complete response in this case."""
        response = self._query_sparql_service(query, args)
        headers = response.get('headers')
        
        status = headers.get('status', headers.get('Status'))
        
        if status in ['200', 200, '204', 204]:
            return response.get('body').encode('UTF-8')
        else:
            return False

    def search(self, query, args={}):
        """Performs a search query and simply returns the body of the response if successful
           - if there is an issue, such as a code 404 or 500, this method will return False. 
           
           Use the _query_search_service method to get hold of
           the complete response in this case."""
        response = self._query_search_service(query, args)
        headers = response.get('headers')
        
        status = headers.get('status', headers.get('Status'))
        
        if status in ['200', 200, '204', 204]:
            parsed_atom = Atom_Search_Results(response.get('body').encode('UTF-8'))
            return parsed_atom.get_item_list()
        else:
            return False
Exemplo n.º 22
0
 def rollback(self):
     ua = Connection(self.rollback_uri, username=self.un, password=self.pw)
     resp = ua.request_post("/")
     return Dezi.Response(resp)
Exemplo n.º 23
0
 def commit(self):
     ua = Connection(self.commit_uri, username=self.un, password=self.pw)
     resp = ua.request_post("/")
     return Dezi.Response(resp)
Exemplo n.º 24
0
class RestClientEngine(object):

    __metaclass__ = Singleton

    def __init__(self, conn=None):
        if not conn:
            self.conn = Connection(GENERAL_PARAMETERS['base_url'],
                                   username=GENERAL_PARAMETERS['username'],
                                   password=GENERAL_PARAMETERS['password'])
        else:
            self.conn = conn

    def executeRequest(self,
                       identifier,
                       body=None,
                       query_parameter=None,
                       **kwargs):
        """
        Execute a Http request using pre configured configurations.
        :param identifier Identifier of the configuration block of the request.
        :param body Body content of the request. Default None.
        :param query_parameter Query parameters of URL. apis/apiServ?param1=value1&...  Default None.
        :param kwargs You can include in it pathVariables and extra headers.
        :return Dictionary with the body response and headers that contains status code too.
        :raises NotFoundException if a parameter is not present in config or in method call.
        """
        rel_url = self.buildUrl(identifier, kwargs)
        headers = self.buildHeaders(identifier, kwargs)

        if identifier in REQUESTS:
            if REQUESTS[identifier]['method'] in ('get', 'post', 'put',
                                                  'delete'):
                if REQUESTS[identifier]['method'] == 'get':
                    return self.conn.request_get(rel_url,
                                                 headers=headers,
                                                 args=query_parameter)
                elif REQUESTS[identifier]['method'] == 'post':
                    return self.conn.request_post(rel_url,
                                                  headers=headers,
                                                  body=body)
                elif REQUESTS[identifier]['method'] == 'put':
                    return self.conn.request_put(rel_url,
                                                 headers=headers,
                                                 body=body)
                elif REQUESTS[identifier]['method'] == 'delete':
                    return self.conn.request_delete(rel_url, headers=headers)
                else:
                    raise NotFoundException('method not found')
            else:
                raise NotFoundException('method not found')

    def buildHeaders(self, identifier, kwargs):
        result = {}
        if 'headers' in GENERAL_PARAMETERS:
            for key, value in GENERAL_PARAMETERS['headers'].items():
                result[key] = value

        if identifier in REQUESTS:
            if 'headers' in REQUESTS[identifier]:
                for key, value in REQUESTS[identifier]['headers'].items():
                    result[key] = value
        else:
            raise NotFoundException('Request identifier not found exception.')

        if 'headers' in kwargs:
            for key, value in kwargs['headers'].items():
                result[key] = value

        if GENERAL_PARAMETERS['username'] and GENERAL_PARAMETERS['password']:
            result['Authorization'] = ''.join([
                'Basic ',
                base64.b64encode(':'.join([
                    GENERAL_PARAMETERS['username'],
                    GENERAL_PARAMETERS['password']
                ]))
            ])

        return result

    def buildUrl(self, identifier, kwargs):
        if identifier in REQUESTS:
            relative_url = REQUESTS[identifier]['relative_url']
        else:
            raise NotFoundException('Request identifier not found exception.')

        parameters = self.getParameterFromConfigFile(relative_url)

        replaced_relative_url = self.replaceParameters(relative_url,
                                                       parameters, kwargs)

        return replaced_relative_url

    def getParameterFromConfigFile(self, relative_url):
        return re.findall('{(?P<parameter>[a-zA-Z%0-9_-]+)}', relative_url)

    def replaceParameters(self, relative_url, parameters, kwargs):
        result = relative_url
        for parameter in parameters:
            if parameter in kwargs:
                result = result.replace(''.join(['{', parameter, '}']),
                                        kwargs[parameter], 1)
            else:
                raise NotFoundException(''.join(
                    ['Parameter ', parameter, ' not found for build the url']))
        return result
Exemplo n.º 25
0
class Client(object):

    version = "0.002001"

    def __init__(self, server, search="/search", index="/index", debug=False, username=False, password=False):

        self.server = server
        self.search = search
        self.index = index
        self.debug = debug
        self.un = username
        self.pw = password

        # docs:
        # http://code.google.com/p/python-rest-client/wiki/Using_Connection
        self.ua = Connection(server)

        # interrogate server
        resp = self.ua.request_get("/")
        # pprint.pprint(resp)
        paths = json.loads(resp["body"])
        self.searcher = Connection(paths["search"])
        self.indexer = Connection(paths["index"], username=username, password=password)
        self.commit_uri = paths["commit"]
        self.rollback_uri = paths["rollback"]
        self.fields = paths["fields"]
        self.facets = paths["facets"]

    def __str__(self):
        return unicode(self).encode("utf-8")

    def __unicode__(self):
        return pprint.pformat(vars(self))

    def _put_doc(self, doc, uri=None, content_type=None):
        # print "adding to index: %s" % doc

        body_buf = ""

        if isinstance(doc, Doc):
            # print "doc isa Dezi.Doc\n"
            body_buf = doc.as_string()
            if uri == None:
                uri = doc.uri
            if content_type == None:
                content_type = doc.mime_type

        elif os.path.isfile(doc):
            f = open(doc, "r")
            body_buf = f.read()
            if uri == None:
                uri = doc

        else:
            # print "doc isa string\n"
            body_buf = doc
            if uri == None:
                raise Exception("uri required")

        server_uri = "/" + uri
        if self.debug:
            print("uri=" + server_uri)
            print("body=%s" % body_buf)

        resp = self.indexer.request_post(server_uri, body=body_buf, headers={"Content-Type": content_type})

        # pprint.pprint(resp)
        return Dezi.Response(resp)

    def add(self, doc, uri=None, content_type=None):
        return self._put_doc(doc, uri, content_type)

    def update(self, doc, uri=None, content_type=None):
        return self._put_doc(doc, uri, content_type)

    def delete(self, uri):
        resp = self.indexer.request_delete(uri)
        return Dezi.Response(resp)

    def commit(self):
        ua = Connection(self.commit_uri, username=self.un, password=self.pw)
        resp = ua.request_post("/")
        return Dezi.Response(resp)

    def rollback(self):
        ua = Connection(self.rollback_uri, username=self.un, password=self.pw)
        resp = ua.request_post("/")
        return Dezi.Response(resp)

    def get(self, **my_args):
        if "q" not in my_args:
            raise Exception("'q' param required")

        resp = self.searcher.request_get("/", args=my_args)
        # pprint.pprint(resp)
        r = Dezi.Response(resp)
        if r.is_success == False:
            self.last_response = r
            return False
        else:
            return r
Exemplo n.º 26
0
class AdflyApi():
    BASE_HOST = 'https://api.adf.ly'
    # TODO: Replace this with your secret key.
    SECRET_KEY = setting.SECRET_KEY
    # TODO: Replace this with your public key.
    PUBLIC_KEY = setting.PUBLIC_KEY
    # TODO: Replace this with your user id.
    USER_ID = setting.USER_ID
    AUTH_TYPE = dict(basic=1, hmac=2)

    def __init__(self):
        # In this example we use rest client provided by
        # http://code.google.com/p/python-rest-client/
        # Of course you are free to use any other client.
        self._connection = Connection(self.BASE_HOST)

    def shorten(self, urls, domain=None, advert_type=None, group_id=None):
        params = dict()
        if domain:
            params['domain'] = domain
        if advert_type:
            params['advert_type'] = advert_type
        if group_id:
            params['group_id'] = group_id

        if type(urls) == list:
            for i, url in enumerate(urls):
                params['url[%d]' % i] = url
        elif type(urls) == str:
            params['url'] = urls

        response = self._connection.request_post('/v1/shorten',
                                                 args=self._get_params(
                                                     params,
                                                     self.AUTH_TYPE['basic']))
        return json.loads(response['body'])

    def _get_params(self, params={}, auth_type=None):
        """Populates request parameters with required parameters,
        such as _user_id, _api_key, etc.
        """
        auth_type = auth_type or self.AUTH_TYPE['basic']

        params['_user_id'] = self.USER_ID
        params['_api_key'] = self.PUBLIC_KEY

        if self.AUTH_TYPE['basic'] == auth_type:
            pass
        elif self.AUTH_TYPE['hmac'] == auth_type:
            # Get current unix timestamp (UTC time).
            params['_timestamp'] = int(time.time())
            params['_hash'] = self._do_hmac(params)
        else:
            raise RuntimeError

        return params

    def _do_hmac(self, params):
        if type(params) != dict:
            raise RuntimeError

        # Get parameter names.
        keys = params.keys()
        # Sort them using byte ordering.
        # So 'param[10]' comes before 'param[2]'.
        keys.sort()
        queryParts = []

        # Url encode query string. The encoding should be performed
        # per RFC 1738 (http://www.faqs.org/rfcs/rfc1738)
        # which implies that spaces are encoded as plus (+) signs.
        for key in keys:
            quoted_key = urllib.quote_plus(str(key))
            if params[key] is None:
                params[key] = ''

            quoted_value = urllib.quote_plus(str(params[key]))
            queryParts.append('%s=%s' % (quoted_key, quoted_value))

        return hmac.new(self.SECRET_KEY, '&'.join(queryParts),
                        hashlib.sha256).hexdigest()
Exemplo n.º 27
0
class Store():
    def __init__(self, base_store_url, username=None, password=None):
        """ Base URL for the store should be pretty self-explanatory. E.g. something like
            "http://api.talis.com/stores/store_name"
            Only needs to enter the username/password if this class is going to tinker
            with things."""
        if base_store_url.endswith('/'):
            base_store_url = base_store_url[:-1]

        self.base_store_url = base_store_url
        # Split the given URL
        if base_store_url:
            self.conn = Connection(base_store_url,
                                   username=username,
                                   password=password)

    def does_snapshot_exist(self, snapshot_filename):
        # Test to see if snapshot exists:
        snapshot_path = SNAPSHOT_TEMPLATE % snapshot_filename

        response = self.conn.request(snapshot_path, method="HEAD")

        if response.get('headers') and response.get('headers').get('status'):
            status = response.get('headers').get('status')

            if status in ['200', '204']:
                return True
            elif status.startswith('4'):
                return False
            # else: raise Error?

        return False

    def schedule_reset_data(self, label, at_time=None):
        """Will request that the store is emptied, and label the request. 
           If a time is given as an ISO8601 formatted string, this will be 
           the scheduled time for the snapshot. Otherwise, it will use the current time."""
        if not at_time:
            at_time = datetime.utcnow().isoformat().split('.')[0]

        snapshot_request = RESET_STORE_TEMPLATE % (label, at_time)

        return self.conn.request_post(
            JOB_REQUESTS,
            body=snapshot_request,
            headers={'Content-Type': 'application/rdf+xml'})

    def schedule_snapshot_data(self, label, at_time=None):
        """Will request a snapshot be made of the store. 
           If a time is given as an ISO8601 formatted string, this will be 
           the scheduled time for the snapshot. Otherwise, it will use the current time."""
        if not at_time:
            at_time = datetime.utcnow().isoformat().split('.')[0]

        snapshot_request = SNAPSHOT_STORE_TEMPLATE % (label, at_time)

        return self.conn.request_post(
            JOB_REQUESTS,
            body=snapshot_request,
            headers={'Content-Type': 'application/rdf+xml'})

    def schedule_snapshot_restore(self,
                                  label,
                                  snapshot_filename,
                                  at_time=None):
        """Will request that the store is restored from a snapshot. If a time is given as
           an ISO8601 formatted string, this will be the scheduled time for
           the recovery. Otherwise, it will use the current time."""
        if not at_time:
            at_time = datetime.utcnow().isoformat().split('.')[0]

        # Test to see if snapshot exists:
        snapshot_path = SNAPSHOT_TEMPLATE % snapshot_filename

        if self.does_snapshot_exist(snapshot_filename):
            snapshot_uri = "%s%s" % (self.base_store_url, snapshot_path)
            snapshot_request = SNAPSHOT_RESTORE_TEMPLATE % (
                label, snapshot_uri, at_time)
            return self.conn.request_post(
                JOB_REQUESTS,
                body=snapshot_request,
                headers={'Content-Type': 'application/rdf+xml'})

    def submit_rdfxml(self, rdf_text):
        """Puts the given RDF/XML into the Talis Store"""
        return self._put_rdf(rdf_text, mimetype="application/rdf+xml")

    def _put_rdf(self, rdf_text, mimetype="application/rdf+xml"):
        """Placeholder for allowing other serialisation types to be put into a
           Talis store, whether the conversion takes place here, or if the Talis
           store starts to accept other formats."""
        if rdf_text:
            request_headers = {}
            if mimetype not in ['application/rdf+xml']:
                raise RDFFormatException(
                    "%s is not an allowed RDF serialisation format" % mimetype)
            request_headers['Content-Type'] = mimetype
            return self.conn.request_post(META_ENDPOINT,
                                          body=rdf_text,
                                          headers=request_headers)

    def _query_sparql_service(self, query, args={}):
        """Low-level SPARQL query - returns the message and response headers from the server.
           You may be looking for Store.sparql instead of this."""
        passed_args = {'query': query}
        passed_args.update(args)
        return self.conn.request_get(
            SPARQL_ENDPOINT,
            args=passed_args,
            headers={'Content-type': 'application/x-www-form-urlencoded'})

    def _query_search_service(self, query, args={}):
        """Low-level content box query - returns the message and response headers from the server.
           You may be looking for Store.search instead of this."""

        passed_args = {'query': query}
        passed_args.update(args)

        return self.conn.request_get(
            CONTENT_ENDPOINT,
            args=passed_args,
            headers={'Content-type': 'application/x-www-form-urlencoded'})

    def _list_snapshots(self, passed_args={}):
        return self.conn.request_get(SNAPSHOTS, args=passed_args, headers={})


##############################################################################
# Convenience Functions
##############################################################################

    def submit_rdfxml_from_url(self,
                               url_to_file,
                               headers={"Accept": "application/rdf+xml"}):
        """Convenience method - downloads the file from a given url, and then pushes that
           into the meta store. Currently, it doesn't put it through a parse-> reserialise
           step, so that it could handle more than rdf/xml on the way it but it is a
           future possibility."""
        import_rdf_connection = Connection(url_to_file)
        response = import_rdf_connection.request_get("", headers=headers)

        if response.get('headers') and response.get('headers').get(
                'status') in ['200', '204']:
            request_headers = {}

            # Lowercase all response header fields, to make matching easier.
            # According to HTTP spec, they should be case-insensitive
            response_headers = response['headers']
            for header in response_headers:
                response_headers[header.lower()] = response_headers[header]

            # Set the body content
            body = response.get('body').encode('UTF-8')

            # Get the response mimetype
            rdf_type = response_headers.get('content-type', None)

            return self._put_rdf(body, mimetype=rdf_type)

    def sparql(self, query, args={}):
        """Performs a SPARQL query and simply returns the body of the response if successful
           - if there is an issue, such as a code 404 or 500, this method will return False. 
           
           Use the _query_sparql_service method to get hold of
           the complete response in this case."""
        response = self._query_sparql_service(query, args)
        headers = response.get('headers')

        status = headers.get('status', headers.get('Status'))

        if status in ['200', 200, '204', 204]:
            return response.get('body').encode('UTF-8')
        else:
            return False

    def search(self, query, args={}):
        """Performs a search query and simply returns the body of the response if successful
           - if there is an issue, such as a code 404 or 500, this method will return False. 
           
           Use the _query_search_service method to get hold of
           the complete response in this case."""
        response = self._query_search_service(query, args)
        headers = response.get('headers')

        status = headers.get('status', headers.get('Status'))

        if status in ['200', 200, '204', 204]:
            parsed_atom = Atom_Search_Results(
                response.get('body').encode('UTF-8'))
            return parsed_atom.get_item_list()
        else:
            return False
Exemplo n.º 28
0
class AdflyApiExample():
    #FROM EXAMPLE
    ##    BASE_HOST = 'https://api.adf.ly'
    ##    # TODO: Replace this with your secret key.
    ##    SECRET_KEY = '4c8fa05a-d826-4c06-86e4-59b86bf4868c'
    ##    # TODO: Replace this with your public key.
    ##    PUBLIC_KEY = '2ba3f6ce601d043c177eb2a83eb34f5f'
    ##    # TODO: Replace this with your user id.
    ##    USER_ID = 2
    ##    AUTH_TYPE = dict(basic=1, hmac=2)

    BASE_HOST = 'https://api.adf.ly'
    SECRET_KEY = 'YOUR SECRET KEY'
    PUBLIC_KEY = 'YOUR PUBLIC KEY'
    USER_ID = 'YOUR USER ID'
    AUTH_TYPE = dict(basic=1, hmac=2)

    def __init__(self):
        # In this example we use rest client provided by
        # http://code.google.com/p/python-rest-client/
        # Of course you are free to use any other client.
        self._connection = Connection(self.BASE_HOST)

    def get_groups(self, page=1):
        response = self._connection.request_get('/v1/urlGroups',
                                                args=self._get_params(
                                                    dict(_page=page),
                                                    self.AUTH_TYPE['hmac']))
        return json.loads(response['body'])

    def expand(self, urls, hashes=[]):
        params = dict()

        if type(urls) == list:
            for i, url in enumerate(urls):
                params['url[%d]' % i] = url
        elif type(urls) == str:
            params['url'] = urls

        if type(hashes) == list:
            for i, hashval in enumerate(hashes):
                params['hash[%d]' % i] = hashval
        elif type(hashes) == str:
            params['hash'] = hashes

        response = self._connection.request_get('/v1/expand',
                                                args=self._get_params(
                                                    params,
                                                    self.AUTH_TYPE['basic']))
        return json.loads(response['body'])

    def shorten(self, urls, domain=None, advert_type=None, group_id=None):
        params = dict()
        if domain:
            params['domain'] = domain
        if advert_type:
            params['advert_type'] = advert_type
        if group_id:
            params['group_id'] = group_id

        if type(urls) == list:
            for i, url in enumerate(urls):
                params['url[%d]' % i] = url
        elif type(urls) == str:
            params['url'] = urls

        response = self._connection.request_post('/v1/shorten',
                                                 args=self._get_params(
                                                     params,
                                                     self.AUTH_TYPE['basic']))
        return json.loads(response['body'])

    def get_urls(self, page=1, search_str=None):
        response = self._connection.request_get('/v1/urls',
                                                args=self._get_params(
                                                    dict(_page=page,
                                                         q=search_str),
                                                    self.AUTH_TYPE['hmac']))
        return json.loads(response['body'])

    def update_url(self, url_id, **kwargs):
        params = dict()

        allowed_kwargs = [
            'url', 'advert_type', 'title', 'group_id', 'fb_description',
            'fb_image'
        ]
        for k, v in kwargs.items():
            if k in allowed_kwargs:
                params[k] = v

        response = self._connection.request_put('/v1/urls/%d' % url_id,
                                                args=self._get_params(
                                                    params,
                                                    self.AUTH_TYPE['hmac']))
        return json.loads(response['body'])

    def delete_url(self, url_id):
        response = self._connection.request_delete('/v1/urls/%d' % url_id,
                                                   args=self._get_params(
                                                       dict(),
                                                       self.AUTH_TYPE['hmac']))
        return json.loads(response['body'])

    def _get_params(self, params={}, auth_type=None):
        """Populates request parameters with required parameters,
        such as _user_id, _api_key, etc.
        """
        auth_type = auth_type or self.AUTH_TYPE['basic']

        params['_user_id'] = self.USER_ID
        params['_api_key'] = self.PUBLIC_KEY

        if self.AUTH_TYPE['basic'] == auth_type:
            pass
        elif self.AUTH_TYPE['hmac'] == auth_type:
            # Get current unix timestamp (UTC time).
            params['_timestamp'] = int(time.time())
            params['_hash'] = self._do_hmac(params)
        else:
            raise RuntimeError

        return params

    def _do_hmac(self, params):
        if type(params) != dict:
            raise RuntimeError

        # Get parameter names.
        keys = params.keys()
        # Sort them using byte ordering.
        # So 'param[10]' comes before 'param[2]'.
        keys.sort()
        queryParts = []

        # Url encode query string. The encoding should be performed
        # per RFC 1738 (http://www.faqs.org/rfcs/rfc1738)
        # which implies that spaces are encoded as plus (+) signs.
        for key in keys:
            quoted_key = urllib.quote_plus(str(key))
            if params[key] is None:
                params[key] = ''

            quoted_value = urllib.quote_plus(str(params[key]))
            queryParts.append('%s=%s' % (quoted_key, quoted_value))

        return hmac.new(self.SECRET_KEY, '&'.join(queryParts),
                        hashlib.sha256).hexdigest()