Coinbase Pro - Get Account Hold Pagination Requests - api

With reference to https://docs.pro.coinbase.com/#get-account-history
HTTP REQUEST
GET /accounts//holds
I am struggling to produce python code to get the account holds via API pagination request and I could not find any example of implementing it.
Could you please advise me on how to proceed with this one?

According to Coinbase Pro documentation, pagination works like so (example with the holds endpoint):
import requests
account_id = ...
url = f'https://api.pro.coinbase.com/accounts/{account_id}/holds'
response = requests.get(url)
assert response.ok
first_page = response.json() # here is the first page
cursor = response.headers['CB-AFTER']
response = requests.get(url, params={'after': cursor})
assert response.ok
second_page = response.json() # then the next one
cursor = response.headers['CB-AFTER']
# and so on, repeat until response.json() is an empty list
You should properly wrap this into helper functions or class, or, even better, use an existing library and save dramatic time.

Related

Kraken - Pull Futures Data ("Unknown Asset Pair")

So I can pull spot data from Kraken with:
import requests
url = 'https://api.kraken.com/0/public/OHLC?pair=XBTUSD'
resp = requests.get(url)
resp.json()
But when I try to pull Futures data I always get
{'error': ['EQuery:Unknown asset pair']}
What I've done currently: I take the url from this website, https://demo-futures.kraken.com/futures/FI_XBTUSD_220930, which is "FI_BTCUSD_220930":
url = 'https://api.kraken.com/0/public/OHLC?pair=FI_BTCUSD_220930'
resp = requests.get(url)
resp.json()
I've considered that it might be due to being unable to pull OHLC data for futures. Even when I try a more simple request like just to get info about the ticker I get the same error.
I've looked in the documetation for seperate rules for futures but can't find any reference to what to do differently for futures?

Can I get the most recent commit of a repository with Bitbucket API?

I am using Bitbucket API to retrieve different information. However I am looking to do a request that retrieves the latest commit for a repository. I initially thought it would be done like this:
https://bitbucket.org/!api/2.0/repositories/xxxx/xxxx/commits?limit=1
This just showed all the commits as normal but I want to show the most recent one. From looking through the API documentation I can't find anything that shows about limiting the number of commits to show. So was wondering if anyone could point me in the right direction?
Okay this wasn't straight forward either and I spent a couple of hours looking for how to do this myself. It ended up being easy if not unfortunate. Simply put the available API will not return just a target commit (like the latest). You have do parse it yourself. The following api:
"https://api.bitbucket.org/2.0/repositories/<project>/<repo>/commits/<branch>?limit=1"
Will still return ALL the commits for that particular branch BUT in order. So you can simply just grab the first result on the first page that is returned and that's the most recent commit for that branch. Here is a basic python example:
import os
import requests
import json
headers = {"Content-Type": "application/json"}
USER = ""
PASS = ""
def get_bitbucket_credentials():
global USER, PASS
USER = "<user>"
PASS = "<pass>"
def get_commits(project, repo, branch):
return json.loads(call_url("https://api.bitbucket.org/2.0/repositories/%s/%s/commits/%s?limit=1" % (project, repo, branch)))
def get_modified_files(url):
data = json.loads(call_url(url))
file_paths = []
for value in data["values"]:
file_paths.append(value["new"]["path"])
return file_paths
def call_url(url):
global USER, PASS
response = requests.get(url, auth=(USER, PASS), headers=headers)
if response.status_code == requests.codes.ok:
return response.text
return ""
if __name__ == "__main__":
get_bitbucket_credentials()
data = get_commits("<project>","<repo>","<branch>")
for item in data["values"]:
print("Author Of Commit: "+item["author"]["raw"])
print("Commit Message: "+item["rendered"]["message"]["raw"])
print("List of Files Changed:")
print(get_modified_files(item["links"]["diff"]["href"].replace("/diff/","/diffstat/")))
break
You can run the above example:
python3 mysavedfile.py
and that will output like:
Author Of Commit: Persons Name <personemail#email.com>
Commit Message: my commit message
List of Files Changed:
['file1.yaml','file2.yaml']
There a simple way to do it, using Bitbucket pagination mechanism.
Like so:
https://bitbucket.org/!api/2.0/repositories/xxxx/xxxx/commits?pagelen=1
Normally you'll need to specify the page number, "page=1", but the default is 1 so...

What data can I save from the spotify API?

I'm building a website and I'm using the Spotify API as a music library. I would like to add more filters and order options to search traks than the api allows me to so I was wondering what track/song data can I save to my DB from the API, like artist name or popularity.
I would like to save: Name, Artists, Album and some other stuff. Is that possible or is it against the terms and conditions?
Thanks in advance!
Yes, it is possible.
Data is stored in Spotify API in endpoints.
Spotify API endpoint reference here.
Each endpoint deals with the specific kind of data being requested by the client (you).
I'll give you one example. The same logic applies for all other endpoints.
import requests
"""
Import library in order to make api calls.
Alternatively, ou can also use a wrapper like "Spotipy"
instead of requesting directely.
"""
# hit desired endpoint
SEARCH_ENDPOINT = 'https://api.spotify.com/v1/search'
# define your call
def search_by_track_and_artist(artist, track):
path = 'token.json' # you need to get a token for this call
# endpoint reference page will provide you with one
# you can store it in a file
with open(path) as t:
token = json.load(t)
# call API with authentication
myparams = {'type': 'track'}
myparams['q'] = "artist:{} track:{}".format(artist,track)
resp = requests.get(SEARCH_ENDPOINT, params=myparams, headers={"Authorization": "Bearer {}".format(token)})
return resp.json()
try it:
search_by_track_and_artist('Radiohead', 'Karma Police')
Store the data and process it as you wish. But you must comply with Spotify terms in order to make it public.
sidenote: Spotipy docs.

How to get the response content in selenium?

When I open the URL with driver.get(url), how can I get the response content of the page? Please refer to the image for more information.
In a separate post I saw this answer. As per it there is a ticket opened for Selenium.
I'm using Python and Django, but it's actually simple to get the response. I'm using a StaticLiveServerTestCase as my base test for the test. The .get() method on self.client actually returns the response itself. For example:
response = self.client.get(url)
However, it looks like what you're really trying to get is the cookie based on what you're pointing to in the picture. I use Django and the Django test suite to authenticate a user session to be used in the test.
def create_pre_authenticated_session(self, username, url="/"):
user = User.objects.create(username=username)
session = SessionStore()
session[SESSION_KEY] = user.pk
session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0]
session[HASH_SESSION_KEY] = user.get_session_auth_hash()
session.save()
# to set a cookie we need to first visit the domain.
# 404 pages load the quickest!
self.browser.get(self.live_server_url + '/404_no_such_url/')
self.browser.add_cookie(dict(
name=settings.SESSION_COOKIE_NAME,
value=session.session_key,
secure=False,
path='/',
))
self.browser.get(self.live_server_url + url)
return user
This has some other stuff in it that I borrowed from Percival's Test-Driven Development with Python, but I hope that it can provide some guidance on what you're trying to accomplish.

How to pass parameters in post call in pentaho-spoon?

I have made an api and I want to access a post call in it. I made the following transformation in kettle:
with a params field in Generate Rows step as:
and REST Client step configuration as:
but I am unable to get any of the parameters in my post call on server side. If I write a simple post call in python as:
import requests
url = "http://10.131.70.73:5000/searchByLatest"
payload = {'search_query': 'donald trump', 'till_date': 'Tuesday, 7 June 2016 at 10:40'}
r = requests.post(url, params=payload)
print(r.text)
print(r.status_code)
I am able to get the parameters by request.args.get("search_query") on the client side in Flask. How can I make an equivalent POST call in kettle?
I found the solution myself eventually. Describe the fields in generate rows as:
and in the parameters tab in REST Client step, we should get the same fields:
Works perfect!