1#!/usr/bin/env python3
2#
3#   Copyright 2018 - The Android Open Source Project
4#
5#   Licensed under the Apache License, Version 2.0 (the "License");
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an "AS IS" BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16
17import collections
18import json
19import logging
20import math
21import os
22import random
23import re
24import requests
25import socket
26import time
27
28
29class BaseLib():
30    def __init__(self, addr, tc, client_id):
31        self.address = addr
32        self.test_counter = tc
33        self.client_id = client_id
34
35    def build_id(self, test_id):
36        """Concatenates client_id and test_id to form a command_id.
37
38        Args:
39            test_id: string, unique identifier of test command.
40        """
41        return self.client_id + "." + str(test_id)
42
43    def send_command(self, test_id, test_cmd, test_args):
44        """Builds and sends a JSON command to SL4F server.
45
46        Args:
47            test_id: string, unique identifier of test command.
48            test_cmd: string, sl4f method name of command.
49            test_args: dictionary, arguments required to execute test_cmd.
50
51        Returns:
52            Dictionary, Result of sl4f command executed.
53        """
54        test_data = json.dumps({
55            "jsonrpc": "2.0",
56            "id": test_id,
57            "method": test_cmd,
58            "params": test_args
59        })
60        return requests.get(url=self.address, data=test_data).json()
61