コード例 #1
0
    async def test_device_CRUD(self):
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/devices') as r:
            self.assertEqual(r.status, 200)
            rst = await r.json()
            self.assertSetEqual(set(rst), {'1', '2', '3'})
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/devices/1') as r:
            self.assertEqual(r.status, 200)
            rst = await r.json()
            self.assertDictEqual(rst, mock_data.device1)
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/devices/99') as r:
            self.assertEqual(r.status, 404)
            rst = await r.text()
            self.assertEqual(rst, 'device_id not found!')

        async with aiohttp.post('http://127.0.0.1:8080/api/v1/devices',
                                data=json.dumps(mock_data.test_device)) as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.hgetall('HS:DEVICE:4')
            self.assertEqual(rst['name'], '测试集中器4')
            rst = self.redis_client.sismember('SET:DEVICE', 4)
            self.assertTrue(rst)
        async with aiohttp.post('http://127.0.0.1:8080/api/v1/devices',
                                data=json.dumps(mock_data.test_device)) as r:
            self.assertEqual(r.status, 409)
            rst = await r.text()
            self.assertEqual(rst, 'device already exists!')

        mock_data.test_device['name'] = '测试集中器5'
        mock_data.test_device['id'] = 5
        async with aiohttp.put('http://127.0.0.1:8080/api/v1/devices/4',
                               data=json.dumps(mock_data.test_device)) as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.exists('HS:DEVICE:4')
            self.assertFalse(rst)
            rst = self.redis_client.sismember('SET:DEVICE', 4)
            self.assertFalse(rst)
            rst = self.redis_client.hgetall('HS:DEVICE:5')
            self.assertEqual(rst['name'], '测试集中器5')
            rst = self.redis_client.sismember('SET:DEVICE', 5)
            self.assertTrue(rst)
        async with aiohttp.put('http://127.0.0.1:8080/api/v1/devices/99',
                               data=json.dumps(mock_data.test_device)) as r:
            self.assertEqual(r.status, 404)
            rst = await r.text()
            self.assertEqual(rst, 'device_id not found!')

        async with aiohttp.delete(
                'http://127.0.0.1:8080/api/v1/devices/5') as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.exists('HS:DEVICE:5')
            self.assertFalse(rst)
            rst = self.redis_client.sismember('SET:DEVICE', 5)
            self.assertFalse(rst)

        devices = [1, 2, 3]
        async with aiohttp.post('http://127.0.0.1:8080/api/v2/devices/del',
                                data=json.dumps(devices)) as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.smembers('SET:DEVICE')
            self.assertEqual(len(rst), 0)
コード例 #2
0
    async def test_device_CRUD(self):
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/devices') as r:
            self.assertEqual(r.status, 200)
            rst = await r.json()
            self.assertSetEqual(set(rst), {'1', '2', '3'})
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/devices/1') as r:
            self.assertEqual(r.status, 200)
            rst = await r.json()
            self.assertDictEqual(rst, mock_data.device1)
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/devices/99') as r:
            self.assertEqual(r.status, 404)
            rst = await r.text()
            self.assertEqual(rst, 'device_id not found!')

        async with aiohttp.post('http://127.0.0.1:8080/api/v1/devices', data=json.dumps(mock_data.test_device)) as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.hgetall('HS:DEVICE:4')
            self.assertEqual(rst['name'], '测试集中器4')
            rst = self.redis_client.sismember('SET:DEVICE', 4)
            self.assertTrue(rst)
        async with aiohttp.post('http://127.0.0.1:8080/api/v1/devices', data=json.dumps(mock_data.test_device)) as r:
            self.assertEqual(r.status, 409)
            rst = await r.text()
            self.assertEqual(rst, 'device already exists!')

        mock_data.test_device['name'] = '测试集中器5'
        mock_data.test_device['id'] = 5
        async with aiohttp.put('http://127.0.0.1:8080/api/v1/devices/4', data=json.dumps(mock_data.test_device)) as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.exists('HS:DEVICE:4')
            self.assertFalse(rst)
            rst = self.redis_client.sismember('SET:DEVICE', 4)
            self.assertFalse(rst)
            rst = self.redis_client.hgetall('HS:DEVICE:5')
            self.assertEqual(rst['name'], '测试集中器5')
            rst = self.redis_client.sismember('SET:DEVICE', 5)
            self.assertTrue(rst)
        async with aiohttp.put('http://127.0.0.1:8080/api/v1/devices/99', data=json.dumps(mock_data.test_device)) as r:
            self.assertEqual(r.status, 404)
            rst = await r.text()
            self.assertEqual(rst, 'device_id not found!')

        async with aiohttp.delete('http://127.0.0.1:8080/api/v1/devices/5') as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.exists('HS:DEVICE:5')
            self.assertFalse(rst)
            rst = self.redis_client.sismember('SET:DEVICE', 5)
            self.assertFalse(rst)

        devices = [1, 2, 3]
        async with aiohttp.post('http://127.0.0.1:8080/api/v2/devices/del', data=json.dumps(devices)) as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.smembers('SET:DEVICE')
            self.assertEqual(len(rst), 0)
コード例 #3
0
ファイル: common.py プロジェクト: DomenRok/Bernard
async def ban_verbose(user, reason):
    try:
        url = config.cfg['discord']['endpoint'] + "/guilds/" + config.cfg[
            'discord']['server'] + "/bans/" + user.id
        params = [('reason', reason), ('delete-message-days', 0)]
        async with aiohttp.put(url,
                               params=params,
                               headers={
                                   'Authorization':
                                   'Bot ' + config.cfg['discord']['token']
                               }) as r:
            logger.info(
                "common.ban_verbose() attempting async URL {0} params {1}".
                format(url, params))
            if r.status == 204:
                return True
            else:
                logger.warn(
                    "common.ban_verbose() unable to PUT to endpoint. HTTP code not valid HTTP/{}"
                    .format(r.status))
                return False

    except Exception as e:
        logger.error("common.ban_verbose() threw an exception: {0}".format(e))
        return False
コード例 #4
0
ファイル: test_resource.py プロジェクト: dmonroy/chilero
    def test_update(self):

        resp = yield from aiohttp.post(
            self.full_url(self.app.reverse('fruit_index')),
            data=json.dumps(dict(name='pear', colors=['green'])),
        )

        self.assertEqual(resp.status, 200)
        resp.close()

        resp = yield from aiohttp.get(
            self.full_url(self.app.reverse('fruit_item', id='pear'))
        )

        self.assertEqual(resp.status, 200)
        jr = yield from resp.json()
        self.assertEqual(jr['body'], dict(colors=['green']))
        resp.close()

        resp = yield from aiohttp.put(
            self.full_url(self.app.reverse('fruit_item', id='pear')),
            data=json.dumps(dict(colors=['green', 'yellow'])),
        )

        self.assertEqual(resp.status, 200)
        resp.close()

        resp = yield from aiohttp.get(
            self.full_url(self.app.reverse('fruit_item', id='pear'))
        )

        self.assertEqual(resp.status, 200)
        jr = yield from resp.json()
        self.assertEqual(jr['body'], dict(colors=['green', 'yellow']))
        resp.close()
コード例 #5
0
ファイル: apitools.py プロジェクト: orels1/ORELS-Cogs
    async def _post(self, ctx, *, url):
        """Performs POST or PUT request to selected URL"""

        await self.bot.say('Set headers by typing them in a `name=value` format, one on each line, or `pass`')

        answer = await self.bot.wait_for_message(timeout=50, author=ctx.message.author)

        parsed = self._parse_headers(answer)
        headers = parsed['headers']
        public_headers = parsed['public_headers']

        await self.bot.say('Headers are set to:\n```json\n{}```\nSet body typing in in a `name=value` one on each line or `pass`\nNested objects are not supported'
                           .format(json.dumps(public_headers, indent=4, sort_keys=True)))

        answer = await self.bot.wait_for_message(timeout=50, author=ctx.message.author)

        body = self._parse_body(answer)

        await self.bot.say('Body is set to:\n```json\n{}```'.format(json.dumps(body, indent=4, sort_keys=True)))

        url = url.strip()
        method = ctx.invoked_with

        if method == 'post':
            t1 = time.perf_counter()
            async with aiohttp.post(url, headers=headers, data=json.dumps(body)) as r:
                t2 = time.perf_counter()
                data = await r.text()
                status = r.status

        if method == 'put':
            if 'Content-Type' not in headers:
                headers['Content-Type'] = 'application/json'

            t1 = time.perf_counter()
            async with aiohttp.put(url, headers=headers, data=json.dumps(body)) as r:
                t2 = time.perf_counter()
                data = await r.text()
                status = r.status

        try:
            parsed = json.loads(data)
        except:
            parsed = json.loads('{}')

        color = status == 200 and 0x2ecc71 or status >= 400 and 0xe74c3c
        embed = discord.Embed(title='Results for **{}** {}'.format(method.upper(), url),
                              color=color,
                              description='```json\n{}```'.format(len(data) < 700
                                                                  and
                                                                  json.dumps(parsed, indent=4, sort_keys=True)
                                                                  or
                                                                  json.dumps(parsed, indent=4, sort_keys=True)[:700]
                                                                  + '\n\n...\n\n'))
        embed.add_field(name='Status',
                        value=status)
        embed.add_field(name='Time',
                        value='{}ms'.format(str((t2-t1) * 1000)[:3]))
        await self.bot.say(embed=embed)
コード例 #6
0
ファイル: bench2.py プロジェクト: sile16/isilon-audit
    async def send_command(self, data, stats):
        # getter function with semaphore
        async with self.sem:
            try:
                start_time = time.time()
                with aiohttp.Timeout(5):
                    async with aiohttp.put(url,data=data) as response:
                        await response.read()
                        stats.add("data",time.time() - start_time)
                        stats.add("success")
                        return

            except Exception as ex:
                stats.add(type(ex).__name__)
                print(traceback.print_exc())

            
            self.count += 1
            if self.count >= self.args.count:
                self.loop.stop()
コード例 #7
0
    async def test_formula_CRUD(self):
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/formulas') as r:
            self.assertEqual(r.status, 200)
            rst = await r.json()
            self.assertSequenceEqual(rst, ['1'])
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/formulas/1') as r:
            self.assertEqual(r.status, 200)
            rst = await r.json()
            self.assertDictEqual(rst, mock_data.formula1)
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/formulas/99') as r:
            self.assertEqual(r.status, 404)
            rst = await r.text()
            self.assertEqual(rst, 'formula_id not found!')

        async with aiohttp.post('http://127.0.0.1:8080/api/v1/formulas', data=json.dumps(mock_data.test_formula)) as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.hgetall('HS:FORMULA:9')
            self.assertEqual(rst['formula'], mock_data.test_formula['formula'])
            rst = self.redis_client.sismember('SET:FORMULA', 9)
            self.assertTrue(rst)
        async with aiohttp.post('http://127.0.0.1:8080/api/v1/formulas', data=json.dumps(mock_data.test_formula)) as r:
            self.assertEqual(r.status, 409)
            rst = await r.text()
            self.assertEqual(rst, 'formula already exists!')

        mock_data.test_formula['formula'] = '2+2'
        mock_data.test_formula['id'] = 5
        async with aiohttp.put('http://127.0.0.1:8080/api/v1/formulas/9', data=json.dumps(mock_data.test_formula)) as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.exists('HS:FORMULA:9')
            self.assertFalse(rst)
            rst = self.redis_client.sismember('SET:FORMULA', 9)
            self.assertFalse(rst)
            rst = self.redis_client.hgetall('HS:FORMULA:5')
            self.assertEqual(rst['formula'], '2+2')
            rst = self.redis_client.sismember('SET:FORMULA', 5)
            self.assertTrue(rst)
        async with aiohttp.put('http://127.0.0.1:8080/api/v1/formulas/99', data=json.dumps(mock_data.test_formula)) as r:
            self.assertEqual(r.status, 404)
            rst = await r.text()
            self.assertEqual(rst, 'formula_id not found!')

        async with aiohttp.delete('http://127.0.0.1:8080/api/v1/formulas/5') as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.exists('HS:FORMULA:5')
            self.assertFalse(rst)
            rst = self.redis_client.sismember('SET:FORMULA', 5)
            self.assertFalse(rst)

        formula_check = {'formula': 'p1[-1]+10', 'p1': '1:10:1000'}
        async with aiohttp.post('http://127.0.0.1:8080/api/v1/formula_check', data=json.dumps(formula_check)) as r:
            self.assertEqual(r.status, 200)
            rst = await r.text()
            self.assertEqual(rst, 'OK')
        formula_check['formula'] = 'p1[-1]+p2[-2]'
        async with aiohttp.post('http://127.0.0.1:8080/api/v1/formula_check', data=json.dumps(formula_check)) as r:
            self.assertEqual(r.status, 200)
            rst = await r.text()
            self.assertEqual(rst, """NameError
   p1[-1]+p2[-2]
           ^^^
name 'p2' is not defined
""")

        formula_check['p1'] = '123:45:6'
        async with aiohttp.post('http://127.0.0.1:8080/api/v1/formula_check', data=json.dumps(formula_check)) as r:
            self.assertEqual(r.status, 200)
            rst = await r.text()
            self.assertEqual(rst, "parameter not found: p1=123:45:6")

        formulas = [1]
        async with aiohttp.post('http://127.0.0.1:8080/api/v2/formulas/del', data=json.dumps(formulas)) as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.smembers('SET:FORMULA')
            self.assertEqual(len(rst), 0)
コード例 #8
0
    async def test_term_item_CRUD(self):
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/terms/10/items/1000') as r:
            self.assertEqual(r.status, 200)
            rst = await r.json()
            self.assertDictEqual(rst, mock_data.term10_item1000)
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/terms/99/items/1000') as r:
            self.assertEqual(r.status, 404)
            rst = await r.text()
            self.assertEqual(rst, 'term_id not found!')
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/terms/10/items/99') as r:
            self.assertEqual(r.status, 404)
            rst = await r.text()
            self.assertEqual(rst, 'item_id not found!')
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/terms/20/items/2000') as r:
            self.assertEqual(r.status, 404)
            rst = await r.text()
            self.assertEqual(rst, 'term_item not found!')

        async with aiohttp.post('http://127.0.0.1:8080/api/v1/terms/20/items',
                                data=json.dumps(mock_data.test_term_item)) as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.hgetall('HS:TERM_ITEM:20:2000')
            mock_data.test_term_item.update({'device_id': '1'})
            self.assertDictEqual(rst, mock_data.test_term_item)
            rst = self.redis_client.hgetall('HS:MAPPING:IEC104:1:400')
            self.assertDictEqual(rst, mock_data.test_term_item)
            rst = self.redis_client.sismember('SET:TERM_ITEM:20', 2000)
            self.assertTrue(rst)
        async with aiohttp.post('http://127.0.0.1:8080/api/v1/terms/20/items',
                                data=json.dumps(mock_data.test_term_item)) as r:
            self.assertEqual(r.status, 409)
            rst = await r.text()
            self.assertEqual(rst, 'term_item already exists!')

        mock_data.test_term_item['protocol_code'] = '401'
        async with aiohttp.put('http://127.0.0.1:8080/api/v1/terms/20/items/2000',
                               data=json.dumps(mock_data.test_term_item)) as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.sismember('SET:TERM_ITEM:20', 2000)
            self.assertTrue(rst)
            rst = self.redis_client.exists('HS:MAPPING:IEC104:1:400')
            self.assertFalse(rst)
            rst = self.redis_client.hgetall('HS:TERM_ITEM:20:2000')
            self.assertEqual(rst['protocol_code'], '401')
            rst = self.redis_client.hgetall('HS:MAPPING:IEC104:1:401')
            self.assertDictEqual(rst, mock_data.test_term_item)
        async with aiohttp.put('http://127.0.0.1:8080/api/v1/terms/30/items/2000',
                               data=json.dumps(mock_data.test_term_item)) as r:
            self.assertEqual(r.status, 404)
            rst = await r.text()
            self.assertEqual(rst, 'term_item not found!')

        async with aiohttp.delete('http://127.0.0.1:8080/api/v1/terms/20/items/2000') as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.exists('HS:TERM_ITEM:20:2000')
            self.assertFalse(rst)
            rst = self.redis_client.exists('HS:MAPPING:IEC104:1:401')
            self.assertFalse(rst)
            rst = self.redis_client.sismember('SET:TERM_ITEM:20', 2000)
            self.assertFalse(rst)
        del mock_data.test_term_item['device_id']

        async with aiohttp.post('http://127.0.0.1:8080/api/v2/term_items/del',
                                  data=json.dumps(mock_data.term_item_list)) as r:
            self.assertEqual(r.status, 200)
            rst = list(self.redis_client.scan_iter('HS:TERM_ITEMS:*'))
            self.assertEqual(len(rst), 0)
コード例 #9
0
    async def test_item_CRUD(self):
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/items') as r:
            self.assertEqual(r.status, 200)
            rst = await r.json()
            self.assertSetEqual(set(rst), {'1000', '2000'})
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/items/1000') as r:
            self.assertEqual(r.status, 200)
            rst = await r.json()
            self.assertDictEqual(rst, mock_data.item1000)
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/items/99') as r:
            self.assertEqual(r.status, 404)
            rst = await r.text()
            self.assertEqual(rst, 'item_id not found!')
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/terms/10/items') as r:
            self.assertEqual(r.status, 200)
            rst = await r.json()
            self.assertSetEqual(set(rst), {'1000', '2000'})
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/terms/99/items') as r:
            self.assertEqual(r.status, 404)
            rst = await r.text()
            self.assertEqual(rst, 'term_id not found!')

        async with aiohttp.post('http://127.0.0.1:8080/api/v1/items', data=json.dumps(mock_data.test_item)) as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.hgetall('HS:ITEM:3000')
            self.assertEqual(rst['name'], 'C相电压')
            rst = self.redis_client.sismember('SET:ITEM', 3000)
            self.assertTrue(rst)
        async with aiohttp.post('http://127.0.0.1:8080/api/v1/items', data=json.dumps(mock_data.test_item)) as r:
            self.assertEqual(r.status, 409)
            rst = await r.text()
            self.assertEqual(rst, 'item already exists!')

        mock_data.test_item['name'] = '功率因数'
        mock_data.test_item['id'] = 4000
        async with aiohttp.put('http://127.0.0.1:8080/api/v1/items/3000', data=json.dumps(mock_data.test_item)) as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.exists('HS:ITEM:3000')
            self.assertFalse(rst)
            rst = self.redis_client.sismember('SET:ITEM', 3000)
            self.assertFalse(rst)
            rst = self.redis_client.hgetall('HS:ITEM:4000')
            self.assertEqual(rst['name'], '功率因数')
            rst = self.redis_client.sismember('SET:ITEM', 4000)
            self.assertTrue(rst)
        async with aiohttp.put('http://127.0.0.1:8080/api/v1/items/99', data=json.dumps(mock_data.test_item)) as r:
            self.assertEqual(r.status, 404)
            rst = await r.text()
            self.assertEqual(rst, 'item_id not found!')

        async with aiohttp.delete('http://127.0.0.1:8080/api/v1/items/4000') as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.exists('HS:ITEM:4000')
            self.assertFalse(rst)
            rst = self.redis_client.sismember('SET:ITEM', 4000)
            self.assertFalse(rst)

        items = [1000, 2000]
        async with aiohttp.post('http://127.0.0.1:8080/api/v2/items/del', data=json.dumps(items)) as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.smembers('SET:ITEM')
            self.assertEqual(len(rst), 0)
コード例 #10
0
    async def test_term_CRUD(self):
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/terms') as r:
            self.assertEqual(r.status, 200)
            rst = await r.json()
            self.assertSetEqual(set(rst), {'10', '20', '30', '40'})
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/terms/10') as r:
            self.assertEqual(r.status, 200)
            rst = await r.json()
            self.assertDictEqual(rst, mock_data.term10)
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/terms/99') as r:
            self.assertEqual(r.status, 404)
            rst = await r.text()
            self.assertEqual(rst, 'term_id not found!')
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/devices/1/terms') as r:
            self.assertEqual(r.status, 200)
            rst = await r.json()
            self.assertSetEqual(set(rst), {'10', '20'})
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/devices/99/terms') as r:
            self.assertEqual(r.status, 404)
            rst = await r.text()
            self.assertEqual(rst, 'device_id not found!')

        async with aiohttp.post('http://127.0.0.1:8080/api/v1/terms', data=json.dumps(mock_data.test_term)) as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.hgetall('HS:TERM:90')
            self.assertEqual(rst['name'], '测试终端9')
            rst = self.redis_client.sismember('SET:TERM', 90)
            self.assertTrue(rst)
            rst = self.redis_client.sismember('SET:DEVICE_TERM:1', 90)
            self.assertEqual(rst, True)
        async with aiohttp.post('http://127.0.0.1:8080/api/v1/terms', data=json.dumps(mock_data.test_term)) as r:
            self.assertEqual(r.status, 409)
            rst = await r.text()
            self.assertEqual(rst, 'term already exists!')

        mock_data.test_term['name'] = '测试终端5'
        mock_data.test_term['id'] = 50
        mock_data.test_term['device_id'] = 2
        async with aiohttp.put('http://127.0.0.1:8080/api/v1/terms/90', data=json.dumps(mock_data.test_term)) as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.exists('HS:TERM:90')
            self.assertFalse(rst)
            rst = self.redis_client.sismember('SET:TERM', 90)
            self.assertFalse(rst)
            rst = self.redis_client.sismember('SET:DEVICE_TERM:1', 90)
            self.assertFalse(rst)
            rst = self.redis_client.hgetall('HS:TERM:50')
            self.assertEqual(rst['name'], '测试终端5')
            rst = self.redis_client.sismember('SET:TERM', 50)
            self.assertTrue(rst)
            rst = self.redis_client.sismember('SET:DEVICE_TERM:2', 50)
            self.assertTrue(rst)
        async with aiohttp.put('http://127.0.0.1:8080/api/v1/terms/99', data=json.dumps(mock_data.test_term)) as r:
            self.assertEqual(r.status, 404)
            rst = await r.text()
            self.assertEqual(rst, 'term_id not found!')

        async with aiohttp.delete('http://127.0.0.1:8080/api/v1/terms/50') as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.exists('HS:TERM:50')
            self.assertFalse(rst)
            rst = self.redis_client.sismember('SET:TERM', 50)
            self.assertFalse(rst)

        terms = [10, 20, 30, 40]
        async with aiohttp.post('http://127.0.0.1:8080/api/v2/terms/del', data=json.dumps(terms)) as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.smembers('SET:TERM')
            self.assertEqual(len(rst), 0)
コード例 #11
0
ファイル: forum.py プロジェクト: xperthieflover/discord_bot
    async def kokoro(self, ctx, *user: str):
        '''Gibt Benutzerdaten über einen Benutzer aus Kokoro-ko.de aus

        Beispiel:
        -----------

        :kokoro

        :user Eddy
        '''
        if len(user) == 0:
            username = ctx.message.author.name
        else:
            username = user[0]

        url = f'{self.discourseURL}/users/{username}.json'
        async with aiohttp.put(url, data=loadconfig.__discourseAPIKey__) as r:
            if r.status == 200:
                json = await r.json()
                if json['user']['primary_group_flair_bg_color'] == None or True:
                    color = 0xff6600  #orange
                else:
                    #color = hex(int(json['user']['primary_group_flair_bg_color'], 16))
                    color = discord.Color(
                        hex(
                            int(json['user']['primary_group_flair_bg_color'],
                                16)))
                    print(color.value)
                    #currently not working??
                embed = discord.Embed(color=color)
                embed.set_footer(
                    text='kokoro-ko.de - Dein Anime und Gaming forum')
                avatarURL = self.discourseURL + json['user']['avatar_template']
                embed.set_thumbnail(url=avatarURL.format(size='124'))
                if json['user']['name'] == '':
                    discordName = json['user']['username']
                else:
                    discordName = '{} ({})'.format(json['user']['username'],
                                                   json['user']['name'])
                embed.add_field(name='Username',
                                value=discordName,
                                inline=True)
                embed.add_field(name='Vertrauensstufe',
                                value=json['user']['trust_level'],
                                inline=True)
                if json['user']['title'] != '' and json['user'][
                        'title'] != None:
                    embed.add_field(name='Titel',
                                    value=json['user']['title'],
                                    inline=True)
                embed.add_field(name='Registriert am',
                                value=json['user']['created_at'],
                                inline=True)
                embed.add_field(name='Abzeichen',
                                value=json['user']['badge_count'],
                                inline=True)
                embed.add_field(name='Beiträge',
                                value=json['user']['post_count'],
                                inline=True)
                if json['user']['user_fields']['7'] != '' and json['user'][
                        'user_fields']['7'] != None:
                    embed.add_field(name='Discord',
                                    value=json['user']['user_fields']['7'],
                                    inline=True)
                if json['user']['user_fields']['1'] != '' and json['user'][
                        'user_fields']['1'] != None:
                    embed.add_field(name='Steam',
                                    value='http://steamcommunity.com/id/' +
                                    json['user']['user_fields']['1'],
                                    inline=True)
                groups = ''
                for group in json['user']['groups']:
                    if group['automatic'] == False:
                        groups += group['name'] + ', '
                if groups != '':
                    embed.add_field(name='Gruppen',
                                    value=groups[:-2],
                                    inline=True)
                embed.add_field(
                    name='Profile Link',
                    value=f'{self.discourseURL}/users/{username}/summary',
                    inline=True)
                await self.bot.say(embed=embed)
            else:
                msg = f':no_entry: Ich konnte keinen Account **{username}** auf kokoro-ko.de finden :sweat:'
                await self.bot.say(msg)
コード例 #12
0
    async def _post(self, ctx, *, url):
        """Performs POST or PUT request to selected URL"""

        await self.bot.say(
            'Set headers by typing them in a `name=value` format, one on each line, or `pass`'
        )

        answer = await self.bot.wait_for_message(timeout=50,
                                                 author=ctx.message.author)

        parsed = self._parse_headers(answer)
        headers = parsed['headers']
        public_headers = parsed['public_headers']

        await self.bot.say(
            'Headers are set to:\n```json\n{}```\nSet body typing in in a `name=value` one on each line or `pass`\nNested objects are not supported'
            .format(json.dumps(public_headers, indent=4, sort_keys=True)))

        answer = await self.bot.wait_for_message(timeout=50,
                                                 author=ctx.message.author)

        body = self._parse_body(answer)

        await self.bot.say('Body is set to:\n```json\n{}```'.format(
            json.dumps(body, indent=4, sort_keys=True)))

        url = url.strip()
        method = ctx.invoked_with

        if method == 'post':
            t1 = time.perf_counter()
            async with aiohttp.post(url,
                                    headers=headers,
                                    data=json.dumps(body)) as r:
                t2 = time.perf_counter()
                data = await r.text()
                status = r.status

        if method == 'put':
            if 'Content-Type' not in headers:
                headers['Content-Type'] = 'application/json'

            t1 = time.perf_counter()
            async with aiohttp.put(url, headers=headers,
                                   data=json.dumps(body)) as r:
                t2 = time.perf_counter()
                data = await r.text()
                status = r.status

        try:
            parsed = json.loads(data)
        except:
            parsed = json.loads('{}')

        color = status == 200 and 0x2ecc71 or status >= 400 and 0xe74c3c
        embed = discord.Embed(
            title='Results for **{}** {}'.format(method.upper(), url),
            color=color,
            description='```json\n{}```'.format(
                len(data) < 700
                and json.dumps(parsed, indent=4, sort_keys=True)
                or json.dumps(parsed, indent=4, sort_keys=True)[:700] +
                '\n\n...\n\n'))
        embed.add_field(name='Status', value=status)
        embed.add_field(name='Time',
                        value='{}ms'.format(str((t2 - t1) * 1000)[:3]))
        await self.bot.say(embed=embed)
コード例 #13
0
    async def test_term_item_CRUD(self):
        async with aiohttp.get(
                'http://127.0.0.1:8080/api/v1/terms/10/items/1000') as r:
            self.assertEqual(r.status, 200)
            rst = await r.json()
            self.assertDictEqual(rst, mock_data.term10_item1000)
        async with aiohttp.get(
                'http://127.0.0.1:8080/api/v1/terms/99/items/1000') as r:
            self.assertEqual(r.status, 404)
            rst = await r.text()
            self.assertEqual(rst, 'term_id not found!')
        async with aiohttp.get(
                'http://127.0.0.1:8080/api/v1/terms/10/items/99') as r:
            self.assertEqual(r.status, 404)
            rst = await r.text()
            self.assertEqual(rst, 'item_id not found!')
        async with aiohttp.get(
                'http://127.0.0.1:8080/api/v1/terms/20/items/2000') as r:
            self.assertEqual(r.status, 404)
            rst = await r.text()
            self.assertEqual(rst, 'term_item not found!')

        async with aiohttp.post('http://127.0.0.1:8080/api/v1/terms/20/items',
                                data=json.dumps(
                                    mock_data.test_term_item)) as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.hgetall('HS:TERM_ITEM:20:2000')
            mock_data.test_term_item.update({'device_id': '1'})
            self.assertDictEqual(rst, mock_data.test_term_item)
            rst = self.redis_client.hgetall('HS:MAPPING:IEC104:1:400')
            self.assertDictEqual(rst, mock_data.test_term_item)
            rst = self.redis_client.sismember('SET:TERM_ITEM:20', 2000)
            self.assertTrue(rst)
        async with aiohttp.post('http://127.0.0.1:8080/api/v1/terms/20/items',
                                data=json.dumps(
                                    mock_data.test_term_item)) as r:
            self.assertEqual(r.status, 409)
            rst = await r.text()
            self.assertEqual(rst, 'term_item already exists!')

        mock_data.test_term_item['protocol_code'] = '401'
        async with aiohttp.put(
                'http://127.0.0.1:8080/api/v1/terms/20/items/2000',
                data=json.dumps(mock_data.test_term_item)) as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.sismember('SET:TERM_ITEM:20', 2000)
            self.assertTrue(rst)
            rst = self.redis_client.exists('HS:MAPPING:IEC104:1:400')
            self.assertFalse(rst)
            rst = self.redis_client.hgetall('HS:TERM_ITEM:20:2000')
            self.assertEqual(rst['protocol_code'], '401')
            rst = self.redis_client.hgetall('HS:MAPPING:IEC104:1:401')
            self.assertDictEqual(rst, mock_data.test_term_item)
        async with aiohttp.put(
                'http://127.0.0.1:8080/api/v1/terms/30/items/2000',
                data=json.dumps(mock_data.test_term_item)) as r:
            self.assertEqual(r.status, 404)
            rst = await r.text()
            self.assertEqual(rst, 'term_item not found!')

        async with aiohttp.delete(
                'http://127.0.0.1:8080/api/v1/terms/20/items/2000') as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.exists('HS:TERM_ITEM:20:2000')
            self.assertFalse(rst)
            rst = self.redis_client.exists('HS:MAPPING:IEC104:1:401')
            self.assertFalse(rst)
            rst = self.redis_client.sismember('SET:TERM_ITEM:20', 2000)
            self.assertFalse(rst)
        del mock_data.test_term_item['device_id']

        async with aiohttp.post('http://127.0.0.1:8080/api/v2/term_items/del',
                                data=json.dumps(
                                    mock_data.term_item_list)) as r:
            self.assertEqual(r.status, 200)
            rst = list(self.redis_client.scan_iter('HS:TERM_ITEMS:*'))
            self.assertEqual(len(rst), 0)
コード例 #14
0
    async def test_item_CRUD(self):
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/items') as r:
            self.assertEqual(r.status, 200)
            rst = await r.json()
            self.assertSetEqual(set(rst), {'1000', '2000'})
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/items/1000') as r:
            self.assertEqual(r.status, 200)
            rst = await r.json()
            self.assertDictEqual(rst, mock_data.item1000)
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/items/99') as r:
            self.assertEqual(r.status, 404)
            rst = await r.text()
            self.assertEqual(rst, 'item_id not found!')
        async with aiohttp.get(
                'http://127.0.0.1:8080/api/v1/terms/10/items') as r:
            self.assertEqual(r.status, 200)
            rst = await r.json()
            self.assertSetEqual(set(rst), {'1000', '2000'})
        async with aiohttp.get(
                'http://127.0.0.1:8080/api/v1/terms/99/items') as r:
            self.assertEqual(r.status, 404)
            rst = await r.text()
            self.assertEqual(rst, 'term_id not found!')

        async with aiohttp.post('http://127.0.0.1:8080/api/v1/items',
                                data=json.dumps(mock_data.test_item)) as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.hgetall('HS:ITEM:3000')
            self.assertEqual(rst['name'], 'C相电压')
            rst = self.redis_client.sismember('SET:ITEM', 3000)
            self.assertTrue(rst)
        async with aiohttp.post('http://127.0.0.1:8080/api/v1/items',
                                data=json.dumps(mock_data.test_item)) as r:
            self.assertEqual(r.status, 409)
            rst = await r.text()
            self.assertEqual(rst, 'item already exists!')

        mock_data.test_item['name'] = '功率因数'
        mock_data.test_item['id'] = 4000
        async with aiohttp.put('http://127.0.0.1:8080/api/v1/items/3000',
                               data=json.dumps(mock_data.test_item)) as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.exists('HS:ITEM:3000')
            self.assertFalse(rst)
            rst = self.redis_client.sismember('SET:ITEM', 3000)
            self.assertFalse(rst)
            rst = self.redis_client.hgetall('HS:ITEM:4000')
            self.assertEqual(rst['name'], '功率因数')
            rst = self.redis_client.sismember('SET:ITEM', 4000)
            self.assertTrue(rst)
        async with aiohttp.put('http://127.0.0.1:8080/api/v1/items/99',
                               data=json.dumps(mock_data.test_item)) as r:
            self.assertEqual(r.status, 404)
            rst = await r.text()
            self.assertEqual(rst, 'item_id not found!')

        async with aiohttp.delete(
                'http://127.0.0.1:8080/api/v1/items/4000') as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.exists('HS:ITEM:4000')
            self.assertFalse(rst)
            rst = self.redis_client.sismember('SET:ITEM', 4000)
            self.assertFalse(rst)

        items = [1000, 2000]
        async with aiohttp.post('http://127.0.0.1:8080/api/v2/items/del',
                                data=json.dumps(items)) as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.smembers('SET:ITEM')
            self.assertEqual(len(rst), 0)
コード例 #15
0
    async def test_term_CRUD(self):
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/terms') as r:
            self.assertEqual(r.status, 200)
            rst = await r.json()
            self.assertSetEqual(set(rst), {'10', '20', '30', '40'})
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/terms/10') as r:
            self.assertEqual(r.status, 200)
            rst = await r.json()
            self.assertDictEqual(rst, mock_data.term10)
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/terms/99') as r:
            self.assertEqual(r.status, 404)
            rst = await r.text()
            self.assertEqual(rst, 'term_id not found!')
        async with aiohttp.get(
                'http://127.0.0.1:8080/api/v1/devices/1/terms') as r:
            self.assertEqual(r.status, 200)
            rst = await r.json()
            self.assertSetEqual(set(rst), {'10', '20'})
        async with aiohttp.get(
                'http://127.0.0.1:8080/api/v1/devices/99/terms') as r:
            self.assertEqual(r.status, 404)
            rst = await r.text()
            self.assertEqual(rst, 'device_id not found!')

        async with aiohttp.post('http://127.0.0.1:8080/api/v1/terms',
                                data=json.dumps(mock_data.test_term)) as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.hgetall('HS:TERM:90')
            self.assertEqual(rst['name'], '测试终端9')
            rst = self.redis_client.sismember('SET:TERM', 90)
            self.assertTrue(rst)
            rst = self.redis_client.sismember('SET:DEVICE_TERM:1', 90)
            self.assertEqual(rst, True)
        async with aiohttp.post('http://127.0.0.1:8080/api/v1/terms',
                                data=json.dumps(mock_data.test_term)) as r:
            self.assertEqual(r.status, 409)
            rst = await r.text()
            self.assertEqual(rst, 'term already exists!')

        mock_data.test_term['name'] = '测试终端5'
        mock_data.test_term['id'] = 50
        mock_data.test_term['device_id'] = 2
        async with aiohttp.put('http://127.0.0.1:8080/api/v1/terms/90',
                               data=json.dumps(mock_data.test_term)) as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.exists('HS:TERM:90')
            self.assertFalse(rst)
            rst = self.redis_client.sismember('SET:TERM', 90)
            self.assertFalse(rst)
            rst = self.redis_client.sismember('SET:DEVICE_TERM:1', 90)
            self.assertFalse(rst)
            rst = self.redis_client.hgetall('HS:TERM:50')
            self.assertEqual(rst['name'], '测试终端5')
            rst = self.redis_client.sismember('SET:TERM', 50)
            self.assertTrue(rst)
            rst = self.redis_client.sismember('SET:DEVICE_TERM:2', 50)
            self.assertTrue(rst)
        async with aiohttp.put('http://127.0.0.1:8080/api/v1/terms/99',
                               data=json.dumps(mock_data.test_term)) as r:
            self.assertEqual(r.status, 404)
            rst = await r.text()
            self.assertEqual(rst, 'term_id not found!')

        async with aiohttp.delete(
                'http://127.0.0.1:8080/api/v1/terms/50') as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.exists('HS:TERM:50')
            self.assertFalse(rst)
            rst = self.redis_client.sismember('SET:TERM', 50)
            self.assertFalse(rst)

        terms = [10, 20, 30, 40]
        async with aiohttp.post('http://127.0.0.1:8080/api/v2/terms/del',
                                data=json.dumps(terms)) as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.smembers('SET:TERM')
            self.assertEqual(len(rst), 0)
コード例 #16
0
    async def test_formula_CRUD(self):
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/formulas') as r:
            self.assertEqual(r.status, 200)
            rst = await r.json()
            self.assertSequenceEqual(rst, ['1'])
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/formulas/1') as r:
            self.assertEqual(r.status, 200)
            rst = await r.json()
            self.assertDictEqual(rst, mock_data.formula1)
        async with aiohttp.get(
                'http://127.0.0.1:8080/api/v1/formulas/99') as r:
            self.assertEqual(r.status, 404)
            rst = await r.text()
            self.assertEqual(rst, 'formula_id not found!')

        async with aiohttp.post('http://127.0.0.1:8080/api/v1/formulas',
                                data=json.dumps(mock_data.test_formula)) as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.hgetall('HS:FORMULA:9')
            self.assertEqual(rst['formula'], mock_data.test_formula['formula'])
            rst = self.redis_client.sismember('SET:FORMULA', 9)
            self.assertTrue(rst)
        async with aiohttp.post('http://127.0.0.1:8080/api/v1/formulas',
                                data=json.dumps(mock_data.test_formula)) as r:
            self.assertEqual(r.status, 409)
            rst = await r.text()
            self.assertEqual(rst, 'formula already exists!')

        mock_data.test_formula['formula'] = '2+2'
        mock_data.test_formula['id'] = 5
        async with aiohttp.put('http://127.0.0.1:8080/api/v1/formulas/9',
                               data=json.dumps(mock_data.test_formula)) as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.exists('HS:FORMULA:9')
            self.assertFalse(rst)
            rst = self.redis_client.sismember('SET:FORMULA', 9)
            self.assertFalse(rst)
            rst = self.redis_client.hgetall('HS:FORMULA:5')
            self.assertEqual(rst['formula'], '2+2')
            rst = self.redis_client.sismember('SET:FORMULA', 5)
            self.assertTrue(rst)
        async with aiohttp.put('http://127.0.0.1:8080/api/v1/formulas/99',
                               data=json.dumps(mock_data.test_formula)) as r:
            self.assertEqual(r.status, 404)
            rst = await r.text()
            self.assertEqual(rst, 'formula_id not found!')

        async with aiohttp.delete(
                'http://127.0.0.1:8080/api/v1/formulas/5') as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.exists('HS:FORMULA:5')
            self.assertFalse(rst)
            rst = self.redis_client.sismember('SET:FORMULA', 5)
            self.assertFalse(rst)

        formula_check = {'formula': 'p1[-1]+10', 'p1': '1:10:1000'}
        async with aiohttp.post('http://127.0.0.1:8080/api/v1/formula_check',
                                data=json.dumps(formula_check)) as r:
            self.assertEqual(r.status, 200)
            rst = await r.text()
            self.assertEqual(rst, 'OK')
        formula_check['formula'] = 'p1[-1]+p2[-2]'
        async with aiohttp.post('http://127.0.0.1:8080/api/v1/formula_check',
                                data=json.dumps(formula_check)) as r:
            self.assertEqual(r.status, 200)
            rst = await r.text()
            self.assertEqual(
                rst, """NameError
   p1[-1]+p2[-2]
           ^^^
name 'p2' is not defined
""")

        formula_check['p1'] = '123:45:6'
        async with aiohttp.post('http://127.0.0.1:8080/api/v1/formula_check',
                                data=json.dumps(formula_check)) as r:
            self.assertEqual(r.status, 200)
            rst = await r.text()
            self.assertEqual(rst, "parameter not found: p1=123:45:6")

        formulas = [1]
        async with aiohttp.post('http://127.0.0.1:8080/api/v2/formulas/del',
                                data=json.dumps(formulas)) as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.smembers('SET:FORMULA')
            self.assertEqual(len(rst), 0)