#!/usr/bin/env ruby
# frozen_string_literal: true

require 'rest-client'
require 'aws-sdk-s3'

Aws.config.update region: 'us-west-2'

if ENV['AWS_TOKEN']
  AWS_TOKEN = JSON.parse(ENV['AWS_TOKEN'])
  Aws.config.update credentials: Aws::Credentials.new(
    AWS_TOKEN['Credentials']['AccessKeyId'],
    AWS_TOKEN['Credentials']['SecretAccessKey'],
    AWS_TOKEN['Credentials']['SessionToken']
  )
else
  Aws.config.update profile: 'cpe-tools'
end

s3_client = Aws::S3::Resource.new.bucket('tracer-reports-default-uswest2-4698a0419732487b2adb98304a68a7dc')

(1..Time.now.month).each do |month|
  response = RestClient.get "https://latency-api.clients.internal.justin.tv/reports/2018/m#{month}", content_type: :json, accept: :json
  s3_client.put_object body: response.body, key: "m#{month}.json", acl: 'authenticated-read', content_type: 'application/json', cache_control: 'private' if response.code == 200
end


