Пример #1
0
def create(opt, conf, username, phone="no"):
    """
    Добавляем новый номер телефона
    fs-api -c endpoint -a create -u <username> --phone=<phone> [-p <password> --enabled=<1|0>] [-s site]
    """
    url = "{2}://{0}{1}".format(conf.get(opt.section, 'host'),conf.get(opt.section, 'pref'),conf.get(opt.section, 'protocol'))
    #TODO: если неуказан номер то берет первый из списка
    if phone == 'no':
        print('fs-api -c endpoint -a create -u <username> --phone=<phone> [-p <password> --enabled=<1|0>] [-s site]')
    else:
        if int(opt.enabled) == 1:
            enabled = 'true'
        else:
            enabled = 'false'
        args={'username': username, 'phone': phone, 'enabled': enabled}
        #if first_name is not None:
        #    args['first_name'] = first_name
        #if last_name is not None:
        #    args['last_name'] = last_name
        if opt.password != 'no':
            args['password'] = opt.password
        #print(args)
        con = Connection(url, username=conf.get(opt.section, 'user'), password=conf.get(opt.section, 'passwd'), path_cache=PATH_CACHE)
        res = con.save("post", '/endpoint/', args)
        if res:
            view(res)
        else:
            print('No create endpoint: {0}; status: {1}'.format(opt.phone,con.status))
Пример #2
0
def create(opt, conf, username, first_name=None, last_name=None):
    """
    Добавляем пользователя
    fs-api -c account -a create -u <username> -e <email> [-p <password> --enabled=<1|0> -t <tariff_id>] [first_name last_name] # новый аккаунт
    """
    url = "{2}://{0}{1}".format(
        conf.get(opt.section, "host"), conf.get(opt.section, "pref"), conf.get(opt.section, "protocol")
    )
    if opt.email == "no":
        print(
            "fs-api -c account -a create -u <username> -e <email> [-p <password> --enabled=<1|0> -t <tariff_id>] [first_name last_name]"
        )
    else:

        if int(opt.enabled) == 1:
            enabled = "true"
        else:
            enabled = "false"
        args = {"username": username, "email": opt.email, "enabled": enabled}
        if first_name is not None:
            args["first_name"] = first_name
        if last_name is not None:
            args["last_name"] = last_name
        if opt.password != "no":
            args["password"] = opt.password
        # print(args)
        con = Connection(
            url, username=conf.get(opt.section, "user"), password=conf.get(opt.section, "passwd"), path_cache=PATH_CACHE
        )
        res = con.save("post", "/account/", args)
        if res:
            view(res)
        else:
            print("No create account: {0}; status: {1}".format(username, con.status))
Пример #3
0
def delete(opt, conf, username):
    """
    Деактивируем телефон
    fs-api -c endpoint -a delete --phone <phone>
    """
    url = "{2}://{0}{1}".format(conf.get(opt.section, 'host'),conf.get(opt.section, 'pref'),conf.get(opt.section, 'protocol'))
    con = Connection(url, username=conf.get(opt.section, 'user'), password=conf.get(opt.section, 'passwd'), path_cache=PATH_CACHE)
    res = con.delete('/endpoint/phone/{0}/'.format(opt.phone))
    if res:
        print("Delete: {0}".format(res))
Пример #4
0
def get(opt, conf, username=None, arg=None):
    """
    Просмотр аккаунтов
    fs-api # все аккаунты
    fs-api --start=10 --limit=15 # все аккаунты с 10 по 25
    fs-api -s diller # все аккаунты для диллера diller
    fs-api -u <username> # просмотр аккаунта username
    """
    url = "{2}://{0}{1}".format(
        conf.get(opt.section, "host"), conf.get(opt.section, "pref"), conf.get(opt.section, "protocol")
    )
    con = Connection(
        url, username=conf.get(opt.section, "user"), password=conf.get(opt.section, "passwd"), path_cache=PATH_CACHE
    )
    args = {"start": opt.start, "limit": opt.limit}
    if username is not None:
        mod_query = "/account/{0}/".format(username)
        res = con.search(mod_query, args)
        # print("res:{0}".format(res))
        if res:
            view(res.get("accounts"))
        else:
            print("No account: {0}; status: {1}".format(username, con.status))
    else:
        mod_query = "/account/"
        res = con.search(mod_query, args)
        # print(con.response)
        # print("res:{0}".format(res))
        if res:
            print("count: {0}".format(res.get("count")))
            print("next: {0}".format(res.get("next")))
            print("previous: {0}".format(res.get("previous")))

            accounts = res.get("accounts")
            for a in accounts:
                if a.get("enabled"):
                    enabled = "enabled"
                else:
                    enabled = "disabled"
                print(
                    "cash: {0}; tariff: [{4}] {5};  username: {1}({3}) {2}".format(
                        a.get("cash"),
                        a.get("accountcode").get("username"),
                        enabled,
                        a.get("accountcode").get("email"),
                        a.get("tariff").get("id"),
                        a.get("tariff").get("name"),
                    )
                )
        else:
            print("No account, status: {0}".format(con.status))
Пример #5
0
def update(opt, conf, arg):
    """
    обновление параметров телефонного номера
    fs-api -c endpoint -a update --phone <phone> <key> <val>
    Пример:
        fs-api -c endpoint -a update --phone 380895000000 effective_caller_id_name Neskaju
    """
    url = "{2}://{0}{1}".format(conf.get(opt.section, 'host'),conf.get(opt.section, 'pref'),conf.get(opt.section, 'protocol'))
    con = Connection(url, username=conf.get(opt.section, 'user'), password=conf.get(opt.section, 'passwd'), path_cache=PATH_CACHE)
    res = con.save('put', '/endpoint/phone/{0}/'.format(opt.phone), arg)
    if res:
        view(res)
    else:
        print('No update endpoint: {0}; status: {1}'.format(opt.phone,con.status))
Пример #6
0
def delete(opt, conf, username):
    """
    Удаляем аккаунт
    fs-api -c account -a delete -u <username>
    """
    url = "{2}://{0}{1}".format(
        conf.get(opt.section, "host"), conf.get(opt.section, "pref"), conf.get(opt.section, "protocol")
    )
    con = Connection(
        url, username=conf.get(opt.section, "user"), password=conf.get(opt.section, "passwd"), path_cache=PATH_CACHE
    )
    res = con.delete("/account/{0}/".format(username))
    if res:
        print("Delete: {0}".format(res))
Пример #7
0
def update(opt, conf, username, arg):
    """
    обновление аккаунта
    fs-api -c account -a update -u <username> <key> <val>
    Пример:
        fs-api -c account -a update -u testuser first_name Neskaju
    """
    url = "{2}://{0}{1}".format(
        conf.get(opt.section, "host"), conf.get(opt.section, "pref"), conf.get(opt.section, "protocol")
    )
    con = Connection(
        url, username=conf.get(opt.section, "user"), password=conf.get(opt.section, "passwd"), path_cache=PATH_CACHE
    )
    res = con.save("put", "/account/{0}/".format(username), arg)
Пример #8
0
    def __init__(self, base_url, options):
        """ Base URL for the store should be pretty self-explanatory. E.g. something like
            "http://api.linktel.com.ua/api"
            Only needs to enter the username/password if this class is going to tinker
             with things."""
        if base_url.endswith('/'):
            base_url = base_url[:-1]

        self.base_url = base_url
        # Split the given URL
        if base_url:
            self.conn = Connection(base_url, username=options.user, password=options.passwd)
Пример #9
0
    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)
Пример #10
0
def get(opt, conf, arg=None):
    """
    просмотр номера телефона
    fs-api -c endpoint -u <username> # все телефонные номера для аккаунта username
    fs-api -c endpoint -u <username> --start=10 --limit=15 # все телефоны с 10 по 25
    """
    # TODO: доделать пока неработает
    url = "{2}://{0}{1}".format(conf.get(opt.section, 'host'),conf.get(opt.section, 'pref'),conf.get(opt.section, 'protocol'))
    con = Connection(url, username=conf.get(opt.section, 'user'), password=conf.get(opt.section, 'passwd'), path_cache=PATH_CACHE)
    args={'start': opt.start, 'limit': opt.limit}
    if opt.username is not None:
        mod_query = "/endpoint/{0}/".format(opt.username)
        res = con.search(mod_query, args)
        #print("res:{0}".format(res))
        if res:
            view(res.get("accounts"))
        else:
            print('No account: {0}; status: {1}'.format(username,con.status))
    else:
        mod_query = "/endpoint/"
        res = con.search(mod_query, args)
        #print(con.response)
        #print("res:{0}".format(res))
        if res:
            print("count: {0}".format(res.get("count")))
            print("next: {0}".format(res.get("next")))
            print("previous: {0}".format(res.get("previous")))

            accounts = res.get("accounts")
            for a in accounts:
                if a.get('enabled'):
                    enabled = 'enabled'
                else:
                    enabled = 'disabled'
                print("phone: {0}; password: {1}({2})".format(a.get("uid"), a.get('password'), enabled))
        else:
            print('No endpoint, status: {0}'.format(con.status))
Пример #11
0
class Tinyurl(object):
    def __init__(self):
        self._conn = Connection(TINYURL_ENDPOINT)
        # TODO test availability
        self.active = True
    
    def get(self, url):
        # Handcraft the ?url=XXX line as Tinyurl doesn't understand urlencoded
        # params - at least, when I try it anyway...
        response = self._conn.request_get("?%s=%s" % (TINYURL_PARAM, url))
        http_status = response['headers'].get('status')
        if http_status == "200":
            return response.get('body').encode('UTF-8')
        else:
            raise ConnectionError
Пример #12
0
 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)
Пример #13
0
 def __init__(self):
     self._conn = Connection(TINYURL_ENDPOINT)
     # TODO test availability
     self.active = True
Пример #14
0
class ClietAPI():
    """"""

    #----------------------------------------------------------------------
    def __init__(self, base_url, options):
        """ Base URL for the store should be pretty self-explanatory. E.g. something like
            "http://api.linktel.com.ua/api"
            Only needs to enter the username/password if this class is going to tinker
             with things."""
        if base_url.endswith('/'):
            base_url = base_url[:-1]

        self.base_url = base_url
        # Split the given URL
        if base_url:
            self.conn = Connection(base_url, username=options.user, password=options.passwd)

    def _query_search(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 = {}
        passed_args.update(args)
        return self.conn.request_get(query, args=passed_args, headers={'Accept':'text/json'})

    def save(self, metod, resource, args={}, body = None):
        """
        metod post or put
        """
        response = self.conn.request(resource, metod, args = args, body = body)
        headers = response.get('headers')
        status = headers.get('status', headers.get('Status'))

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

    def delete(self, resource):
        """
        metod post or put
        """
        response = self.conn.request_delete(resource)
        headers = response.get('headers')
        status = headers.get('status', headers.get('Status'))

        if status in ['200', 200, '204', 204]:
            return True
        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(query, args)
        headers = response.get('headers')
        status = headers.get('status', headers.get('Status'))

        if status in ['200', 200, '204', 204]:
            body = simplejson.loads(response.get('body').encode('UTF-8'))
            return body
        else:
            return False
Пример #15
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