import os
import pika


RABBIT_URL = os.getenv('RABBIT_URL', None)
assert RABBIT_URL

QUEUE_NAME = os.getenv('QUEUE_NAME', None)
assert QUEUE_NAME

EXCHANGE_NAME = os.getenv('EXCHANGE_NAME', 'user_key')
assert EXCHANGE_NAME


def do_something():
	return "something"


def handle_request(ch, method, props, body):
	print(body)
	response = do_something()
	ch.basic_publish(
		exchange='',
		routing_key=props.reply_to,
		properties=pika.BasicProperties(correlation_id=props.correlation_id),
		body=response)
	ch.basic_ack(delivery_tag = method.delivery_tag)


connection = pika.BlockingConnection(pika.URLParameters(RABBIT_URL))

channel = connection.channel()
channel.queue_declare(queue=QUEUE_NAME)

channel.exchange_declare(exchange=EXCHANGE_NAME, type='direct')
channel.queue_bind(exchange=EXCHANGE_NAME, queue=QUEUE_NAME)

channel.basic_qos(prefetch_count=1)
channel.basic_consume(handle_request, queue=QUEUE_NAME)

channel.start_consuming()

@@ -79,26 +79,26 @@
 IT_URL%0A%0A
-QUEUE_NAME
+DEVICE_KEY
  = os.ge
@@ -103,26 +103,26 @@
 getenv('
-QUEUE_NAME
+DEVICE_KEY
 ', None)
@@ -133,144 +133,765 @@
 ert 
-QUEUE_NAME%0A%0AEXCHANGE_NAME = os.getenv('EXCHANGE_NAME', 'user_key')%0Aassert EXCHANGE_NAME%0A%0A%0Adef do_something():%0A%09return %22something%22%0A%0A%0A
+DEVICE_KEY%0A%0A%0Aclass RPCService(object):%0A%0A    def __init__(self, rabbit_url, device_key):%0A        self.rabbit_url = rabbit_url%0A        self.queue_name = 'gpio_service'%0A        self.device_key = device_key%0A        self.connection = pika.BlockingConnection(pika.URLParameters(self.rabbit_url))%0A        self._setup_channel()%0A%0A    def _setup_channel(self):%0A        self.channel = self.connection.channel()%0A        self.channel.queue_declare(queue=self.queue_name)%0A        self.channel.exchange_declare(exchange=self.device_key, type='direct')%0A        self.channel.queue_bind(exchange=self.device_key, queue=self.queue_name)%0A        self.channel.basic_qos(prefetch_count=1)%0A        self.channel.basic_consume(self._handle_request, queue=self.queue_name)%0A%0A    
 def 
+_
 hand
@@ -901,16 +901,22 @@
 request(
+self, 
 ch, meth
@@ -933,17 +933,24 @@
  body):%0A
-%09
+        
 print(bo
@@ -953,17 +953,24 @@
 t(body)%0A
-%09
+        
 response
@@ -972,16 +972,22 @@
 ponse = 
+self._
 do_somet
@@ -993,17 +993,24 @@
 thing()%0A
-%09
+        
 ch.basic
@@ -1019,18 +1019,28 @@
 ublish(%0A
-%09%09
+            
 exchange
@@ -1044,18 +1044,28 @@
 nge='',%0A
-%09%09
+            
 routing_
@@ -1084,18 +1084,28 @@
 ply_to,%0A
-%09%09
+            
 properti
@@ -1170,10 +1170,20 @@
 d),%0A
-%09%09
+            
 body
@@ -1197,9 +1197,16 @@
 se)%0A
-%09
+        
 ch.b
@@ -1255,392 +1255,377 @@
 g)%0A%0A
-%0Aconnection = pika.BlockingConnection(pika.URLParameters(RABBIT_URL))%0A%0Achannel = connection.channel()%0Achannel.queue_declare(queue=QUEUE_NAME)%0A%0Achannel.exchange_declare(exchange=EXCHANGE_NAME, type='direct')%0Achannel.queue_bind(exchange=EXCHANGE_NAME, queue=QUEUE_NAME)%0A%0Achannel.basic_qos(prefetch_count=1)%0Achannel.basic_consume(handle_request, queue=QUEUE_NAME)%0A%0Achannel.start_consum
+    def _do_something(self):%0A        return %22something%22%0A%0A    def run(self):%0A    %09try:%0A        %09self.channel.start_consuming()%0A        except KeyboardInterrupt:%0A        %09self.channel.stop_consuming()%0A        self.connection.close()%0A%0A%0Adef main():%0A%09rpc_service = RPCService(rabbit_url=RABBIT_URL, device_key=DEVICE_KEY)%0A%09rpc_service.run()%0A%0A%0Aif __name__ == '__main__':%0A%09ma
 in
-g
 ()%0A

Make service a class