Skip to content

Commit

Permalink
Provide forecasts client
Browse files Browse the repository at this point in the history
  • Loading branch information
jspc committed Dec 1, 2021
1 parent af7fe2d commit eba2bba
Show file tree
Hide file tree
Showing 8 changed files with 566 additions and 3 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DOCUMENTATION_CHECKOUT ?= ../documentation

src/anko/proto: $(DOCUMENTATION_CHECKOUT)/proto/gateway.proto
mkdir -p $@
python -m grpc_tools.protoc -I $(dir $<) --python_out=$@ --grpc_python_out=$@ $<
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,29 @@
# Anko Investor Python SDK

This module provides a simple Anko Investor Forecasts gRPC Service client for python.

This module does little more wrap [grpc](https://pypi.org/project/grpcio/) with retry logic and authorization wrappers.

```
$ pip install anko-sdk
```

## Usage

Given a valid token from https://anko-investor.com (see: [Getting Started](https://github.com/anglo-korean/documentation#getting-started) for more information), the following example will start consuming Forecasts

```python
import os
import socket

from anko import Client

c = Client(os.environ.get('ANKO_TOKEN'), socket.gethostname())

for forecast in c:
if forecast:
print(forecast)

```

(Here we use the current machine's hostname as a client identifier- this can be anything, really; it's useful to set in case you need to open a support ticket to help debug connections. It can even be an empty string).
12 changes: 9 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import os
import setuptools

with open("README.md", "r", encoding="utf-8") as fh:
long_description = fh.read()

ver = os.environ.get('VERSION')

setuptools.setup(
name="anko-python",
version="1.0.0",
name="anko-sdk",
version=ver,
author="Anglo Korean",
author_email="[email protected]",
description="Python SDK for https://anko-investor.com market forecasts. Signup today!",
Expand All @@ -21,6 +24,9 @@
"Operating System :: OS Independent",
],
package_dir={"": "src"},
packages=setuptools.find_packages(where="src"),
packages=["anko", "anko.proto"],
python_requires=">=3.6",
install_requires=[
"grpcio",
],
)
1 change: 1 addition & 0 deletions src/anko/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .anko import Client
69 changes: 69 additions & 0 deletions src/anko/anko.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env python

import grpc

from .proto import gateway_pb2_grpc, gateway_pb2

addr = "forecasts.anko-investor.com:443"
ua = "github.com/anglo-korean/anko-python-sdk#0.1.0"

class InvalidForecast(Exception):
super

class Client():
def __init__(self, token, ident):
'''
Create new connection
'''
self.auth = [('authorization', f'bearer {token}')]

m = gateway_pb2.Metadata()
m.ua = ua

t = gateway_pb2.Tag()
t.key = 'Identifier'
t.value = ident

m.tags.insert(0, t)
self.tags = m

self.__connect()


def __connect(self):
chan = grpc.secure_channel(addr, grpc.ssl_channel_credentials())

self.conn = gateway_pb2_grpc.ForecastsStub(chan)


def __reset_stream(self):
self.stream = self.conn.Stream(self.tags, metadata=self.auth)
self.count = 0


def __iter__(self):
self.__reset_stream()

return self


def __next__(self):
self.count += 1

try:
f = next(self.stream)
if self.count == 1:
if not f.id == 'dummy-forecast':
raise InvalidForecast('expected dummy forecast')

return

return f

except grpc.RpcError as e:
self.__reset_stream()

except Exception as e:
print(e)
print(type(e))
raise e
Empty file added src/anko/proto/__init__.py
Empty file.
Loading

0 comments on commit eba2bba

Please sign in to comment.