#!/usr/bin/env python # -*- coding: utf-8 -*- from configparser import ConfigParser # SelfConfigParsed has been renamed to ConfigParser in Python 3.2 (https://github.com/python/cpython/blob/cd90f6a3692e0f7ef0a13aae651e19a08d1f9b31/Lib/configparser.py#L1230) from ssl_helper import SSLAdapter from urllib.parse import urlencode # urlencode is now inside urlib.parse (https://stackoverflow.com/questions/44031471/importerror-cannot-import-name-urlencode-when-trying-to-install-flask-ext-sto) import json import os import re import requests import ssl class Meli(object): def __init__(self, client_id, client_secret, access_token=None, refresh_token=None): self.client_id = client_id self.client_secret = client_secret self.access_token = access_token self.refresh_token = refresh_token self.expires_in = None parser = ConfigParser() parser.read(os.path.dirname(os.path.abspath(__file__))+'/config.ini') self._requests = requests.Session() try: self.SSL_VERSION = parser.get('config', 'ssl_version') self._requests.mount('https://', SSLAdapter(ssl_version=getattr(ssl, self.SSL_VERSION))) except: self._requests = requests self.API_ROOT_URL = parser.get('config', 'api_root_url') self.SDK_VERSION = parser.get('config', 'sdk_version') self.AUTH_URL = parser.get('config', 'auth_url') self.OAUTH_URL = parser.get('config', 'oauth_url') #AUTH METHODS def auth_url(self,redirect_URI): params = {'client_id':self.client_id,'response_type':'code','redirect_uri':redirect_URI} url = self.AUTH_URL + '/authorization' + '?' + urlencode(params) return url def authorize(self, code, redirect_URI): params = { 'grant_type' : 'authorization_code', 'client_id' : self.client_id, 'client_secret' : self.client_secret, 'code' : code, 'redirect_uri' : redirect_URI} headers = {'Accept': 'application/json', 'User-Agent':self.SDK_VERSION, 'Content-type':'application/json'} uri = self.make_path(self.OAUTH_URL) response = self._requests.post(uri, params=urlencode(params), headers=headers) if response.ok: response_info = response.json() self.access_token = response_info['access_token'] if 'refresh_token' in response_info: self.refresh_token = response_info['refresh_token'] else: self.refresh_token = '' # offline_access not set up self.expires_in = response_info['expires_in'] return self.access_token else: # response code isn't a 200; raise an exception response.raise_for_status() def get_refresh_token(self): if self.refresh_token: params = {'grant_type' : 'refresh_token', 'client_id' : self.client_id, 'client_secret' : self.client_secret, 'refresh_token' : self.refresh_token} headers = {'Accept': 'application/json', 'User-Agent':self.SDK_VERSION, 'Content-type':'application/json'} uri = self.make_path(self.OAUTH_URL) response = self._requests.post(uri, params=urlencode(params), headers=headers, data=params) if response.ok: response_info = response.json() self.access_token = response_info['access_token'] self.refresh_token = response_info['refresh_token'] self.expires_in = response_info['expires_in'] return self.access_token else: # response code isn't a 200; raise an exception response.raise_for_status() else: raise Exception("Offline-Access is not allowed.") # REQUEST METHODS def get(self, path, params=None, extra_headers=None): params = params or {} headers = {'Accept': 'application/json', 'User-Agent':self.SDK_VERSION, 'Content-type':'application/json'} if extra_headers: headers.update(extra_headers) uri = self.make_path(path) response = self._requests.get(uri, params=urlencode(params), headers=headers) return response def post(self, path, body=None, params=None, extra_headers=None): params = params or {} headers = {'Accept': 'application/json', 'User-Agent':self.SDK_VERSION, 'Content-type':'application/json'} if extra_headers: headers.update(extra_headers) uri = self.make_path(path) if body: body = json.dumps(body) response = self._requests.post(uri, data=body, params=urlencode(params), headers=headers) return response def put(self, path, body=None, params=None, extra_headers=None): params = params or {} headers = {'Accept': 'application/json', 'User-Agent':self.SDK_VERSION, 'Content-type':'application/json'} if extra_headers: headers.update(extra_headers) uri = self.make_path(path) if body: body = json.dumps(body) response = self._requests.put(uri, data=body, params=urlencode(params), headers=headers) return response def delete(self, path, params=None, extra_headers=None): params = params or {} headers = {'Accept': 'application/json', 'User-Agent':self.SDK_VERSION, 'Content-type':'application/json'} if extra_headers: headers.update(extra_headers) uri = self.make_path(path) response = self._requests.delete(uri, params=params, headers=headers) return response def options(self, path, params=None, extra_headers=None): params = params or {} headers = {'Accept': 'application/json', 'User-Agent':self.SDK_VERSION, 'Content-type':'application/json'} if extra_headers: headers.update(extra_headers) uri = self.make_path(path) response = self._requests.options(uri, params=urlencode(params), headers=headers) return response def make_path(self, path, params=None): params = params or {} # Making Path and add a leading / if not exist if not (re.search("^\/", path)): path = "/" + path path = self.API_ROOT_URL + path if params: path = path + "?" + urlencode(params) return path