I need help with Currency calculator and view in browser from live rever

Hello to everyone,
I want help about a task programming.

I am newbie in Python and from a Company send me a task to solve it.

So I am begginer in Python language and I have learned the basic and work from live server and view in browser.

Application Programming Task

The task is to implement an API that will have retrieve, store and expose currency exchange rates. All endpoints should use a database of your choice as a persistence layer. The following endpoints are needed: - POST - /register (No Auth needed) This endpoint should accept username, password, confirmPassword in JSON format. After validating the input it should create and persist a user in the DB. - POST - /login (No Auth needed) This endpoint should accept username, password in JSON format. After validating the input and checking that user exists and password is correct should return a Json Web Token (JWT) that should
be used to communicate with the other 2 endpoints of the API. - POST - /seed (Needs Authentication) This endpoint should use a free currency API of your choice. Keep in mind that you will need to potentially register with this API to use it and also respect potential request rate limits. When called it should retrieve the exchange rates for all currencies with EUR, USD, CHF andGBP as base currency respectively. e.g. {base: EUR, currencies: [{ EUR: 1, USD: 0.86, CAD: 1.23, SGD: 2.7 … }} // Base EUR {base: USD, currencies: [{ EUR: 1.23, USD: 1, CAD: 0.67, SGD: 8.2 … }} // Base USD … The exchange rates should then be persisted in the database using a schema that makes
sense. - GET - /rates/{base} (Needs Authentication) This endpoint should return all exchange rate from the database in both JSON and XML format. It should support pagination and sorting alphabetically or by exchange rate. - GET - /rates/{base}/{target} (Needs Authentication)
Should return the particular exchange rate. - POST - /rates/{base}/{target} (Needs Authentication) Should create / update the particular exchange rate. In order to be able to discover, test and understand the API make sure it utilizes the OpenAPISpecification (Swagger) which will automatically create a UI interface that will serve as API documentation and testing bed.

Ι’m not sure if i started right and I am trying many hours but I would like your help.
Thanks in advance

Below is the code:

print “Content-type:text/html”
print “”

import requests
import getpass
import json
import argparse
from datetime import date
import time

def callAPI(sourcelist, sourcecurrency, destcurrency):

jsonlist = [] 

APIKEY=getpass.getpass(prompt="ccc0b100-8a55-11ec-8276-b71d1ddd6a9b (hidden)")
URLBASE='https://freecurrencyapi.net/dashboard'
    for date in sourcelist:
    
            url=f'{URLBASE}?access_key={APIKEY}&date={date}&currencies={sourcecurrency},{destcurrency}&format=1c'
            convert=requests.get(url)
            jsonlist.append(convert.json())
            time.sleep(1) 

return(jsonlist)

print("Exchange Rates via 'https://freecurrencyapi.net/’")

Hi @aristotelisagoratsios, and welcome!

I have a few doubts about your issue.

print “Content-type:text/html”

print("Exchange Rates via 'https://freecurrencyapi.net/’")

Are you running Python 2 or 3?

Also, to facilitate the work, please move all the code to a Markdown code block, like this:

```
{add your Python code here}
```

Hello Diego, welcome Ι found you!
I am running Python 3.10 and as I know you can run it in a live webserver and probably view in a browser and save it as *.py file(correct me if I am wrong).
So you can run it from public_html in live webserver and this is the way to run python and view in chrome :

#! /usr/bin/python
print “Content-type:text/html”
print “Hello world”

I am beginning to solve the task and I posted here to take a look and correct me.
Sorry about the wrong place added python code.
Thank you
Sincerely
Aristotelis

Ok, here goes the first point: In Python 3, print is a function (like input, and others), not a statement (like for, while, yield, etc.). So, you have to replace these lines:

print "Hello world"
print "Some other stuff?"

with these others:

print("Hello world")
print("Some other stuff")

I don’t know about doing that (I always run my code from a cmd prompt), but as far as I know, if you want to run your code with Python 3, you may have to replace /usr/bin/python with /usr/bin/python3.

Hello again Diego,
thanks a lot about your advises and for answers!
I send you a link with my project builded and created by Python 3 which is Mastermind Game:
Mastermind Game
which runs in live web server and of course in browser,
and here you can take a look the code: [Mastermind Game code (GitHub - aristotelisagoratsios/mastermind: A mini project Mastermind Game, created and builded from Python Program Language!)
So just take a look if you want!
Anyway is there any solutions about my query with the task?
Thanks you
Sincerely
Aristotelis

Ok. I took a look at your Python game: I would point the same things than here. Just check that Python 3 don’t raise errors while running it in a local console.

Going back to your original issue, and fixing the print note I pointed:

print(“Content-type:text/html”)
print()  # I think you wanted a newline jump here?

import requests
import getpass
import json
import argparse
from datetime import date
import time

def callAPI(sourcelist, sourcecurrency, destcurrency):
    jsonlist = [] 
    APIKEY=getpass.getpass(prompt="ccc0b100-8a55-11ec-8276-b71d1ddd6a9b (hidden)")
    URLBASE='https://freecurrencyapi.net/dashboard'
    for date in sourcelist:
        url=f'{URLBASE}?access_key={APIKEY}&date={date}&currencies={sourcecurrency},{destcurrency}&format=1c'
        convert=requests.get(url)
        jsonlist.append(convert.json())
        time.sleep(1) 
    return(jsonlist)  # this could also be `return jsonlist`, because "return" is a statement, not a function!

print("Exchange Rates via 'https://freecurrencyapi.net/’")

Well, in a terminal, the program will only show this:

Content-type:text/html
Exchange Rates via 'https://freecurrencyapi.net/’

As far as I can see, you wanna run what’s inside the callAPI function. But, for Python, you never ran that. Maybe if you add this line to the code:

print(callAPI())  # Run `callAPI()` and print what it returns inmediately

The output obtained from the request must be shown.

Hello Diego again about your response!
I have fix your point and change the code as you say but there is no result in web server and in browser!
So the code is :

#!/usr/bin/python

print (“Content-type:text/html”)
print () # I think you wanted a newline jump here?

import requests
import getpass
import json
import argparse
from datetime import date
import time

def callAPI(sourcelist, sourcecurrency, destcurrency):
jsonlist =
APIKEY=getpass.getpass(prompt=“ccc0b100-8a55-11ec-8276-b71d1ddd6a9b (hidden)”)
URLBASE=‘https://freecurrencyapi.net/dashboard’
for date in sourcelist:
url=f’{URLBASE}?access_key={APIKEY}&date={date}&currencies={sourcecurrency},{destcurrency}&format=1c’
convert=requests.get(url)
jsonlist.append(convert.json())
time.sleep(1)
return(jsonlist) # this could also be return jsonlist, because “return” is a statement, not a function!

print(“Exchange Rates via 'https://freecurrencyapi.net/’”)

print(callAPI()) # Run callAPI() and print what it returns inmediately

And here is the screenshot from browser(Firefox) :

Anyway I will search and check it again!
Thanks a lot!
Sincerely
Aristotelis

1 Like