1# Copyright 2021 - The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""A client that talks to EngProd APIs."""
16
17import json
18import subprocess
19
20from urllib.parse import urljoin
21
22
23class EngProdClient():
24    """Client that manages EngProd api."""
25
26    @staticmethod
27    def LeaseDevice(build_target, build_id, api_key, api_url):
28        """Lease one cuttlefish device.
29
30        Args:
31            build_target: Target name, e.g. "aosp_cf_x86_phone-userdebug"
32            build_id: Build ID, a string, e.g. "2263051", "P2804227"
33            api_key: String of api key.
34            api_url: String of api url.
35
36        Returns:
37            The response of curl command.
38        """
39        request_data = "{\"target\": \"%s\", \"build_id\": \"%s\"}" % (
40            build_target, build_id)
41        lease_url = urljoin(api_url, "lease?key=%s" % api_key)
42        response = subprocess.check_output([
43            "curl", "--request", "POST", lease_url, "-H",
44            "Accept: application/json", "-H", "Content-Type: application/json",
45            "-d", request_data
46        ])
47        return json.loads(response)
48