from urllib2 import Request, urlopen, URLError #import libraries
import unirest #import unirest
import json #import json library


while True:
    #--------------------------------------------------------------------------------------------------------------------------------------------
    def xAxis():
        try:
            response = unirest.get("https://api.particle.io/v1/devices/[your device number here]/xAccel?access_token=[your access token here]", #try unirest API call
                headers=
                    {
                    "accesstoken": "[your access token here]", #access key
                    "Content-Type": "application/x-www-form-urlencoded", #content type
                    "accept": "application/json" #verify JSON
                    },
                    params={ "parameter": 23, "foo": "bar"} #give parameters
                 )
            acceleration = response.raw_body #create variable to hold JSON response
            json_string = acceleration #use json, turn it into dictionary
            parsed_json = json.loads(acceleration) #parsed_json is now dictionary for response
    
            x_accelParse = (parsed_json["result"]) #print parsed json
  
            return (x_accelParse)
    
        except URLError, e:
            print 'could not contact spark API', e #throw URL error if API can't be contacted
    #----------------------------------------------------------------------------------------------------------------------------------------
    def yAxis():
        try:
            response = unirest.get("https://api.particle.io/v1/devices/[your device number here]/yAccel?access_token=[your access token here]", #try unirest API call
                headers=
                    {
                    "accesstoken": "[your access key here]", #access key
                    "Content-Type": "application/x-www-form-urlencoded", #content type
                    "accept": "application/json" #verify JSON
                    },
                    params={ "parameter": 23, "foo": "bar"} #give parameters
                 )
            acceleration = response.raw_body #create variable to hold JSON response
            json_string = acceleration #use json, turn it into dictionary
            parsed_json = json.loads(acceleration) #parsed_json is now dictionary for response
    
            y_accelParse = (parsed_json["result"]) #print parsed json
  
            return (y_accelParse)
    
        except URLError, e:
            print ('could not contact spark API'), e #throw URL error if API can't be contacted
    #-----------------------------------------------------------------------------------------------------------------------------------------
    def zAxis():
        try:
            response = unirest.get("https://api.particle.io/v1/devices/[your device number here]/zAccel?access_token=[your access token here]", #try unirest API call
                headers=
                    {
                    "accesstoken": "[your access key here]", #access key
                    "Content-Type": "application/x-www-form-urlencoded", #content type
                    "accept": "application/json" #verify JSON
                    },
                    params={ "parameter": 23, "foo": "bar"} #give parameters
                 )
            acceleration = response.raw_body #create variable to hold JSON response
            json_string = acceleration #use json, turn it into dictionary
            parsed_json = json.loads(acceleration) #parsed_json is now dictionary for response
    
            z_accelParse = (parsed_json["result"]) #print parsed json
  
            return (z_accelParse)
    
        except URLError, e:
            print 'could not contact spark API', e #throw URL error if API can't be contacted
    #-------------------------------------------------------------------------------------------------------------------------
    xAxis = xAxis()
    yAxis = yAxis()
    zAxis = zAxis()
    
    print("x: ");
    print(xAxis);
    print("y: ");
    print(yAxis);
    print("z: ");
    print(zAxis);
    
