# Copyright (c) 2020 CloudZero - ALL RIGHTS RESERVED - PROPRIETARY AND CONFIDENTIAL
# Unauthorized copying of this file and/or project, via any medium is strictly prohibited.
# Direct all questions to legal@cloudzero.com
from typing import Any

import boto3

from czc import json

s3_client = boto3.client('s3')

DEFAULT_URL_EXPIRATION = 900


def get_presigned_url(bucket: str, key: str, expires_in: int = DEFAULT_URL_EXPIRATION) -> str:
    return s3_client.generate_presigned_url(
        'get_object',
        Params={
            'Bucket': bucket,
            'Key': key,
        },
        ExpiresIn=expires_in,
        HttpMethod='GET',
    )


def get_json_object(bucket: str, key: str) -> Any:
    response = s3_client.get_object(Bucket=bucket, Key=key)
    data = response['Body']
    return json.loads(data.read())


def put_json_object(bucket: str, key: str, data: Any) -> None:
    body = json.dumps(data).encode()
    s3_client.put_object(Bucket=bucket, Key=key, Body=body)
