Skip to content

Commit a95cd56

Browse files
add set_token
1 parent a19d0bc commit a95cd56

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,22 @@ bot = Farmbot()
7171

7272
Use the same login credentials associated with the web app account you are interacting with. The server is "https://my.farm.bot" by default.
7373
```python
74-
bot.get_token("email", "password")
74+
token = bot.get_token("email", "password")
7575
```
7676

7777
To avoid storing your account credentials in plaintext, you can print your token:
7878
```python
79-
print(bot.state.token)
79+
print(token)
8080
```
8181

8282
and then delete the `bot.get_token("email", "password")` line and set your token directly instead:
8383
```python
84-
fb.state.token = {'token': {'unencoded': {'aud': ...
84+
token = {'token': {'unencoded': {'aud': ...
85+
bot.set_token(token)
8586
```
8687

88+
where the part after `token = ` is the entire token pasted from the `print(token)` output. The start should look similar to the value above.
89+
8790
> [!CAUTION]
8891
> Store your authorization token securely. It grants full access and control over your FarmBot and your FarmBot Web App account.
8992

@@ -156,6 +159,7 @@ sidecar-starter-pack/
156159
| class `ApiConnect()` | Description |
157160
| :--- | :--- |
158161
| `get_token()` | Get FarmBot authorization token. Server is "https://my.farm.bot" by default. |
162+
| `set_token()` | Set FarmBot authorization token. |
159163
| `check_token()` | Ensure the token persists throughout sidecar. |
160164
| `request_handling()` | Handle errors associated with different endpoint errors. |
161165
| `request()` | Make requests to API endpoints using different methods. |

main.py

+4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ def get_token(self, email, password, server="https://my.farm.bot"):
4343
"""Get FarmBot authorization token. Server is 'https://my.farm.bot' by default."""
4444
return self.api.get_token(email, password, server)
4545

46+
def set_token(self, token):
47+
"""Set FarmBot authorization token."""
48+
self.state.token = token
49+
4650
# basic_commands.py
4751

4852
def wait(self, duration):

tests/tests_main.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class TestFarmbot(unittest.TestCase):
2727
def setUp(self):
2828
'''Set up method called before each test case'''
2929
self.fb = Farmbot()
30-
self.fb.state.token = MOCK_TOKEN
30+
self.fb.set_token(MOCK_TOKEN)
3131
self.fb.set_verbosity(0)
3232
self.fb.state.test_env = True
3333
self.fb.state.broker_listen_duration = 0.3
@@ -41,7 +41,7 @@ def test_get_token_default_server(self, mock_post):
4141
mock_response.status_code = 200
4242
mock_response.text = 'text'
4343
mock_post.return_value = mock_response
44-
self.fb.state.token = None
44+
self.fb.set_token(None)
4545
# Call with default server
4646
self.fb.get_token('[email protected]', 'test_pass_123')
4747
mock_post.assert_called_once_with(
@@ -61,7 +61,7 @@ def test_get_token_custom_server(self, mock_post):
6161
mock_response.status_code = 200
6262
mock_response.text = 'text'
6363
mock_post.return_value = mock_response
64-
self.fb.state.token = None
64+
self.fb.set_token(None)
6565
# Call with custom server
6666
self.fb.get_token('[email protected]', 'test_pass_123',
6767
'https://staging.farm.bot')
@@ -82,7 +82,7 @@ def helper_get_token_errors(self, *args, **kwargs):
8282
mock_response = Mock()
8383
mock_response.status_code = status_code
8484
mock_post.return_value = mock_response
85-
self.fb.state.token = None
85+
self.fb.set_token(None)
8686
self.fb.get_token('[email protected]', 'test_pass_123')
8787
mock_post.assert_called_once_with(
8888
'https://my.farm.bot/api/tokens',
@@ -121,7 +121,7 @@ def helper_get_token_exceptions(self, *args, **kwargs):
121121
exception = kwargs['exception']
122122
error_msg = kwargs['error_msg']
123123
mock_post.side_effect = exception
124-
self.fb.state.token = None
124+
self.fb.set_token(None)
125125
self.fb.get_token('[email protected]', 'test_pass_123')
126126
mock_post.assert_called_once_with(
127127
'https://my.farm.bot/api/tokens',
@@ -288,7 +288,7 @@ def test_api_get_with_id(self, mock_request):
288288
@patch('requests.request')
289289
def test_check_token_api_request(self, mock_request):
290290
'''Test check_token: API request'''
291-
self.fb.state.token = None
291+
self.fb.set_token(None)
292292
with self.assertRaises(ValueError) as cm:
293293
self.fb.api_get('points')
294294
self.assertEqual(cm.exception.args[0], self.fb.state.NO_TOKEN_ERROR)
@@ -301,7 +301,7 @@ def test_check_token_broker(self, mock_request, mock_mqtt):
301301
'''Test check_token: broker'''
302302
mock_client = Mock()
303303
mock_mqtt.return_value = mock_client
304-
self.fb.state.token = None
304+
self.fb.set_token(None)
305305
with self.assertRaises(ValueError) as cm:
306306
self.fb.on(123)
307307
self.assertEqual(cm.exception.args[0], self.fb.state.NO_TOKEN_ERROR)
@@ -1774,6 +1774,3 @@ def test_print_status(self, mock_print):
17741774
call_strings = [s.split('(')[0].strip('`') for s in call_strings]
17751775
self.assertIn('[\n "testing"\n]', call_strings)
17761776
self.assertIn('test_print_status', call_strings)
1777-
1778-
if __name__ == '__main__':
1779-
unittest.main()

0 commit comments

Comments
 (0)