Changeset - 16160deb284a
[Not reviewed]
default
0 2 1
Dennis Fink - 10 years ago 2015-07-06 14:33:16

Added rudimentary test client, and fixed state setting
3 files changed with 50 insertions and 11 deletions:
0 comments (0 inline, 0 general)
client.py
Show inline comments
 
new file 100644
 
import requests
 
import json
 
import time
 

	
 
from requests.auth import HTTPDigestAuth
 

	
 
base_url = 'http://localhost:5000'
 
auth = HTTPDigestAuth('test', 'test')
 

	
 

	
 
print("Testing setting state to closed")
 
payload = {"value": False}
 
r = requests.post(base_url + '/state/set/open',
 
                  auth=auth,
 
                  data=json.dumps(payload))
 
print(r.text)
 

	
 
time.sleep(1)
 

	
 
print("Testing setting message")
 
payload = {"value": "Test message"}
 
auth = HTTPDigestAuth('test', 'test')
 

	
 
r = requests.post(base_url + '/state/set/message', auth=auth,
 
                  data=json.dumps(payload))
 
print(r.text)
 
time.sleep(1)
 

	
 
print("Testing setting trigger person")
 
payload = {"value": "metalgamer"}
 
auth = HTTPDigestAuth('test', 'test')
 

	
 
r = requests.post(base_url + '/state/set/trigger_person',
 
                  auth=auth,
 
                  data=json.dumps(payload))
 
print(r.text)
 
time.sleep(1)
spaceapi/auth.py
Show inline comments
 
from flask import current_app
 
from flask.ext.httpauth import HTTPDigestAuth
 
 
httpauth = HTTPDigestAuth()
 
 
@httpauth.get_password
 
def get_pw(username):
 
    if username == app.config.get('HTTP_DIGEST_AUTH_USER'):
 
        return app.config.get('HTTP_DIGEST_AUTH_PASSWORD')
 
    if username == current_app.config.get('HTTP_DIGEST_AUTH_USER'):
 
        return current_app.config.get('HTTP_DIGEST_AUTH_PASSWORD')
 
    return None
spaceapi/state.py
Show inline comments
 
import json
 

	
 
from time import time
 

	
 
from flask import Blueprint, jsonify, request
 
from flask import Blueprint, jsonify, request, current_app
 

	
 
from .active import active_json, save_last_state
 
from .auth import httpauth
 
@@ -18,18 +20,17 @@ ALLOWED_STATE_KEYS = {
 
@httpauth.login_required
 
def set_state(key):
 

	
 
    value = request.form['value']
 
    value = literal_eval(value)
 
    value = json.loads(request.data.decode('utf-8'))['value']
 
    current_app.logger.info(value)
 
    current_app.logger.info(type(value))
 

	
 
    if key in ALLOWED_STATE_KEYS and isinstance(value,
 
                                                ALLOWED_STATE_KEYS[key]):
 
    if key in ALLOWED_STATE_KEYS:
 
        active_json['state'][key] = value
 

	
 
        if key == 'open':
 
            active_json['state']['lastchange'] = int(time())
 

	
 
        save_last_state()
 
        return jsonify(active_json)
 
    else:
 
        return 400
 

	
 
    save_last_state()
 

	
 
    return jsonify(active_json)
0 comments (0 inline, 0 general)