Created
April 23, 2019 23:13
-
-
Save faustobdls/b2a4eee5a3b4fcb5194ec1aa3635df64 to your computer and use it in GitHub Desktop.
Olgflix python integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
class Gateway: | |
""" | |
Oldflix Integration | |
Christian David <christian@cdavid.eti.br> | |
""" | |
def __init__(self, **kwargs): | |
self.API_URL = kwargs.get('url','https://reseller.oldflix.com.br/api/') | |
self.username = kwargs.get('username') | |
self.password = kwargs.get('password') | |
self.TOKEN = None | |
""" | |
Login on API to retrieve a token | |
""" | |
def login(self): | |
d = { | |
'username': str(self.username), | |
'password': str(self.password) | |
} | |
try: | |
r = requests.post('%s%s'%(self.API_URL,'token'),data=d) | |
req = r.json() | |
self.TOKEN = req['token'] | |
return True | |
except: | |
return False | |
""" | |
if empty return all users | |
if has a email, will return a user information by email | |
kwargs: | |
* email - string | |
""" | |
def users_list(self,**kwargs): | |
if self.TOKEN is None: | |
self.login() | |
h = { 'Authorization': 'Bearer %s' %(self.TOKEN) } | |
if not kwargs.get('email'): | |
URL = '%s%s'%(self.API_URL,'reseller/find/') | |
else: | |
URL = '%s%s%s'%(self.API_URL,'reseller/find/',kwargs.get('email')) | |
try: | |
r = requests.get(URL,headers=h) | |
req = r.json() | |
return req | |
except: | |
return False | |
""" | |
Create a user and return all users registered | |
-required: | |
* name: First name from client | |
* surname: Second name from client | |
* mail: E-Mail from client | |
* password: Password from client | |
""" | |
def create_user(self,**kwargs): | |
if self.TOKEN is None: | |
self.login() | |
h = { 'Authorization': 'Bearer %s' %(self.TOKEN), | |
'Content-Type': 'application/json'} | |
print(h) | |
if all (k in kwargs for k in ("name","surname","mail","password")): | |
urlr = '%s%s' %(self.API_URL,'reseller/create') | |
print(urlr) | |
r = requests.post(urlr,headers=h,data=kwargs) | |
print(r.text) | |
#resp = r.json() | |
print(r) | |
#print(resp) | |
return r | |
else: | |
return False | |
""" | |
Edit a previously client status | |
-required: | |
* ID (generated by Oldflix) | |
* action | |
- renew: Reactivate the previously registered client by ID | |
- cancel: Deactivate the previously registered client by ID | |
""" | |
def edit_user(self,**kwargs): | |
if self.TOKEN is None: | |
self.login() | |
h = { 'Authorization': 'Bearer %s' %(self.TOKEN) } | |
if kwargs.get('id'): | |
if kwargs.get('action') == 'renew': | |
r = requests.post('%s%s%s'%(self.API_URL,'reseller/renew/',kwargs.get('id')),headers=h) | |
elif kwargs.get('action') == 'cancel': | |
r = requests.delete('%s%s%s'%(self.API_URL,'reseller/cancel/',kwargs.get('id')),headers=h) | |
return r.json() | |
else: | |
return False | |
g=Gateway(**{'username':'noc@vivatelecom.com.br','password':'h3gxnk5d'}) | |
print g.login() | |
print g.create_user(id='1000',name='thiago',surname='montenegro',phone='84981381274',mail='thiago@sgp.net.br',password='Vivasempre123') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment