· 5 years ago · Sep 19, 2020, 04:30 PM
1# PETICIÓ GET
2 # load inventories if api enabled for this box
3 if _box.box_type.api_enabled == 1:
4 _count = _box.apis.filter(fiscal_id=_complaint.fiscal_id).count()
5
6 if _count != 0:
7 try:
8 _api_customer = _box.apis.filter(fiscal_id=_complaint.fiscal_id)
9 _api_customer = _api_customer[0]
10 _url = _api_customer.api_instance.url
11
12 # connect API
13 client = views_api.connect(request, _api_customer.api_instance)
14
15 _companies = client.get(_url + '/company/')
16
17 _company = []
18
19 for _c in _companies:
20 if _c['nif'] == _complaint.fiscal_id:
21 _company = _c
22
23 _api_inventories = _company['inventory']
24 _inventories = []
25
26 for _api_url in _api_inventories:
27 _inventory = client.get(str(_api_url))
28 _inventories.append(_inventory)
29
30 except Exception:
31 _inventories = []
32 else:
33 _inventories = []
34
35
36# CONTINGUT views_api.connect()
37@login_required
38def connect(request, api_instance):
39 auth = coreapi.auth.BasicAuthentication(
40 username=api_instance.username,
41 password=api_instance.password,
42 )
43 client = coreapi.Client(auth=auth)
44 return client
45
46###################################################################################################
47
48# PETICIÓ POST
49# connect to API and send InventoryChange
50 if _box.box_type.api_enabled == 1:
51 # connect API
52 _api_customer = _box.apis.filter(fiscal_id=_complaint.fiscal_id)
53 _api_customer = _api_customer[0]
54 _url = _api_customer.api_instance.url
55 client = views_api.connect(request, _api_customer.api_instance)
56
57 _inventory = int(post['inventory'])
58 _api_status = post['api_status']
59
60 _schema = client.get(_url + '/inventorychange/')
61
62 # a aquesta línia és a on dubto què va a key ['Inventory Change List', 'POST'], ni flapa de què he de posar a Inventory Change List o al POST, al post he posat create, params interpreto que estic passant les variables POST.
63 client.action(_schema, ['Inventory Change List', 'POST'], params={"inventory": _inventory, "name": "temporal", "text": "temporal text", "status": _api_status})
64
65
66# DEBUGGEJANT I VEIENT QUÈ TÉ la classe Client com a variable client
67(Pdb) client._decoders
68List([<coreapi.codecs.corejson.CoreJSONCodec object at 0x7f12c52f77f0>, <coreapi.codecs.jsondata.JSONCodec object at 0x7f12c52c2470>, <coreapi.codecs.text.TextCodec object at 0x7f12c52c2e48>, <coreapi.codecs.download.DownloadCodec object at 0x7f12c52c27f0>])
69(Pdb) client._transports
70List([<coreapi.transports.http.HTTPTransport object at 0x7f12c52c2a20>])
71(Pdb) dir(client)
72['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_decoders', '_transports', 'action', 'decoders', 'get', 'reload', 'transports']
73(Pdb) client.decoders
74List([<coreapi.codecs.corejson.CoreJSONCodec object at 0x7f12c52f77f0>, <coreapi.codecs.jsondata.JSONCodec object at 0x7f12c52c2470>, <coreapi.codecs.text.TextCodec object at 0x7f12c52c2e48>, <coreapi.codecs.download.DownloadCodec object at 0x7f12c52c27f0>])
75(Pdb) client.__dict__
76{'_decoders': List([<coreapi.codecs.corejson.CoreJSONCodec object at 0x7f12c52f77f0>, <coreapi.codecs.jsondata.JSONCodec object at 0x7f12c52c2470>, <coreapi.codecs.text.TextCodec object at 0x7f12c52c2e48>, <coreapi.codecs.download.DownloadCodec object at 0x7f12c52c27f0>]), '_transports': List([<coreapi.transports.http.HTTPTransport object at 0x7f12c52c2a20>])}
77
78# Documentació
79https://core-api.github.io/python-client/api-guide/client/
80https://github.com/core-api/python-client
81https://www.coreapi.org/
82
83
84