Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: moroen/ikea-tradfri
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: Consoletation/ikea-tradfri
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 3 commits
  • 3 files changed
  • 1 contributor

Commits on Oct 28, 2019

  1. Copy the full SHA
    1a98397 View commit details
  2. Copy the full SHA
    2472b94 View commit details
  3. test

    Stealthii committed Oct 28, 2019
    Copy the full SHA
    9da0e33 View commit details
Showing with 28 additions and 2 deletions.
  1. +11 −0 ikeatradfri/http_server.py
  2. +16 −2 ikeatradfri/routes.py
  3. +1 −0 requirements.txt
11 changes: 11 additions & 0 deletions ikeatradfri/http_server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from aiohttp import web
import aiohttp_cors
import asyncio
import signal
import logging
@@ -20,10 +21,20 @@ async def start(hostConfig):
loop = asyncio.get_event_loop()

app = web.Application()
# Configure default CORS settings.
cors = aiohttp_cors.setup(app, defaults={
"*": aiohttp_cors.ResourceOptions(
allow_credentials=True,
expose_headers="*",
allow_headers="*",
)
})
try:
app["api"], app["gateway"], APP_FACTORY = await connect_to_gateway(hostConfig)

app.add_routes(routes)
for route in list(app.router.routes()):
cors.add(route)

runner = web.AppRunner(app)
await runner.setup()
18 changes: 16 additions & 2 deletions ikeatradfri/routes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from aiohttp import web

from aiohttp_cors import CorsConfig, APP_CONFIG_KEY
from aiohttp_cors import ResourceOptions, CorsViewMixin, custom_cors

try:
from . import devices as Devices
from . import server_commands as Server_Commands
@@ -40,7 +43,15 @@ async def listdevices(request):


@routes.view("/devices/{id}")
class DeviceView(web.View):
class DeviceView(web.View, CorsViewMixin):
cors_config = {
"*": ResourceOptions(
allow_credentials=True,
expose_headers="*",
allow_headers="*",
)
}

async def get(self):
device = await Devices.get_device(
self.request.app["api"],
@@ -53,7 +64,10 @@ async def get(self):

async def put(self):
if self.request.body_exists:
return web.json_response(await Server_Commands.serverCommand(self.request))
try:
return web.json_response(await Server_Commands.serverCommand(self.request))
except TypeError: # Can't serialize, bypass for now
return web.json_response(None)
else:
return web.json_response(
Server_Commands.return_object(
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
aiocoap==0.4a1
aiohttp==4.0.0a0
aiohttp_cors==0.7.0
appdirs==1.4.3
async-timeout==3.0.1
attrs==19.1.0