Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

from beyonic.api_client import ApiClient 

from beyonic.resources import GenericObject 

from beyonic.errors import BeyonicError 

 

 

class AbstractAPI(GenericObject): 

    """ 

    AbtractApi class, all the other api class extends it 

    """ 

    @classmethod 

    def get_client(cls, client=None): 

        url = cls.get_url() 

        from beyonic import api_key, verify_ssl_certs, api_version 

 

        if client: 

            return ApiClient(api_key=api_key, url=url, client=client, verify_ssl_certs=verify_ssl_certs, 

                             api_version=api_version) 

        else: 

            return ApiClient(api_key=api_key, url=url, client=None, verify_ssl_certs=verify_ssl_certs, 

                             api_version=api_version) 

 

    @classmethod 

    def get_url(cls): 

        from beyonic import api_endpoint_base, DEFAULT_ENDPOINT_BASE 

 

        if not api_endpoint_base: 

            api_endpoint_base = DEFAULT_ENDPOINT_BASE 

 

        if not api_endpoint_base.endswith("/"): 

            api_endpoint_base = api_endpoint_base + "/" 

        cls_name = api_endpoint_base + str(cls._method_path) 

        return cls_name 

 

    @classmethod 

    def list(cls, client=None, **kwargs): 

        """ 

        This will return list of resources. 

        """ 

        objs = cls.get_client(client).get(**kwargs) 

 

        #setting client object for each of the return object so that it can be used while saving data 

        for obj in objs.results: 

            if obj.id: 

                base = cls.get_url() 

                url = "%s/%s" % (base, obj.id) 

                api_client = cls.get_client(client) 

                api_client.set_url(url) 

                obj.set_client(api_client) 

 

        return objs 

 

    @classmethod 

    def create(cls, client=None, **kwargs): 

        """ 

        This will create new object 

        """ 

        return cls.get_client(client).post(**kwargs) 

 

    @classmethod 

    def get(cls, id, client=None, **kwargs): 

        """ 

        This will return the single object 

        """ 

        if not id: 

            raise BeyonicError('Invalid ID or ID hasn\'t been specified') 

 

        base = cls.get_url() 

        url = "%s/%s" % (base, id) 

        api_client = cls.get_client(client) 

        api_client.set_url(url) 

        obj = api_client.get(**kwargs) 

        obj.set_client(api_client) 

        return obj 

 

    @classmethod 

    def update(cls, id, client=None, **kwargs): 

        """ 

        This will update the object 

        """ 

        if not id: 

            raise BeyonicError('Invalid ID or ID hasn\'t been specified') 

        base = cls.get_url() 

        url = "%s/%s" % (base, id) 

        api_client = cls.get_client(client) 

        api_client.set_url(url) 

        return api_client.patch(**kwargs) 

 

    @classmethod 

    def delete(cls, id, client=None, **kwargs): 

        """ 

        This will return the single object 

        """ 

        if not id: 

            raise BeyonicError('Invalid ID or ID hasn\'t been specified') 

 

        base = cls.get_url() 

        url = "%s/%s" % (base, id) 

        api_client = cls.get_client(client) 

        api_client.set_url(url) 

        return api_client.delete(**kwargs)