earthcubeprojects-chords / chords

EarthCube CHORDS application code
GNU General Public License v2.0
25 stars 13 forks source link

Data Fetch feature via script is broken #605

Closed brilund closed 10 months ago

brilund commented 1 year ago

Running any kind of fetch on data works in a browser, but fail when ran in code (such as

http://3d-kenya.chordsrt.com/api/v1/data/16.csv?last
http://3d-kenya.chordsrt.com/api/v1/data/16?start=2023-07-19T17:00&end=2023-07-19T17:01
http://3d-kenya.chordsrt.com/api/v1/data/16?start=2023-07-19T17:00&end=2023-07-19T17:01

)

MisterMartin commented 1 year ago

Can you paste a snippet of the code? Most likely it has to do with authentication.

MisterMartin commented 1 year ago

An email and API key are required. It was probably working from the browser because you were logged into an account on the portal?

The following python works. Substitute your user_email and api_key:

import requests
from json import dumps
from json import loads

portal = 'http://3d-kenya.chordsrt.com'
inst_id='16'
user_email='REDACTED'
api_key='REDACTED'
start='2023-07-19T17:00'
end='2023-07-19T17:01'
url = f"{portal}/api/v1/data/{inst_id}?start={start}&end={end}&email={user_email}&api_key={api_key}"

response = requests.get(url=url)
all_fields = loads(dumps(response.json()))
data = all_fields['features'][0]['properties']['data']
print(data)

which prints the data variable containing multiple data points:

[{'time': '2023-07-19T17:00:05Z', 'test': 'false', 'measurements': {'t1': 19, 'rh1': 64.9, 'wd': 0, 't3': 18.9}}, {'time': '2023-07-19T17:00:09Z', 'test': 'false', 'measurements': {'sp1': 811.61, 't2': 19.6, 'msl1': 1000.97}}, {'time': '2023-07-19T17:00:38Z', 'test': 'false', 'measurements': {'rain': 0}}, {'time': '2023-07-19T17:00:55Z', 'test': 'false', 'measurements': {'ws': 0.58}}]

If you just want the most recent data, substitute the following for the url:

url = f"{portal}/api/v1/data/{inst_id}?last&email={user_email}&api_key={api_key}"

which prints:

[{'time': '2023-08-08T20:45:54Z', 'test': 'false', 'measurements': {'ws': 0}}]
brilund commented 1 year ago

This was the piece of code the user attempted:

import requests

r = requests.get('http://3d-kenya.chordsrt.com/api/v1/data/16.csv?last') print(r)

r = requests.get('http://3d-kenya.chordsrt.com/api/v1/data/16?start=2023-07-19T17:00&end=2023-07-19T17:01') print(r)

import urllib.request contents = urllib.request.urlopen("http://3d-kenya.chordsrt.com/api/v1/data/16?start=2023-07-19T17:00&end=2023-07-19T17:01").read()

MisterMartin commented 1 year ago

Has anyone tried out the code that I posted? Can we close this issue?

brilund commented 1 year ago

The piece of code you provided has not yet been tested.

brilund commented 10 months ago

Hi Charlie,

Sorry it took so long to respond, but we finally got a user to test this code and they said it worked, so thank you!

Cheers, Brianna

On Thu, Aug 10, 2023 at 5:16 PM Charlie Martin @.***> wrote:

Has anyone tried out the code that I posted? Can we close this issue?

— Reply to this email directly, view it on GitHub https://github.com/earthcubeprojects-chords/chords/issues/605#issuecomment-1674041273, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOM2YEJLAVSHYIB44NWQLHLXUVTVVANCNFSM6AAAAAA3HHNLHM . You are receiving this because you authored the thread.Message ID: @.***>

MisterMartin commented 10 months ago

Terrific! I’ll close the issue.