1# -*- coding: utf-8 -*-
2# Copyright 2014 Google Inc. All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15"""Helper functions for Cloud API implementations."""
16
17from __future__ import absolute_import
18
19import json
20
21from gslib.cloud_api import ArgumentException
22
23
24def ValidateDstObjectMetadata(dst_obj_metadata):
25  """Ensures dst_obj_metadata supplies the needed fields for copy and insert.
26
27  Args:
28    dst_obj_metadata: Metadata to validate.
29
30  Raises:
31    ArgumentException if metadata is invalid.
32  """
33  if not dst_obj_metadata:
34    raise ArgumentException(
35        'No object metadata supplied for destination object.')
36  if not dst_obj_metadata.name:
37    raise ArgumentException(
38        'Object metadata supplied for destination object had no object name.')
39  if not dst_obj_metadata.bucket:
40    raise ArgumentException(
41        'Object metadata supplied for destination object had no bucket name.')
42
43
44def GetDownloadSerializationData(src_obj_metadata, progress=0):
45  """Returns download serialization data.
46
47  There are four entries:
48    auto_transfer: JSON-specific field, always False.
49    progress: How much of the download has already been completed.
50    total_size: Total object size.
51    url: Implementation-specific field used for saving a metadata get call.
52         For JSON, this the download URL of the object.
53         For XML, this is a pickled boto key.
54
55  Args:
56    src_obj_metadata: Object to be downloaded.
57    progress: See above.
58
59  Returns:
60    Serialization data for use with Cloud API GetObjectMedia.
61  """
62
63  serialization_dict = {
64      'auto_transfer': 'False',
65      'progress': progress,
66      'total_size': src_obj_metadata.size,
67      'url': src_obj_metadata.mediaLink
68  }
69
70  return json.dumps(serialization_dict)
71