33For example internet radios from: Medion, Hama, Auna, ...
44"""
55import requests
6+ import logging
7+ import traceback
68from lxml import objectify
79
810
911class FSAPI (object ):
1012
13+ DEFAULT_TIMEOUT_IN_SECONDS = 1
14+
1115 PLAY_STATES = {
1216 0 : 'stopped' ,
1317 1 : 'unknown' ,
1418 2 : 'playing' ,
1519 3 : 'paused' ,
1620 }
1721
18- def __init__ (self , fsapi_device_url , pin ):
22+ def __init__ (self , fsapi_device_url , pin , timeout = DEFAULT_TIMEOUT_IN_SECONDS ):
1923 self .pin = pin
2024 self .sid = None
2125 self .webfsapi = None
2226 self .fsapi_device_url = fsapi_device_url
27+ self .timeout = timeout
2328
2429 self .webfsapi = self .get_fsapi_endpoint ()
2530 self .sid = self .create_session ()
2631
2732 def get_fsapi_endpoint (self ):
28- endpoint = requests .get (self .fsapi_device_url )
33+ endpoint = requests .get (self .fsapi_device_url , timeout = self . timeout )
2934 doc = objectify .fromstring (endpoint .content )
3035 return doc .webfsapi .text
3136
@@ -34,24 +39,30 @@ def create_session(self):
3439 return doc .sessionId .text
3540
3641 def call (self , path , extra = None ):
37- if not self .webfsapi :
38- raise Exception ('No server found' )
42+ """Execute a frontier silicon API call."""
43+ try :
44+ if not self .webfsapi :
45+ raise Exception ('No server found' )
3946
40- if type (extra ) is not dict :
41- extra = dict ()
47+ if type (extra ) is not dict :
48+ extra = dict ()
4249
43- params = dict (
44- pin = self .pin ,
45- sid = self .sid ,
46- )
50+ params = dict (
51+ pin = self .pin ,
52+ sid = self .sid ,
53+ )
4754
48- params .update (** extra )
55+ params .update (** extra )
4956
50- result = requests .get ('%s/%s' % (self .webfsapi , path ), params = params )
51- if result .status_code == 404 :
52- return None
57+ result = requests .get ('%s/%s' % (self .webfsapi , path ), params = params , timeout = self .timeout )
58+ if result .status_code == 404 :
59+ return None
60+
61+ return objectify .fromstring (result .content )
62+ except Exception as e :
63+ logging .error ('FSAPI Exception: ' + traceback .format_exc ())
5364
54- return objectify . fromstring ( result . content )
65+ return None
5566
5667 def __del__ (self ):
5768 self .call ('DELETE_SESSION' )
0 commit comments