Différences

Cette page vous donne les différences entre la révision choisie et la version actuelle de la page.

hub_principal:exemple_api_python [2018/04/23 11:22] (version actuelle)
Ligne 1: Ligne 1:
 +====== Exemple d'utilisation de l'api REST dans un script Python. ======
 +
 +<note warning>Attention se script n'est qu'un exemple non exhaustif d'utilisation de l'API REST en python, il n'est en aucun cas à utiliser tel quel en Production.</note>
 +
 +
 +
 +<code python>
 +# -*- coding: utf-8 -*-
 +#    This program is free software: you can redistribute it and/or modify
 +#    it under the terms of the GNU Lesser General Public License (LGPL) as
 +#    published by the Free Software Foundation, either version 2 of the License,
 +#    or (at your option) any later version.
 +#
 +#    This program is distributed in the hope that it will be useful,
 +#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 +#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 +#    GNU Lesser General Public License for more details.
 +#
 +#    See <http://www.gnu.org/licenses/lgpl.html/>.
 +#    (c) Accelya 2010
 + 
 +import pprint,urllib,httplib, simplejson,base64
 + 
 +#Convert a dict to http query args
 +def prepare_request(request,prefix=''):
 + request_dict={}
 + if (type(request) == dict):
 + for key,value in request.items():
 + if (prefix == '') :
 + request_dict.update(prepare_request(value,key))
 + else :
 + request_dict.update(prepare_request(value,"%s.%s" % (prefix,key)))
 + elif (type(request) == list ):
 + for j in range(0,len(request)):
 + request_dict.update(prepare_request(request[j],"%s[%i]" % (prefix,j)))
 + else :
 + request_dict[prefix] = request
 + 
 + if(prefix == '') :
 + return urllib.urlencode(request_dict)
 + else :
 + return request_dict
 + 
 +#Call a REST method
 +#Here you have to define your gln and password
 +def call_service(method,request):
 +  glnReseller='1111111111111'
 +  passwordReseller='password'
 +  conn = httplib.HTTPSConnection("hub-dilicom.centprod.com")
 +  url = "/v1/hub-numerique-api/json/"+method+"?"+prepare_request(request)
 +  print "URL : %s" % (url)
 +  base64string = base64.encodestring('%s:%s' % (glnReseller,passwordReseller))[:-1]
 +  conn.request("GET", url,headers={"Authorization":"Basic %s" % base64string})
 +  resp = conn.getresponse()
 +  if(resp.status != 200):
 + print "ERROR request return %i : %s" % (resp.status,resp.reason)
 +  data = resp.read()
 +  return simplejson.loads(data)
 + 
 + 
 + 
 +#SendOrder Example
 +order_req = {
 + 'orderId':'3PC200148',
 + 'glnGroup':'6235482301523',
 + 'customerId':'test',
 + 'finalBookOwner' : {
 +  'identifier':'test',
 +  'civility':'M',
 +  'firstName':'John',
 +  'lastName':'Doe',
 +  'email':'john@doe.com',
 +          'country':'France',
 +          'city':'Paris',
 +          'postalCode':'75001'
 + },
 + 'orderRequestLines': [{
 +  'ean13':'9782814500136',
 +  'glnDistributor':'3015594118311',
 +  'unitPrice':'1',
 +  'quantity':'1',
 +  'lineReference':'01'
 + },{
 +  'ean13':'9990000000001',
 +  'glnDistributor':'3012410002007',
 +  ':unitPrice':'1',
 +  'quantity':'1',
 +  'lineReference':'02'
 + }]
 +}
 + 
 +print "\nSendorder"
 +pprint.pprint(call_service("sendOrder",order_req), depth=3)
 + 
 + 
 + 
 +#Exemple de recuperation des notices depuis une date fournie
 +since_req = {
 + 'sinceDate':'2010-08-20T00:00:00'
 +}
 + 
 +print "\nGetNotices (since)"
 +pprint.pprint(call_service("getNotices",since_req), depth=3)
 + 
 + 
 + 
 +#Exemple de recuperation des notices depuis la derniere connection
 +last_req = {
 + 'lastConnection':''
 +}
 +print "\nGetNotices (lastConnection)"
 +pprint.pprint(call_service("getNotices",last_req), depth=3)
 + 
 + 
 + 
 + 
 +#Exemple de recuperation de toutes les notices
 +full_req = {
 + 'initialization':''
 +}
 +print "\nGetNotices (initialization)"
 +pprint.pprint(call_service("getNotices",full_req), depth=3)
 + 
 + 
 + 
 +#Exemple de verification de disponibilité
 +check_req = {
 +    'checkAvailabilityLines' : [{
 +  'ean13':'9782814500136',
 +  'glnDistributor':'3015594118311',
 +  'unitPrice':'1'
 + },{
 +  'ean13':'9782814500136',
 +  'glnDistributor':'3015594125312',
 +  'unitPrice':'1'
 + }]
 +}
 +print "\nCheckAvailability (initialization)"
 +pprint.pprint(call_service("checkAvailability",check_req), depth=3)
 +
 +
 +</code>