Skip to content

Add report download helper for Reporting v3 #5

Description

@lineoffligbot

Reporting currently stops at three generated methods:

create_async_report(configuration:, start_date:, end_date:, name: nil)
get_async_report(report_id)
delete_async_report(report_id)

Getting actual report data out of those is entirely on the caller: poll until the
status is terminal, pull the presigned S3 url out of the response, fetch it, and
gunzip it.

report = reporting.create_async_report(...).parse
until (status = reporting.get_async_report(report["reportId"]).parse)["status"] == "COMPLETED"
  sleep(30)
end
Zlib::GzipReader.new(URI.open(status["url"])).read

Note that :auto_inflate on the API client does not help here. The report is a
gzipped body fetched from S3, not a gzip content encoding on an API response.

Proposal

Follow the pattern Peddler uses: a hand-written helper module per API, mixed into
the generated class, so regeneration never clobbers it.

module AmazonAds
  module Helpers
    module Reporting
      def download_report(report_id_or_url)
        url = if report_id_or_url.start_with?("http")
          report_id_or_url
        else
          get_async_report(report_id_or_url).parse.fetch("url")
        end

        HTTP.use(:auto_inflate).get(url)
      end
    end
  end
end

The generator would include AmazonAds::Helpers::<ClassName> when
lib/amazon_ads/helpers/<file_name>.rb exists, matching
Generator::API#has_helper? in Peddler.

Open questions

  • Do we also ship a polling helper, or leave the wait loop to the caller? Peddler
    deliberately does not ship one. A blocking sleep loop in a client library is
    easy to get wrong (no jitter, no cap, unusable from a reactor), so leaving it out
    is defensible.
  • Confirm whether the S3 object carries Content-Encoding: gzip, which decides
    whether :auto_inflate suffices or we need an explicit Zlib::GzipReader.
    Peddler has an inconsistency here worth not copying: its Data Kiosk helper uses
    HTTP.use(:auto_inflate) while its Reports helper does not.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions