service.py 1.5 KB
Newer Older
Shoulin Wei's avatar
init  
Shoulin Wei committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import grpc
import random

class _Service:
    def __init__(self, data):
        self._data = data

    @property
    def name(self):
        raise NotImplementedError()

    @property
    def version(self):
        raise NotImplementedError()

    @property
    def metadata(self):
        raise NotImplementedError()

    @property
    def id(self):
        raise NotImplementedError()

    @property
    def address(self):
        raise NotImplementedError()

    @property
    def port(self):
        raise NotImplementedError()

    @property
    def node_metadata(self):
        raise NotImplementedError()

    def __str__(self):
        return '{}:{}'.format(self.name, self.address)
        
class Service(_Service):

    def __init__(self, data):
        self._data = data

    @property
    def name(self):
        return self._data['name']

    @property
    def version(self):
        return self._data['version']

    @property
    def metadata(self):
        return self._data['metadata']

    @property
    def _node(self):
        return random.choice(self._data['nodes'])

    @property
    def id(self):
        return self._node['id']

    @property
    def address(self):
        return self._node['address']

    @property
    def node_metadata(self):
        return self._node['metadata']


class ServiceProxy:
    def __init__(self, gatewayCfg = None):
        self.gatewayCfg = gatewayCfg

    def insecure(self, stubCls):
        channel = grpc.insecure_channel(self.gatewayCfg.url)
        return stubCls(channel)