Ejemplo n.º 1
0
    def run(self):
        while not self.terminated:
            if self.event.wait(1):
                try:
                    self.logger.debug("An upload processor is running...")
                    if self.stream:
                        # Count how many retries so far.
                        retry = 0
                        # Flag when the upload completed successfully.
                        success = False
                        # Maxiumum amount of exceptions handled before giving up.
                        MAX_RETRY = 3
                        while (retry < MAX_RETRY):
                            try:
                                client = ApiClient(self.username, self.token, self.url)
                                client.upload_file(self.device_id, stream=self.stream, ext='.jpg')
                                retry = MAX_RETRY
                                success = True
                            except Exception as e:
                                self.logger.error(e)
                                retry += 1

                        # Fallback recovery, this is bad - save the image to another location.
                        if not success:
                            self.logger.error("Failed to report an image...")
                            self.save_image(stream=self.stream, dir=self.fallback_dir)
                finally:
                    self.event.clear()
                    with self.lock:
                        self.logger.debug("Upload processor returned to the pool.")
                        self.pool.append(self)
Ejemplo n.º 2
0
def run_main(args: Arguments):
    api = ApiClient(args.subdomain, args.username, args.token)
    stats = UserStats()
    topic_id = None
    posts = api.get_posts(topic_id,
                          filter_from=args.filter_from,
                          filter_to=args.filter_to)
    for post in posts:
        if api_util.included_in_date_range(post['created_at'],
                                           filter_from=args.filter_from,
                                           filter_to=args.filter_to):
            stats.observe_post_by_user(post['author_id'])
        comments = api.get_comments(post['id'],
                                    filter_from=args.filter_from,
                                    filter_to=args.filter_to)
        for comment in comments:
            stats.observe_comment_by_user(comment['author_id'])
    if args.outputfile:
        with open(args.outputfile, "w") as f:
            for line in stats.to_csv():
                f.write(line)
                f.write('\n')
        print(f"Results saved to {args.outputfile}")
    else:
        print("--- RESULTS ---")
        for line in stats.to_csv():
            print(line)
Ejemplo n.º 3
0
def get_mock_trello(trello_config):
    api_client = ApiClient()
    api_client.get = mock_get([{
        "id": "5eeb648682bb2c685cc659f0",
        "name": NOT_STARTED,
        "closed": False,
        "pos": 1,
        "softLimit": None,
        "idBoard": "5eeb6486ebbfa66d2e519922",
        "subscribed": False
    }, {
        "id": "5eeb648623bb2c685cc659f0",
        "name": IN_PROGRESS,
        "closed": False,
        "pos": 2,
        "softLimit": None,
        "idBoard": "5eeb6486ebbfa66d2e519922",
        "subscribed": False
    }, {
        "id": "5eeb6486cae807625c9f0819",
        "name": COMPLETED,
        "closed": False,
        "pos": 3,
        "softLimit": None,
        "idBoard": "5eeb6486ebbfa66d2e519922",
        "subscribed": False
    }])
    trello = Trello(trello_config, api_client)
    return trello, api_client, trello_config
Ejemplo n.º 4
0
 def __init__(self):
     self._last_invalid_measurements = {}
     self._mail_sender = MailSender(config['sender'],
                                    config['receiver_email'],
                                    config['MONITORED_VALUES'])
     self._timer = MonitorTimer()
     self._api_client = ApiClient()
     self.test_dedicated_state = False
Ejemplo n.º 5
0
class Credentials:
    def __init__(self, app_token):
        self.base = 'credentials'
        self.api_client = ApiClient(app_token)

    def widget(self):
        return self.api_client.call_api('get', url=self.base)

    def resource(self, resource_id):
        url = self.base + '?search=resource_id:' + str(resource_id)
        return self.api_client.call_api('get', url)
Ejemplo n.º 6
0
def main():
    if len(sys.argv) < 3:
        print(
            'Usage: Path to model config path to model last_checkpoint model type'
        )
    output_dir = sys.argv[2]
    model_type = sys.argv[3]
    cfg.merge_from_file(sys.argv[1])
    cfg.merge_from_list(["OUTPUT_DIR", output_dir])
    model = ChataDemo(cfg, model_type)

    client = ApiClient()
    client.login()
    #latest_id = client.get_latest()
    newest_ids = client.get_new_publications()
    for pub_id in newest_ids:
        pages = client.get_pages(pub_id)
        print(pub_id)
        for id, url in pages:
            print(id)
            print(url)
            resp = urllib.request.urlopen(url)
            image = np.asarray(bytearray(resp.read()), dtype="uint8")
            image = cv2.imdecode(image, cv2.IMREAD_COLOR)
            predictions = model.get_predictions(image)
            annotations = annotate(predictions)
            print(annotations)
            if annotations:
                print('writing annotation...')
                for annotation in annotations:
                    client.add_annotation(id, annotation)
Ejemplo n.º 7
0
    def log_out(access_token: str) -> bool:
        """
        Revoke your access_token
        :param access_token: <str>
        :return:
        """

        resource_path = '/oauth/access_token'
        api_client = ApiClient(access_token=access_token)

        api_client.call_api(resource_path=resource_path, method='DELETE')

        confirm(f"Successfully logged out.")
        return True
Ejemplo n.º 8
0
def main():
    charts_cfg = cfg.clone()
    tables_cfg = cfg.clone()
    charts_cfg.merge_from_file(charts_path)
    tables_cfg.merge_from_file(tables_path)
    model_charts = ChataDemo(charts_cfg, "charts")
    model_tables = ChataDemo(tables_cfg, "tables")
    
    client = ApiClient()
    client.login()
    newest_ids = client.get_new_publications()
    for pub_id in newest_ids:
        pages = client.get_pages(pub_id)
        print(pub_id)
        for id, url in pages:
            print(id)
            print(url)
            resp = urllib.request.urlopen(url)
            image = np.asarray(bytearray(resp.read()), dtype="uint8")
            image = cv2.imdecode(image, cv2.IMREAD_COLOR)
            predictions_charts = model_charts.get_predictions(image)
            predictions_tables = model_tables.get_predictions(image)
            annotations_charts = annotate(predictions_charts)
            annotations_tables = annotate(predictions_tables)
            annotations = annotations_charts+annotations_tables
            print(annotations)
            if annotations:
                client.add_annotation(id, annotations)
    def authenticate(self):
        try:
            configuration = Configuration()
            if os.name != 'nt':
                permissions = os.stat(self.config).st_mode
                if (permissions & 4) or (permissions & 32):
                    print(
                        'Warning: Your Kaggle API key is readable by other users on '
                        +
                        'this system! To fix this, you can run \'chmod 600 {}\''
                        .format(self.config))
            with open(self.config, 'r') as f:
                config_data = json.load(f)

            self.copy_config_value(self.CONFIG_NAME_PROXY, config_data)
            self.copy_config_value(self.CONFIG_NAME_PATH, config_data)
            self.copy_config_value(self.CONFIG_NAME_COMPETITION, config_data)
            self.copy_config_value(self.CONFIG_NAME_USER, config_data)

            configuration.username = config_data[self.CONFIG_NAME_USER]
            configuration.password = config_data[self.CONFIG_NAME_KEY]
            if self.CONFIG_NAME_PROXY in config_data:
                configuration.proxy = config_data[self.CONFIG_NAME_PROXY]
            self.api_client = ApiClient(configuration)

        except Exception as error:
            if 'Proxy' in type(error).__name__:
                sys.exit('The specified proxy ' +
                         config_data[self.CONFIG_NAME_PROXY] +
                         ' is not valid, please check your proxy settings')
            else:
                sys.exit(
                    'Unauthorized: you must download an API key from ' +
                    'https://www.kaggle.com/<username>/account\nThen put ' +
                    self.config_file + ' in the folder ' + self.config_path)
Ejemplo n.º 10
0
class Availability:
    def __init__(self, app_token):
        self.base = 'findtime'
        self.api_client = ApiClient(app_token)

    def query(self, resource_ids, **kwargs):
        """
        Required
        Note: only resource_ids or calendar_ids are required
        :param resource_ids: array, resource-uuids
        :param: calendar_ids: array of strings, use this for only specific calendars

        Optional:
        :param length: string, how much time must each avaliable timeslot contain
        :param filters: array (mixed types, see filters)
        :param start: string, human written time eg, 1 day, tomorrow, 2 weeks
        :param future: string, defines the end of the search-space
        :param sort: string, chronologica order of results, accepted values: asc, desc
        :param ignore_all_day_events: boolean, ignore all day events in this query?
        :param all_solutions: boolean, return overlapping timeslots?
        :param emails: array of strings, check emails for mutual availability
        :param buffer: string, amount of time to add to each booking
        :param no_day_span: boolean, if you dont want time-slots that span span days
        """

        data = kwargs

        if type(resource_ids) == str:
            data['resource_ids'] = [resource_ids]

        return self.api_client.call_api('post', self.base, data)
Ejemplo n.º 11
0
 def __init__(self, access_token: str):
     """
     VenmoAPI Client
     :param access_token: <str> Need access_token to work with the API.
     """
     super().__init__()
     self.__access_token = validate_access_token(access_token=access_token)
     self.__api_client = ApiClient(access_token=access_token)
     self.user = UserApi(self.__api_client)
Ejemplo n.º 12
0
 def get_data(self):
     result = []
     response = ApiClient.fetch(self, self.url)
     data = response.get("data", {})
     children = data.get("children", {})
     return [
         child['data']['preview']['images'][0]['source']['url']
         for child in children if 'preview' in child.get("data", {})
     ]
Ejemplo n.º 13
0
def results():
    # retrieves user query inputs
    park_name = request.args.get('park_name')
    state = request.args.get('state')
    designation = request.args.get('designation')
    keyword = request.args.get('keyword')
    # api client builds the query with specific parameters
    parks = ApiClient.parks(park_name=park_name,
                            state=state,
                            designation=designation,
                            keyword=keyword)
    return render_template("results.html", parks=parks)
Ejemplo n.º 14
0
class CoreV1Api(object):
    def __init__(self):
        self.api_client = ApiClient()

    def list_pods(self, **kwargs):

        namespace = kwargs.get('namespace')
        url = 'api/v1/pods'
        if namespace:
            url = 'api/v1/namespaces/{}/pods'.format(namespace)

        headers = {
            'Content-type': 'application/json'
        }

        return self.api_client.api_call('GET', url, headers=headers)

    def list_services(self, **kwargs):

        namespace = kwargs.get('namespace')
        url = 'api/v1/services'
        if namespace:
            url = 'api/v1/namespaces/{}/services'.format(namespace)
        headers = {
            'Content-type': 'application/json'
        }

        return self.api_client.api_call('GET', url, headers=headers)

    def submit_deployment(self, **kwargs):
        if ('data' not in kwargs) or (kwargs['data'] is None):
            raise ValueError("Missing the required parameter `data` when calling `submit_deployment`")

        data = kwargs.get('data')
        namespace = kwargs.get('namespace', 'default')
        url = 'apis/extensions/v1beta1/namespaces/{}/deployments'.format(namespace)
        headers = {
            'Content-type': 'application/yaml'
        }
        return self.api_client.api_call('POST', url, data=data, headers=headers)
Ejemplo n.º 15
0
class CoreV1Api(object):
    def __init__(self):
        self.api_client = ApiClient()

    def list_pods(self, **kwargs):

        namespace = kwargs.get('namespace')
        url = 'api/v1/pods'
        if namespace:
            url = 'api/v1/namespaces/{}/pods'.format(namespace)

        headers = {'Content-type': 'application/json'}

        return self.api_client.api_call('GET', url, headers=headers)

    def list_services(self, **kwargs):

        namespace = kwargs.get('namespace')
        url = 'api/v1/services'
        if namespace:
            url = 'api/v1/namespaces/{}/services'.format(namespace)
        headers = {'Content-type': 'application/json'}

        return self.api_client.api_call('GET', url, headers=headers)

    def submit_deployment(self, **kwargs):
        if ('data' not in kwargs) or (kwargs['data'] is None):
            raise ValueError(
                "Missing the required parameter `data` when calling `submit_deployment`"
            )

        data = kwargs.get('data')
        namespace = kwargs.get('namespace', 'default')
        url = 'apis/extensions/v1beta1/namespaces/{}/deployments'.format(
            namespace)
        headers = {'Content-type': 'application/yaml'}
        return self.api_client.api_call('POST',
                                        url,
                                        data=data,
                                        headers=headers)
Ejemplo n.º 16
0
class AppClient:
    def __init__(self, app_token):
        self.base = 'app'
        self.api_client = ApiClient(app_token, self.base)

    def get_current_app(self):
        """
        returns the application object
        """

        return self.api_client.call_api('get')

    def invite_resources(self, email):
        """
        required
        :param email: string, email address to invite

        returns: the new resource object
        """
        url = self.base + '/invite'
        data = {'email': email}
        return self.api_client.call_api('post', url, data=data)
Ejemplo n.º 17
0
 def __init__(self,
              base_url,
              token,
              sleep_seconds=2,
              timeout_seconds=sys.maxsize,
              timeout_func=_default_timeout_func,
              verbose=True):
     """
     :param base_url: Base URL of API such as https://acme.cloud.databricks.com/api/2.0
     :param token: API token
     :param sleep_seconds: Seconds to sleep when polling for cluster readiness
     :param timeout_seconds: Timeout in seconds
     :param verbose: To be verbose or not?
     """
     self.api_client = ApiClient(base_url, token)
     self.base_url = base_url
     self.sleep_seconds = sleep_seconds
     self.timeout_seconds = timeout_seconds
     self.timeout_func = timeout_func
     self.verbose = verbose
     config_file = "config.json"
     if os.path.exists(config_file):
         logging.info("Reading config file {}".format(config_file))
         with open(config_file, 'rb') as f:
             dct = json.loads(f.read())
         self.cluster_noninit_states = set(dct['cluster_noninit_states'])
         self.run_terminal_states = set(dct['run_terminal_states'])
     else:
         self.cluster_noninit_states = {
             "RUNNING", "TERMINATED", "ERROR", "UNKNOWN"
         }
         self.run_terminal_states = {
             "TERMINATED", "SKIPPED", "INTERNAL_ERROR"
         }
     logging.info("cluster_noninit_states: {}".format(
         self.cluster_noninit_states))
     logging.info("run_terminal_states: {}".format(
         self.run_terminal_states))
Ejemplo n.º 18
0
 def get_access_token(username: str,
                      password: str,
                      device_id: str = None) -> str:
     """
     Log in using your credentials and get an access_token to use in the API
     :param username: <str> Can be username, phone number (without +1) or email address.
     :param password: <str> Account's password
     :param device_id: <str> [optional] A valid device-id.
     :return: <str> access_token
     """
     authn_api = AuthenticationApi(api_client=ApiClient(),
                                   device_id=device_id)
     return authn_api.login_with_credentials_cli(username=username,
                                                 password=password)
Ejemplo n.º 19
0
    def test_get_comments(self, mock_requests_get):

        expected = [
            {'id': 2, 'body': 'some comment', 'postId': 2},
            {'id': 3, 'body': 'some comment', 'postId': 2}
        ]

        mock_response = MagicMock()
        mock_response.json.return_value = expected

        mock_requests_get.return_value = mock_response

        r = ApiClient().get_comments()

        self.assertEqual(r, expected)
Ejemplo n.º 20
0
def test_app():
    trello_config = Config(".env").trello_config
    trello = Trello(trello_config, ApiClient()).create_test_board()
    application = app.create_app({}, trello)

    thread = Thread(target=lambda: application.run(use_reloader=False))
    thread.daemon = True
    thread.start()

    # I really feel like this shouldn't be necessary, but otherwise selenium makes requests before flask has started
    time.sleep(1)

    yield app

    thread.join(1)
    trello.delete_board()
def run_main(args: Arguments):
    api = ApiClient(args.subdomain, args.username, args.token)
    selected_user_id = None
    while True:
        mode = enquiries.choose('Select an action:', ['Get stats', 'Award badge', 'Exit'])
        print(mode)
        if mode == 'Exit':
            return
        elif mode == 'Award badge':
            if selected_user_id:
                badges = api.get_badges()
                badge_labels = map(lambda b: f"{b['id']} {b['name']}", badges)
                badge = enquiries.choose('Select a badge:', badge_labels)
                badge_id = badge.split(' ', 1)[0]
                print(f"Assigning badge {badge_id} to user {selected_user_id}")
                response = api.create_badge_assignment(selected_user_id, badge_id)
                print(str(response))
            else:
                print("First select a user, for example by looking at stats")
        elif mode == 'Get stats':
            stats = UserStats()
            topic_id = None
            posts = api.get_posts(topic_id, filter_from=args.filter_from, filter_to=args.filter_to)
            for post in posts:
                if api_util.included_in_date_range(post['created_at'], filter_from=args.filter_from, filter_to=args.filter_to):
                    stats.observe_post_by_user(post['author_id'])
                comments = api.get_comments(post['id'], filter_from=args.filter_from, filter_to=args.filter_to)
                for comment in comments:
                    stats.observe_comment_by_user(comment['author_id'])
            users = []
            for user_id in stats.stats_by_user:
                userstats = stats.stats_by_user[user_id]
                user = f"{user_id} - {userstats.get('posts') or 0} posts, {userstats.get('comments') or 0} comments"
                users.append(user)
            select_user = enquiries.choose('Here are your top contributors. Select one:', users)
            selected_user_id = select_user.split(' ', 1)[0]
Ejemplo n.º 22
0
from api_client import ApiClient
import json

api = ApiClient()

query_params = {'page_no': 1, "page_size": 2}
rooms_resp = api.request("GET",
                         "http://127.0.0.1:5000/rooms",
                         query_params=query_params,
                         _preload_content=True)
data = json.loads(rooms_resp.data)

f1 = "D:/work/py-apps/client/config/openapi.json"

with open(f1) as f:
    oas_data = json.load(f)

urls = []
"""
path:
method: 
parameters,
order: 
dependent: [""]
type: full|incremental
pagination: true or false
"""
for path, path_dict in oas_data.get("paths"):

    for method, method_dict in path_dict:
        url = {
Ejemplo n.º 23
0
from dotenv import load_dotenv
import os
from state import read_state, write_state, ALERTED_FOR_LEVEL, update_state_alert_level
from webhook import make_webhook


def optional_environ(name, default):
    return default if name not in os.environ else os.environ[name]


load_dotenv()

api_host = optional_environ("API_HOST", "localhost")
api_port = int(optional_environ("API_PORT", "5000"))

client = ApiClient(api_host, api_port)
state = read_state()


def get_msg_data(dist):
    return f"\n\tDistance: {dist}\n\tWarning At: {warn_distance}"


def handle_salt_low(current_distance):
    dist = current_distance["distance"]
    alerted = ALERTED_FOR_LEVEL in state
    if dist < warn_distance or alerted:
        return

    update_state_alert_level(dist)
    webhook(
Ejemplo n.º 24
0
 def __init__(self, app_token):
     self.base = 'calendars'
     self.api_client = ApiClient(app_token, self.base)
Ejemplo n.º 25
0
class Calendar:

    def __init__(self, app_token):
        self.base = 'calendars'
        self.api_client = ApiClient(app_token, self.base)

    def create(self, resource_id, name, description, **kwargs):
        """
        required
        :param resource_id: string, id of the resource to create the calendar for
        :param name: string, name of calendar
        :param description: string, description of the calendar

        optional
        :param backgroundcolor: string, hex color to use as background for events
        :param foregroundcolor: string, hex color to use as forground for events

        returns new calendar object
        """
        data = kwargs

        data['resource_id'] = resource_id
        data['name'] = name
        data['description'] = description

        return self.api_client.call_api('post', data=data)

    def list(self):
        return self.api_client.call_api('get')

    def retrieve(self, calendar_id):
        """
        required
        :param calendar_id: string, id of calendar

        returns calendar object
        """
        url = self.base + '/' + str(calendar_id)

        return self.api_client.call_api('get', url)

    def update(self, calendar_id, **kwargs):
        """
        required
        :param calendar_id: string, id of calendar

        optional
        :param name: string, name of the calendar
        :param description: string, description of the calendar
        :param foregroundcolor: string, hex color code of the forground
        :param backgroundcolor: string, hex color code of the background
        :param provider_sync: boolean, enable or disable syncing
        :param primary: boolean, set the calendar as primary

        return boolean
        """
        url = self.base + '/' + str(calendar_id)
        data = kwargs

        return self.api_client.call_api('put', url, data)

    def delete(self, calendar_id):
        """
        required
        :param calendar_id: string, calendar id to delete

        returns boolean
        """
        url = self.base + '/' + str(calendar_id)
        return self.api_client.call_api('delete', url)
Ejemplo n.º 26
0
 def create(cls, base, username, password):
     """Factory method using default ApiClient class."""
     client = ApiClient(base, username, password)
     return cls(client)
Ejemplo n.º 27
0
 def create(cls, base, username, password):
     client = ApiClient(base, username, password)
     return cls(client)
Ejemplo n.º 28
0
def main():

    """
    Let's setup logging.
    Note: The 'logging.json->loggers' configuration corresponds to each item returned by 'logging.getLogger'
    we'll just setup root to handle everything but we could target specific modules if we wanted to by simply
    defining a logger targeting a specific module name.
    """
    try:
        # attempt to load from the config file
        logger_config = Configuration('logging.json')
        logging.config.dictConfig(logger_config.get())

        logging.info("\n\n+ ================================ +\n+ Logging initialised. \n+ ================================ +\n")
    except Exception as e:
        print("Error with logging:", type(e), e)
        quit()

    config = Configuration('motion.json')

    force_capture = config.get('force_capture')
    force_capture_time = config.get('force_capture_time')

    url = config.get('home_url')
    username = config.get('username')
    token = config.get('token')

    device_id = None
    api = ApiClient(username, token, url)
    try:
        id_config = Configuration('id.json', catch_errors=False)
        device_id = id_config.get('id')
        if not device_id or not api.confirm_device_id(device_id):
            logging.error("Device ID not registered.")
            raise Exception("Device ID not registered.")
    except Exception as e:
        logging.debug(type(e), e)
        device_name = config.get('device_name')
        logging.info("Provisioning new device: %s." % device_name)
        device_id = api.register_device(device_name)
        id_config.set('id', device_id)
        id_config.save()
        logging.info("Registered new device: %s" % device_id)

    pool_lock = threading.Lock()
    pool = []

    m = None

    logging.info("Queuing upload processors.")

    # TODO: Upload processors consume memory, actively monitor memory and set an upper limit of how many processors can be running...
    # TODO: What if the server goes down? Network storage?
    for i in range(config.get('upload_processor_count', 15)):
        logging.debug("Adding upload processor %i." % i)
        pool.append(UploadProcessor(pool_lock, pool))

    logging.info("Starting motion detection loop.")

    fallback_dir = os.path.join(config.get('fallback_dir', '/home/pi/default_fallback_dir'), device_id)
    fallback_space_to_reserve = config.get('fallback_space_to_reserve', 16)

    try:
        m = Motion()
        running = True

        time_now = time.time()

        while running:
            # If motion was detected, or we've manually scheduled an image to be uploaded.
            if m.detect_motion() or m.get_capture_queue_length() > 0:
                with pool_lock:
                    if pool:
                        processor = pool.pop()
                    else:
                        processor = None
                if processor:
                    logging.debug("Acquired an upload processor.")

                    # load the processor with an image and required backend auth details.
                    processor.stream = m.pop_capture_stream_queue()
                    processor.username = username
                    processor.token = token
                    processor.url = url
                    processor.device_id = device_id
                    processor.fallback_dir = fallback_dir

                    # tell the processor to start working.
                    processor.event.set()
                else:
                    logging.info("Upload processor pool exhausted. Waiting for more uploaders...")

            # Do we want to capture images every N seconds?
            if (force_capture):
                # If timeout, let's capture an image to be processed.
                if ((time.time() - time_now) > force_capture_time):
                    # Reset the clock
                    time_now = time.time()
                    logging.info("Timeout reached. Capturing an image.")
                    m.capture_to_stream_queue()

            # Maintain fallback directory - don't use up more space than allocated
            keep_disk_space_free(dir=fallback_dir, fallback_space_to_reserve=fallback_space_to_reserve)

    except Exception as e:
        # TODO: Keep a proper log of errors and causes. We'll be logging this to the server and recovering the device by restarting it after a given timeout.
        logging.critical(type(e), e)
    finally:
        if m:
            m.close()

        logging.info("Terminating upload processors.")
        while pool:
            processor = pool.pop()
            processor.terminated = True
            processor.join()
            logging.debug("Upload processor terminated.")

    logging.info("Program ended.")
Ejemplo n.º 29
0
label_dict = {
    0: "T-shirt/top",
    1: "Trouser",
    2: "Pullover",
    3: "Dress",
    4: "Coat",
    5: "Sandal",
    6: "Shirt",
    7: "Sneaker",
    8: "Bag",
    9: "Ankle boot"
}

if __name__ == "__main__":
    # in order to use the web service, first create the instance of ApiClient class
    client = ApiClient(
    )  # NOTE: you need to call ApiClient(auth_key=YOUR_API_KEY)

    # if needed, test it with echo service
    # this is the simplest service that we can use to check if web service is running correctly
    # uncomment the code below to test echo()
    # val = client.echo("hi there!!!!")
    # print("For the echo service the returned value is: ", val)

    # you can use the method get_fashion_mnist_data to get data for fashion mnist
    # this will be rate limited to 10000 samples per call
    # for test accounts we will return a sample of 5 records to give you a feel
    num_samples = 10
    images, labels = client.get_fashion_mnist_data(num_samples)

    # you can print the labels returned by the service and examine
    # I am printing first 10 labels below
import pickle
import nltk
from api_client import ApiClient

my_auth_key = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1NTQ2MjY5ODAsImlhdCI6MTUzOTA3NDk4MCwibmJmIjoxNTM5MDc0OTgwLCJpZGVudGl0eSI6MTF9.kbHzSmkCqCu3akW7bMM6Vj4flZ-CVGuRYixh-k2x0Ps"

client = ApiClient(auth_key=my_auth_key)

#Load the dataset that was obtained from API and pickeled beforehand
with open('dataset.pkl', 'rb') as handle:
    dataset = pickle.load(handle)

#Extract all questions tokenized as sentences from the dataset
sentences = []
for i in dataset:
    sentences += nltk.sent_tokenize(i['question1'])
    sentences += nltk.sent_tokenize(i['question2'])

#Tokenize as words and find their frequency and select top 10000
word_dist = nltk.FreqDist()
for s in sentences:
    word_dist.update([i.lower() for i in nltk.word_tokenize(s)])

word_dist = word_dist.most_common(10000)

#obtain the glove vectors for the 10000 words from the web service
embeddings_list = []
for i in range(10, 100):
    embeddings_list += client.w2v(
        [i[0] for i in word_dist[i * 100:i * 100 + 100]])
def main():
    '''
    main entry point into the 'app'
    every function needs a Docstring in order to follow best
    practices
    '''

    # show_intro()
    '''
    collect the environment options from the command line
    this is packaged and managed via the EnvironmentOptions
    module in the environment_options directory
    '''
    environment_options = EnvironmentOptions()
    environment_options.get_options()

    if environment_options.debug:
        print(f'{environment_options}\n')
    '''
    check to make sure all required options have been specified
    if the username and password have not been provided on the
    command line, we'll prompt for them here
    '''
    if not environment_options.cluster_ip:
        raise DetailsMissingException('Cluster IP is required.')
    elif not environment_options.username:
        raise DetailsMissingException('Username is required.')
    elif not environment_options.password:
        raise DetailsMissingException('Password is required.')
    else:
        if environment_options.debug:
            print('All parameters OK.\n')

        if environment_options.debug:
            print('Creating instance of ApiClient class ...')
        client_blueprint = ApiClient(
            environment_options.cluster_ip,
            (f'blueprints/{environment_options.blueprint_uuid}'
             '/runtime_editables'), '', environment_options.username,
            environment_options.password, environment_options.read_timeout)
        if environment_options.debug:
            print(f'Client info: {client_blueprint}\n')
        if environment_options.debug:
            print('Requesting blueprint details ...\n')
        results_blueprint = client_blueprint.send_request()
        '''
        check to see if there are app profile references
        in the JSON response
        this is ONLY required if --app_profile was not
        specified as a command-line parameter

        if no app_profile has been specified, we can get it
        from the API request we just made
        if no app_profile has been specified but it is also
        missing from the JSON response, it likely indicates
        a failed request or a request that wasn't a call to
        a blueprint's runtime_editables api

        ***Important note:
        This demo script only looks for
        an app profile named 'Default'; if no matching app
        profile reference exists in your environment, please
        edit the 'Default' name below, and/or manually construct
        the value of app_profile_reference

        '''

        if environment_options.app_profile_reference == '':
            if environment_options.debug:
                print('No app profile specified; grabbing it from '
                      'the blueprint spec ...')
            if 'resources' in results_blueprint:
                if len(results_blueprint['resources']) > 0:
                    if 'app_profile_reference' in results_blueprint[
                            'resources'][0]:
                        if len(results_blueprint['resources'][0]) > 0:
                            if environment_options.debug:
                                print('App profile reference found.')
                            for reference in results_blueprint['resources']:
                                if (reference['app_profile_reference']['name']
                                        == 'Default'):
                                    environment_options.app_profile_reference = (
                                        reference['app_profile_reference']
                                        ['uuid'])
                                    if environment_options.debug:
                                        print(
                                            'Default app profile reference UUID extracted.'
                                        )
                        else:
                            print('no app profile references found; exiting.')
                            sys.exit()
                    else:
                        print('no app profile references found; exiting.')
                        sys.exit()
                else:
                    print('no app profile references found; exiting.')
                    sys.exit()
            else:
                print('no app profile references found; exiting.')
                sys.exit()
        else:
            if environment_options.debug:
                print(
                    'App profile already specified; using command line parameter ...'
                )
            pass
        '''
        at this point we do have our app profile reference
        we can continue with the next part of the script, i.e.
        creating the payload/request body that will be sent
        with the actual launch request
        '''
        if environment_options.debug:
            print('Building blueprint launch payload ...\n')
        payload = ('{ '
                   '"spec":{ '
                   f'"app_name":"{environment_options.app_name}", '
                   f'"app_description":"{environment_options.app_desc}", '
                   '"app_profile_reference":{ '
                   '"kind":"app_profile", '
                   '"name":"Default", '
                   f'"uuid":"{environment_options.app_profile_reference}" '
                   '} '
                   '} '
                   '}')

        if environment_options.debug:
            print(f'Payload: \n{payload}\n')

        if environment_options.debug:
            print('Creating instance of ApiClient class ...')
        client_launch = ApiClient(
            environment_options.cluster_ip,
            (f'blueprints/{environment_options.blueprint_uuid}'
             '/simple_launch'),
            payload,
            environment_options.username,
            environment_options.password,
            environment_options.read_timeout,
            method='post')

        if environment_options.debug:
            print(f'Client info: {client_launch}\n')
        if environment_options.debug:
            print('Sending request ...\n')
        results_launch = client_launch.send_request()
        if environment_options.debug:
            print(f'Results: \n{results_launch}')
Ejemplo n.º 32
0
class Resource:
    def __init__(self, app_token):
        self.base = "resources"
        self.api_client = ApiClient(app_token)

    def list(self):
        """
        returns an object of resources
        """
        return self.api_client.call_api('get', self.base)

    def create(self,
               name,
               timezone=None,
               email=None,
               first_name=None,
               last_name=None,
               password=None,
               tags=[]):
        """
        required
        :param name: string, name of the resource
        :param timezone: timezone of the resource
            defaults to machine local

        optional
        :param email: string
        :param first_name: string
        :param last_name: string
        :param password: string, if not set, a random password will be assigned

        """
        if not timezone:
            timezone = get_localzone()

        data = {'name': name, 'timezone': str(timezone)}
        if email:
            data['email'] = email

        if first_name:
            data['first_name'] = first_name

        if last_name:
            data['last_name'] = last_name

        if password:
            data['password'] = password

        return self.api_client.call_api('post', self.base, data)

    def retrieve(self, resource_id):
        """
        :param resource_id: id of resource

        returns the resource object
        """
        url = self.base + '/' + str(resource_id)
        return self.api_client.call_api('get', url)

    def update(self, resource_id, **kwargs):
        """
        required
        :param resource_id: id of the resource

        optional
        :param name: string
        :param first_name: string
        :param last_name: string
        :param password: string, if not set, a random password will be assigned
        :param timezone: timezone of the resource defaults to machine local
        """

        url = self.base + '/' + str(resource_id)

        data = kwargs
        data['id'] = str(resource_id)

        if len(data) <= 1:
            return False

        return self.api_client.call_api('put', url, kwargs)

    def delete(self, id):
        url = self.base + '/' + str(id)

        return self.api_client.call_api('delete', url)
Ejemplo n.º 33
0
 def __init__(self):
     self.api_client = ApiClient()
Ejemplo n.º 34
0
 def __init__(self, app_token):
     self.base = "resources"
     self.api_client = ApiClient(app_token)