Cubicle.Jockey
Cubicle.Jockey

Reputation: 3328

Trying to call Lifx API and getting Error in one case but not the other

Okay to save on space I will post pieces of the code. Secondly I am not a Python coder. I am usually C#. So I did my best especially when finding out there was no SWITCH STATEMENT.

So I have one method in my class to talk to Lifx Cloud API and it works fine.

def GetAllLifxs(self):
        selector = 'all';

        uri = '%s%s' % (self._baseUri, selector)
        response = requests.get(uri, headers = self._headers)

        result = LifxProxyResult(999, {})
        if response:
            result = LifxProxyResult(response.status_code, json.loads(response.text))

        return result

The above code ends up hitting the API URL: https://api.lifx.com/v1/lights/all

I am attempting to call (this is not the only method that has this same issue) the toggle api call. I have tried a few different selectors still nothing.

The toggle code is as such:

def ToggleLight(self, value, selectorType = 'all'):
        if not selectorType == 'all':
            if value == None:
                raise TypeError('[value] cannot be None.')

        typeSwitch = {
            'id': 'id:%s' % value,
            'label': 'label:%s' % value,
            'group_id': 'group_id:%s' % value,
            'group': 'group:%s' % value,
            'location_id': 'location_id:%s' % value,
            'location': 'location:%s' % value,
            'scene_id': 'scene_id:%s' % value
        }

        #If nothing just for fun Toggle ALL lights
        selector = '%s/toggle' % typeSwitch.get(selectorType, 'all')   

        uri = '%s%s' % (self._baseUri, selector)
        response = requests.get(uri, headers = self._headers)

        return response

Three attempts have a Response Code of 404. The ToggleLight method in each case produces these URLs.

  1. https://api.lifx.com/v1/lights/label:DreLight/toggle
  2. https://api.lifx.com/v1/lights/id:d073d5127a6e/toggle
  3. https://api.lifx.com/v1/lights/all/toggle

None of them work when I call the ToggleLight method. But here is the kicker. When I copy the URLs generated urls into this plain Python file and run it functions and manipulates the light properly.

import requests

token = "MyMagicKeyHere"

headers = {
    "Authorization": "Bearer %s" % token,
}

response = requests.post('https://api.lifx.com/v1/lights/label:DreLight/toggle', headers=headers)

Python is so new to me I don't understand what my issue is. As the function that works and sets the header information with the token is the same for every method so I don't think it could be that.

Thanks in advance for the second pair of eyes. Business.

EDIT:--------------------- To go along with the answer I was given I could pay closer attention to my method chart and what I type. I messed up pretty stupidly (new word). Lesson here kids is walk away when you get stuck then come back. more staring doesn't help.

enter image description here

Upvotes: 1

Views: 128

Answers (1)

Gerrat
Gerrat

Reputation: 29700

The issue appears to be calling a request.get in ToggleLight, instead of requests.post, like in the stand alone program.

Upvotes: 1

Related Questions