예제 #1
0
    def execute(self) -> StepResult:
        dest_request_body_filepath = self.job_config["dest_updated_items_request_body_filepath"]

        with open(dest_request_body_filepath) as fp:
            body_str = fp.read()

        headers = {'X-API-Key': self.job_config['caiasoft_api_key'], 'Content-Type': 'application/json'}
        dest_url = self.job_config["dest_updates_url"]

        step_result = http_post_request(dest_url, headers, body_str)

        # Write updated items dest response body to a file
        write_to_file(self.job_config['dest_updated_items_response_body_filepath'], step_result.get_result())

        if step_result.was_successful():
            SendUpdatedItemsToDest.log_response(step_result.get_result())

        return step_result
예제 #2
0
    def __call__(self, start_time: str, args: argparse.Namespace) -> caia.core.command.CommandResult:
        # Create job configuration
        job_config = create_job_configuration(start_time)

        # Validate preconditions
        step_result = run_step(ValidateJobPreconditions(job_config))
        if not step_result.was_successful():
            return CommandResult(step_result.was_successful(), step_result.get_errors())

        # Query source URL
        step_result = run_step(QuerySourceUrl(job_config))
        write_to_file(job_config["source_response_body_filepath"], step_result.result)
        if not step_result.was_successful():
            return CommandResult(step_result.was_successful(), step_result.get_errors())

        # Diff against last success
        current_time = datetime.datetime.strptime(start_time, '%Y%m%d%H%M%S')
        step_result = run_step(DiffAgainstLastSuccess(job_config, current_time))
        if not step_result.was_successful():
            return CommandResult(step_result.was_successful(), step_result.get_errors())

        diff_result = step_result.get_result()

        # Write diff result to a file
        write_to_file(job_config['diff_result_filepath'], json.dumps(diff_result.as_dict()))

        if (len(diff_result.new_entries) == 0) and (len(diff_result.modified_entries) == 0):
            logger.info("No new or modified entries found, no CaiaSoft update required.")
            # No new entries, so nothing to send to CaiaSoft
            # Record job as successful
            step_result = run_step(UpdateLastSuccess(job_config))
            return CommandResult(step_result.was_successful(), step_result.get_errors())

        # Create POST body
        step_result = run_step(CreateDestRequest(job_config, diff_result))
        if not step_result.was_successful():
            return CommandResult(step_result.was_successful(), step_result.get_errors())

        # Write POST request body to file
        write_to_file(job_config['dest_request_body_filepath'], step_result.get_result())

        # Send POST data to destination
        step_result = run_step(SendToDest(job_config))

        if not step_result.was_successful():
            return CommandResult(step_result.was_successful(), step_result.get_errors())

        # Record denied keys (if any)
        dest_response_body = step_result.get_result()
        step_result = run_step(RecordDeniedKeys(job_config, dest_response_body, current_time,
                                                diff_result))

        if not step_result.was_successful():
            return CommandResult(step_result.was_successful(), step_result.get_errors())

        # Record job as successful
        step_result = run_step(UpdateLastSuccess(job_config))
        return CommandResult(step_result.was_successful(), step_result.get_errors())
예제 #3
0
    def __call__(self, start_time: str,
                 args: argparse.Namespace) -> caia.core.command.CommandResult:
        # Create job configuration
        job_config = create_job_configuration(start_time)

        # Validate preconditions
        step_result = run_step(ValidateJobPreconditions(job_config))
        if not step_result.was_successful():
            return CommandResult(step_result.was_successful(),
                                 step_result.get_errors())

        # Get the last timestamp
        step_result = run_step(GetLastTimestamp(job_config))
        if not step_result.was_successful():
            return CommandResult(step_result.was_successful(),
                                 step_result.get_errors())

        last_timestamp = step_result.get_result()
        end_time = None
        next_item = None
        iteration_count = 1

        while True:
            logger.info(f"---- Running iteration {iteration_count} ----")
            job_config.set_iteration(iteration_count)

            # Query source URL
            step_result = run_step(
                QuerySourceUrl(job_config, last_timestamp, end_time,
                               next_item))
            write_to_file(job_config["source_response_body_filepath"],
                          step_result.result)
            if not step_result.was_successful():
                return CommandResult(step_result.was_successful(),
                                     step_result.get_errors())

            source_response = step_result.get_result()

            # Parse source response
            step_result = run_step(ParseSourceResponse(source_response))
            if not step_result.was_successful():
                return CommandResult(step_result.was_successful(),
                                     step_result.get_errors())

            source_items = step_result.get_result()
            new_item_count = len(source_items.get_new_items())
            updated_item_count = len(source_items.get_updated_items())
            next_item = source_items.get_next_item()
            end_time = source_items.get_end_time()

            if new_item_count == 0:
                logger.info(
                    "No new entries found, skipping CaiaSoft new items request."
                )
            else:
                logger.info(
                    f"Sending {new_item_count} new item(s) to CaiaSoft.")
                # Create new items POST body
                step_result = run_step(CreateDestNewItemsRequest(source_items))
                if not step_result.was_successful():
                    return CommandResult(step_result.was_successful(),
                                         step_result.get_errors())

                # Write new items request body to file
                write_to_file(
                    job_config['dest_new_items_request_body_filepath'],
                    step_result.get_result())

                # Send POST new items data to destination
                step_result = run_step(SendNewItemsToDest(job_config))

                if not step_result.was_successful():
                    return CommandResult(step_result.was_successful(),
                                         step_result.get_errors())

            if updated_item_count == 0:
                logger.info(
                    "No updated entries found, skipping CaiaSoft updated items request."
                )
            else:
                logger.info(
                    f"Sending {updated_item_count} updated item(s) to CaiaSoft."
                )
                # Create updated items POST body
                step_result = run_step(
                    CreateDestUpdatedItemsRequest(source_items))
                if not step_result.was_successful():
                    return CommandResult(step_result.was_successful(),
                                         step_result.get_errors())

                # Write updated items request body to file
                write_to_file(
                    job_config['dest_updated_items_request_body_filepath'],
                    step_result.get_result())

                # Send POST updated items data to destination
                step_result = run_step(SendUpdatedItemsToDest(job_config))

                if not step_result.was_successful():
                    return CommandResult(step_result.was_successful(),
                                         step_result.get_errors())

                if not step_result.was_successful():
                    return CommandResult(step_result.was_successful(),
                                         step_result.get_errors())

            # Record job as successful
            step_result = run_step(UpdateLastSuccess(job_config))

            if next_item is None:
                return CommandResult(step_result.was_successful(),
                                     step_result.get_errors())
            else:
                logger.info(
                    f"next_item is '{next_item}'. Commencing next iteration.")
                iteration_count = iteration_count + 1