Esempio n. 1
0
    def test_from_dict(self):
        api = Client(config=
            {'key': self.key, 'secret': self.secret, 'address': self.uri,
             'user': self.user, 'app_name': self.app_name}
            )

        self.assertTrue(isinstance(api, Client))
Esempio n. 2
0
 def test_query_id(self):
     api = Client(key=self.key, secret=self.secret, url=self.uri)
     result = api.query(query_id=self.query_id,
                        stream=False, response="json/compact")
     self.assertIsNotNone(result)
     self.assertNotEqual(result, {})
     self.assertEqual(type(len(json.loads(result)['object'])), type(1))
Esempio n. 3
0
 def test_token(self):
     api = Client(auth={"token": self.token},
                  address=self.uri,
                  config=ClientConfig(stream=False, response="json"))
     result = api.query(query=self.query)
     self.assertIsNotNone(result)
     self.assertTrue(len(json.loads(result)['object']) > 0)
Esempio n. 4
0
 def test_query_from_seven_days(self):
     api = Client(key=self.key, secret=self.secret, url=self.uri)
     result = api.query(query=self.query,
                        dates={'from': 'now()-7*day()', 'to': 'now()'},
                        stream=False, response="json")
     self.assertIsNotNone(result)
     self.assertEqual(len(json.loads(result)['object']), 1)
Esempio n. 5
0
 def _query_stream(self, response_type, result,
                   processor):
     client = Client(retries=0, config={'address': "URI", "stream": True,
                                        "response": response_type,
                                        "processor": processor})
     client._make_request = MagicMock(return_value=result)
     return client.query()
Esempio n. 6
0
 def _query_stream(self, response_type, result,
                   keepAliveToken=DEFAULT_KEEPALIVE_TOKEN):
     client = Client(retries=0, config={'address': "URI", "stream": True,
                                        "response": response_type,
                                        "keepAliveToken": keepAliveToken})
     client._make_request = MagicMock(return_value=result)
     return client.query()
Esempio n. 7
0
 def test_query_id(self):
     api = Client(auth={"key": self.key, "secret": self.secret},
                  address=self.uri,
                  config=ClientConfig(stream=False, response="json"))
     result = api.query(query_id=self.query_id)
     self.assertIsNotNone(result)
     self.assertNotEqual(result, {})
     self.assertEqual(type(len(json.loads(result)['object'])), type(1))
Esempio n. 8
0
 def test_query_from_seven_days(self):
     api = Client(auth={"key": self.key, "secret": self.secret},
                  address=self.uri,
                  config=ClientConfig(stream=False, response="json"))
     result = api.query(query=self.query,
                        dates={'from': 'now()-7*day()', 'to': 'now()'})
     self.assertIsNotNone(result)
     self.assertEqual(len(json.loads(result)['object']), 1)
Esempio n. 9
0
 def test_stream_query(self):
     api = Client(auth={"key": self.key, "secret": self.secret},
                  address=self.uri,
                  config=ClientConfig(response="json/simple"))
     result = api.query(query=self.query)
     self.assertTrue(isinstance(result, types.GeneratorType))
     result = list(result)
     self.assertEqual(len(result), 1)
Esempio n. 10
0
 def test_query_from_fixed_dates(self):
     api = Client(self.key, self.secret, self.uri)
     result = api.query(query=self.query,
                        dates={'from': strftime("%Y-%m-%d", gmtime()),
                              'to': strftime("%Y-%m-%d %H:%M:%S", gmtime())},
                        stream=False, response="json")
     self.assertIsNotNone(result)
     self.assertEqual(len(json.loads(result)['object']), 1)
Esempio n. 11
0
    def __init__(self,
                 profile='default',
                 api_key=None,
                 api_secret=None,
                 end_point=None,
                 oauth_token=None,
                 jwt=None,
                 credential_path=None,
                 timeout=None,
                 retries=1,
                 verify=True,
                 user=None,
                 app_name=None,
                 **kwargs):

        self.profile = profile
        self.api_key = api_key
        self.api_secret = api_secret
        self.end_point = end_point
        self.oauth_token = oauth_token
        self.jwt = jwt

        if credential_path is None:
            self.credential_path = Path.home() / '.devo_credentials'
        else:
            self.credential_path = Path(credential_path).expanduser().resolve()

        if not (self.end_point and (self.oauth_token or self.jwt or
                                    (self.api_key and self.api_secret))):
            self._read_profile()

        if not (self.end_point and (self.oauth_token or self.jwt or
                                    (self.api_key and self.api_secret))):
            raise Exception(
                'End point and either API keys or OAuth Token must be specified or in ~/.devo_credentials'
            )

        config = kwargs
        config.update({
            'auth': {
                'key': self.api_key,
                'secret': self.api_secret,
                'token': self.oauth_token,
                'jwt': self.jwt
            },
            'address': self.end_point
        })
        self.client = Client(config=config)

        self.client.timeout = timeout
        self.client.retries = retries
        self.client.verify = verify

        if user:
            self.client.config.set_user(user)
        if app_name:
            self.client.config.set_app_name(app_name)
Esempio n. 12
0
 def test_pragmas_not_comment_free(self):
     """Test the api when the pragma comment.free is not used"""
     api = Client(key=self.key, secret=self.secret, url=self.uri,
                  user=self.user, app_name=self.app_name, stream=False)
     result = api.query(
         query=self.query,
         response="json")
     self.assertIsNotNone(result)
     self.assertEqual(len(json.loads(result)['object']), 1)
Esempio n. 13
0
    def test_query(self):
        config = ClientConfig(stream=False, response="json")

        api = Client(auth={"key": self.key, "secret": self.secret},
                     address=self.uri,
                     config=config)

        result = api.query(query=self.query)
        self.assertIsNotNone(result)
        self.assertTrue(len(json.loads(result)['object']) > 0)
Esempio n. 14
0
 def test_query_from_fixed_dates(self):
     api = Client(auth={"key": self.key, "secret": self.secret},
                  address=self.uri,
                  config=ClientConfig(stream=False, response="json"))
     result = api.query(query=self.query,
                        dates={'from': strftime("%Y-%m-%d", gmtime()),
                               'to': strftime(
                                   "%Y-%m-%d %H:%M:%S",
                                   gmtime())})
     self.assertIsNotNone(result)
     self.assertEqual(len(json.loads(result)['object']), 1)
Esempio n. 15
0
 def test_pragmas(self):
     """Test the api when the pragma comment.free is used"""
     api = Client(auth={"key": self.key, "secret": self.secret},
                  address=self.uri,
                  config=ClientConfig(response="json",
                                      stream=False))
     api.config.set_user(user=self.user)
     api.config.set_app_name(app_name=self.app_name)
     result = api.query(query=self.query, comment=self.comment)
     self.assertIsNotNone(result)
     self.assertEqual(len(json.loads(result)['object']), 1)
Esempio n. 16
0
 def test_query_yesterday_to_today(self):
     api = Client(self.key, self.secret, self.uri)
     result = api.query(query=self.query,
                        dates={
                            'from': 'yesterday()',
                            'to': 'today()'
                        },
                        stream=False,
                        response="json")
     self.assertIsNotNone(result)
     self.assertEqual(len(json.loads(result)['object']), 1)
Esempio n. 17
0
 def _query_no_stream(self, response_type, result,
                      keepAliveToken=DEFAULT_KEEPALIVE_TOKEN):
     client = Client(retries=0, config={'address': "URI", "stream": False,
                                        "response": response_type,
                                        "keepAliveToken": keepAliveToken})
     with mock.patch(
             'devo.api.Client._make_request') as patched_make_request:
         if isinstance(result, str):
             patched_make_request.return_value.text = result
         else:
             patched_make_request.return_value.content = result
         return client.query()
Esempio n. 18
0
 def test_stream_query_no_results_bounded_dates(self):
     api = Client(auth={
         "key": self.key,
         "secret": self.secret
     },
                  address=self.uri,
                  config=ClientConfig(response="json/simple"),
                  retries=3)
     result = api.query(query=self.query_no_results,
                        dates={
                            'from': '1h',
                            'to': 'now()'
                        })
     self.assertTrue(isinstance(result, types.GeneratorType))
     result = list(result)
     self.assertEqual(len(result), 0)
Esempio n. 19
0
 def setUp(self):
     self.client = Client(
         config={
             'key':
             os.getenv('DEVO_API_KEY', None),
             'secret':
             os.getenv('DEVO_API_SECRET', None),
             'address':
             os.getenv('DEVO_API_ADDRESS', 'https://apiv2-us.devo.com/'),
             "stream":
             False,
             "destination": {
                 "type": "donothing",
                 "params": {
                     "friendlyName": "devo-sdk-api-test"
                 }
             }
         })
Esempio n. 20
0
 def test_stream_query_no_results_unbounded_dates(self):
     api = Client(auth={
         "key": self.key,
         "secret": self.secret
     },
                  address=self.uri,
                  config=ClientConfig(response="json/simple"),
                  retries=3)
     result = api.query(query=self.query_no_results)
     self.assertTrue(isinstance(result, types.GeneratorType))
     try:
         with stopit.ThreadingTimeout(3) as to_ctx_mgr:
             result = list(result)
     except DevoClientException:
         # This exception is sent because
         # devo.api.client.Client._make_request catches the
         # stopit.TimeoutException, but the latter is not
         # wrapped, so we cannot obtain it from here.
         self.assertEqual(to_ctx_mgr.state, to_ctx_mgr.TIMED_OUT)
Esempio n. 21
0
    def test_unsecure_http_query(self):
        """
        This test is intended for checking unsecure HTTP requests. Devo will NEVER provide an unsecure HTTP endpoint
        for API REST services. Therefore, you are not going to need to use or test this functionality.
        In order to enable UNSECURE_HTTP environment var should be TRUE.
        The endpoint is served by https://httpbin.org/. You can run with `docker run -p 80:80 kennethreitz/httpbin`. It
        will expose an HTTP service at port 80. The URL `http://localhost:80/anything` will answer with the content of
        the request.
        """
        os.environ["UNSECURE_HTTP"] = "TRUE"
        config = ClientConfig(stream=False, response="json")

        api = Client(auth={
            "key": self.key,
            "secret": self.secret
        },
                     address="localhost:80/anything",
                     config=config,
                     retries=3)

        result = api.query(query=self.query)
        self.assertIsNotNone(result)
        self.assertIn('json', json.loads(result))
        self.assertIn('query', json.loads(result)['json'])
Esempio n. 22
0
    def __init__(self,
                 profile='default',
                 api_key=None,
                 api_secret=None,
                 end_point=None,
                 oauth_token=None,
                 jwt=None,
                 credential_path=None):

        self.profile = profile
        self.api_key = api_key
        self.api_secret = api_secret
        self.end_point = end_point
        self.oauth_token = oauth_token
        self.jwt = jwt

        if credential_path is None:
            self.credential_path = Path.home() / '.devo_credentials'
        else:
            self.credential_path = Path(credential_path).resolve().expanduser()

        if not (self.end_point and (self.oauth_token or self.jwt or
                                    (self.api_key and self.api_secret))):
            self._read_profile()

        if not (self.end_point and (self.oauth_token or self.jwt or
                                    (self.api_key and self.api_secret))):
            raise Exception(
                'End point and either API keys or OAuth Token must be specified or in ~/.devo_credentials'
            )

        self.client = Client(auth=dict(key=self.api_key,
                                       secret=self.api_secret,
                                       token=self.oauth_token,
                                       jwt=self.jwt),
                             address=self.end_point)
Esempio n. 23
0
import os
from devo.api import Client, ClientConfig, JSON

key = os.getenv('DEVO_API_KEY', None)
secret = os.getenv('DEVO_API_SECRET', None)

api = Client(auth={
    "key": key,
    "secret": secret
},
             address="https://apiv2-eu.devo.com/search/query",
             config=ClientConfig(response="json", processor=JSON))

response = api.query(query="from demo.ecommerce.data select * limit 20",
                     dates={
                         'from': "today()-1*day()",
                         'to': "today()"
                     })

print(response)
Esempio n. 24
0
import os
from devo.api import Client, ClientConfig, TO_BYTES

key = os.getenv('DEVO_API_KEY', None)
secret = os.getenv('DEVO_API_SECRET', None)

api = Client(auth={
    "key": key,
    "secret": secret
},
             address="https://apiv2-eu.devo.com/search/query",
             config=ClientConfig(response="csv",
                                 stream=True,
                                 processor=TO_BYTES))

response = api.query(query="from demo.ecommerce.data select * limit 20",
                     dates={
                         'from': "today()-1*day()",
                         'to': "today()"
                     })

with open("example_data/example.csv", "wb") as f:
    try:
        for item in response:
            f.write(item)
            f.write(b"\n")
    except Exception as error:
        print(error)
Esempio n. 25
0
 def test_stream_query(self):
     api = Client(self.key, self.secret, self.uri)
     result = api.query(query=self.query, response="json/simple")
     self.assertTrue(isinstance(result, Buffer))
     api.close()
import os
from devo.api import Client, ClientConfig, SIMPLECOMPACT_TO_OBJ
from devo.sender import Sender, SenderConfigSSL

key = os.getenv('DEVO_API_KEY', None)
secret = os.getenv('DEVO_API_SECRET', None)

api = Client(auth={
    "key": key,
    "secret": secret
},
             address="https://apiv2-eu.devo.com/search/query",
             config=ClientConfig(response="json/simple/compact",
                                 stream=True,
                                 processor=SIMPLECOMPACT_TO_OBJ))

response = api.query(query="from demo.ecommerce.data select *",
                     dates={
                         'from': "today()-1*day()",
                         'to': "today()"
                     })

server = "us.elb.relay.logtrust.net"
port = 443
key = os.getenv('DEVO_SENDER_KEY')
cert = os.getenv('DEVO_SENDER_CERT')
chain = os.getenv('DEVO_SENDER_CHAIN')

engine_config = SenderConfigSSL(address=(server, port),
                                key=key,
                                cert=cert,
Esempio n. 27
0
 def test_query(self):
     api = Client(key=self.key, secret=self.secret, url=self.uri)
     result = api.query(query=self.query, stream=False, response="json")
     self.assertIsNotNone(result)
     self.assertTrue(len(json.loads(result)['object']) > 0)
Esempio n. 28
0
 def test_token(self):
     api = Client(token=self.token, url=self.uri)
     result = api.query(query=self.query, stream=False, response="json")
     self.assertIsNotNone(result)
     self.assertTrue(len(json.loads(result)['object']) > 0)
Esempio n. 29
0
 def test_stream_query(self):
     api = Client(key=self.key, secret=self.secret, url=self.uri)
     result = api.query(query=self.query, response="json/simple")
     self.assertTrue(isinstance(result, types.GeneratorType))
     result = list(result)
     self.assertEqual(len(result), 1)