-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
566 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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=$@ $< |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!", | ||
|
@@ -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", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .anko import Client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Oops, something went wrong.