This repository has been archived by the owner on May 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathinvestmentportfolio.py
213 lines (195 loc) · 7.7 KB
/
investmentportfolio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# Copyright 2015 IBM Corp. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import requests
import json
import argparse
from dotenv import load_dotenv
import os
import datetime
#Initalize Investment Portfolio Service credentials to find on Bluemix otherwise from .env file
if 'VCAP_SERVICES' in os.environ:
vcap_servicesData = json.loads(os.environ['VCAP_SERVICES'])
# Log the fact that we successfully found some service information.
print("Got vcap_servicesData\n")
#print(vcap_servicesData)
# Look for the IP service instance.
IP_W_username=vcap_servicesData['fss-portfolio-service'][0]['credentials']['writer']['userid']
IP_W_password=vcap_servicesData['fss-portfolio-service'][0]['credentials']['writer']['password']
IP_R_username=vcap_servicesData['fss-portfolio-service'][0]['credentials']['reader']['userid']
IP_R_password=vcap_servicesData['fss-portfolio-service'][0]['credentials']['reader']['password']
# Log the fact that we successfully found credentials
print("Got IP credentials\n")
else:
load_dotenv(os.path.join(os.path.dirname(__file__), ".env"))
IP_W_username=os.environ.get("CRED_PORTFOLIO_USERID_W")
IP_W_password=os.environ.get("CRED_PORTFOLIO_PWD_W")
IP_R_username=os.environ.get("CRED_PORTFOLIO_USERID_R")
IP_R_password=os.environ.get("CRED_PORTFOLIO_PWD_R")
def Get_Portfolios():
"""
Retreives portfolio data by calling the Investment Portfolio service
"""
print ("Get Portfolios")
#call the url
BASEURL = "https://investment-portfolio.mybluemix.net/api/v1/portfolios/"
headers = {
'accept': "application/json",
'content-type': "application/json"
}
get_data = requests.get(BASEURL, auth=(IP_R_username, IP_R_password), headers=headers)
print("Investment Portfolio status: " + str(get_data.status_code))
# return json data
data = get_data.json()
print(data)
return data
def Get_Portfolio_Holdings(Portfolio,latest=True):
"""
Retreives holdinga data from the Investment Portfolio service for the Portfolio
"""
print ("Get Portfolio Holdings for " + Portfolio)
#construct the url
BASEURL = "https://investment-portfolio.mybluemix.net/api/v1/portfolios/" + Portfolio + "/holdings"
if latest:
BASEURL += "?latest=true"
#call the url
headers = {
'accept': "application/json",
'content-type': "application/json"
}
get_data = requests.get(BASEURL, auth=(IP_R_username, IP_R_password), headers=headers)
print("Investment Portfolio - Get Portfolio Holdings status: " + str(get_data.status_code))
#return json data
data = get_data.json()
return data
def Get_Portfolios_by_Selector(selector,value):
"""
Retreives portfolio data by calling the Investment Portfolio service
"""
print ("Get Portfolios by Selector")
#call the url
BASEURL = "https://investment-portfolio.mybluemix.net/api/v1/portfolios/_find"
headers = {
'accept': "application/json",
'content-type': "application/json"
}
s = {
'dataSelector':{
selector:value
}
}
get_data = requests.post(BASEURL, auth=(IP_R_username, IP_R_password), headers=headers, data=json.dumps(s))
print("Investment Portfolio status: " + str(get_data.status_code))
# return json data
data = get_data.json()
return data
def Get_Holdings_by_Selector(portfolio,selector,value):
"""
Retreives portfolio data by calling the Investment Portfolio service
"""
print ("Get Portfolios by Selector")
#call the url
BASEURL = "https://investment-portfolio.mybluemix.net/api/v1/portfolios/" + portfolio + "/holdings/_find"
headers = {
'accept': "application/json",
'content-type': "application/json"
}
s = {
'dataSelector':{
str(selector):str(value)
}
}
get_data = requests.post(BASEURL, auth=(IP_R_username, IP_R_password), headers=headers, data=json.dumps(s))
print("Investment Portfolio status: " + str(get_data.status_code))
# return json data
data = get_data.json()
return data
def Create_Portfolio(Portfolio):
"""
Creates portfolio in the database.
"""
#construct the url
BASEURL = "https://investment-portfolio.mybluemix.net/api/v1/portfolios"
headers = {
'Content-Type': "application/json",
'Accept': "application/json"
}
get_data = requests.post(BASEURL, auth=(IP_W_username, IP_W_password), headers=headers, data=json.dumps(Portfolio))
#print the status and returned json
status = get_data.status_code
print("Investment Portfolio status: " + str(status))
if status != 200:
return get_data
else:
data = get_data.json()
return json.dumps(data, indent=4, sort_keys=True)
def Create_Portfolio_Holdings(portfolio_name,holdings):
"""
Creates portfolio holdings.
"""
timestamp = '{:%Y-%m-%dT%H:%M:%S.%fZ}'.format(datetime.datetime.now())
BASEURL = "https://investment-portfolio.mybluemix.net/api/v1/portfolios/" + portfolio_name + "/holdings"
headers = {
'Content-Type': "application/json",
'Accept': "application/json"
}
data = {
'timestamp': timestamp,
'holdings': holdings
}
get_data = requests.post(BASEURL, auth=(IP_W_username, IP_W_password), headers=headers, data=json.dumps(data))
#print the status and returned json
status = get_data.status_code
print("Investment Portfolio Holding status: " + str(status))
if status != 200:
return get_data
else:
data = get_data.json()
return json.dumps(data, indent=4, sort_keys=True)
def Delete_Portfolio(portfolio_name,timestamp,rev):
"""
Deletes a portfolio.
"""
BASEURL = "https://investment-portfolio.mybluemix.net/api/v1/portfolios/" + str(portfolio_name) + "/" + str(timestamp) + "?rev=" + str(rev)
headers = {
'Content-Type': "application/json",
'Accept': "application/json",
'Authorization':"Basic aGV5cmVsc2VuZG9udHJhdGlyc2VudWVuOjM4NDUzNTZjNzY2NTY4NTA0YjkyYzM3ZDJiOGVkZTkzZWYzMTg0NTA="
}
res = requests.delete(BASEURL, auth=(IP_W_username, IP_W_password), headers=headers)
#print the status and returned json
status = res.status_code
print("Investment Portfolio delete status: " + str(status))
if status != 200:
return res
else:
return "Portfolio " + portfolio_name + " deleted successfully."
def Delete_Portfolio_Holdings(portfolio_name,timestamp,rev):
"""
Deletes a portfolio.
"""
BASEURL = "https://investment-portfolio.mybluemix.net/api/v1/portfolios/" + str(portfolio_name) + "/holdings/" + str(timestamp) + "?rev=" + str(rev)
print(BASEURL)
headers = {
'Content-Type': "application/x-www-form-urlencoded",
'Accept': "application/json",
'authorization':'Basic REPLACE_BASIC_AUTH'
}
res = requests.delete(BASEURL, auth=(IP_W_username, IP_W_password), headers=headers)
#print the status and returned json
status = res.status_code
print("Investment Portfolio holdings delete status: " + str(status))
if status != 200:
return res
else:
return "Portfolio " + portfolio_name + " deleted successfully."