'getting this error 'AttributeError: 'str' object has no attribute 'get_balance_f' on my rest api
Im making a rest api so I can communicate with my python script that does requests to a website and gets the data from the website.
class Classroom():
def __init__(self, numbercard, password) -> None:
self.session = requests.Session()
self.school = 400786
self.numbercard = numbercard
self.password = password
self.logged_in = False
def login(self):
payload = {
'modo': 'manual',
'escola': '400786',
'nrcartao': self.numbercard,
'codigo': self.password,
}
res = self.session.post('http://giae.esars.pt/cgi-bin/webgiae2.exe/loginv2',
json=payload)
self.logged_in = res.ok
def get_balance_f(self):
balance_request = self.session.get('http://giae.esars.pt/cgi-
bin/webgiae2.exe/saldo')
balance_json = balance_request.json()
self.balance = balance_json['saldo']
print(balance_json['saldo'])
this is one part of my python scrip that logins into the website and gets how much money i have in my school card.
from flask import Flask, request
from classroom import Classroom
from random import choice
chars = [
'q',
'w',
'e',
'r',
't',
'y',
'u',
'i',
'o',
'p',
'a',
's',
'd',
'f',
'g',
'h',
'j',
'k',
]
app = Flask(__name__)
def generate_id() -> str:
return ''.join(choice(chars) for _ in range(8))
@app.route('/login', methods = ['POST', 'GET'])
def login_receive():
card_number = request.json['username']
password = request.json['password']
user = Classroom(card_number, password)
user.login()
if user.logged_in:
uid = generate_id()
return {'message': 'Login succesful', 'uid':uid}
else:
return {'message': 'Login failed'}
@app.route('/classroom/myinfo', methods=['GET', 'POST'])
def myinfo():
user = request.headers['uid']
return user.get_balance_f()
on this code the login part is working, but when i tried to send a get request i get this error 'AttributeError: 'str' object has no attribute 'get_balance_f', and i dont get it.
Solution 1:[1]
On the myinfo() function you define the variable user as the uid header's value. Then, you call the get_balance_f() method on that user variable, which is most likely just a string — there is no function defined on the default python string object with that name.
You declared the user variable inside your login_receive() function as user = Classroom(card_number, password). This would work, but, as you're defining the variable inside that function, its' scope is limited to that function only. I see you tried to indent the myinfo() function to supposedly inherit the scope from the login_receive() function, but that won't work as you're defining different routes.
Maybe try instantiating your Classroom class as a global variable (outside a function) and then call its' methods inside the functions, as it has global scope.
[...]
my_classroom = Classroom()
@app.route('/login', methods = ['POST', 'GET'])
def login_receive():
card_number = request.json['username']
password = request.json['password']
user = my_classroom(card_number, password)
user.login()
if user.logged_in:
uid = generate_id()
return {'message': 'Login succesful', 'uid':uid}
else:
return {'message': 'Login failed'}
@app.route('/classroom/myinfo', methods=['GET', 'POST'])
def myinfo():
user = request.headers['uid']
return my_classroom.get_balance_f()
(with this indentation)
Also, try not to use such a simple token generation method for session purposes. Even if you're just using it in a private environment, it's always worth it to implement proper security in your apps.
PS: Pretty nice that you're messing around with GIAE's API! There's a lot of information on there which you can use for automating stuff and creating nice apps to improve your school experience. If you need any more help with GIAE's API just let me know!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
