Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python/AppDev/app/myApi.py #5

Open
i4o opened this issue Oct 4, 2022 · 0 comments
Open

Python/AppDev/app/myApi.py #5

i4o opened this issue Oct 4, 2022 · 0 comments

Comments

@i4o
Copy link

i4o commented Oct 4, 2022

This script actually fails to search beyond first entry in db.txt.
I've wasted 15 mins figuring out what's wrong, instead of learning :).

As you can see below, if first record is match, OK found.
If 1st record is not match, it will return with "No match".
2nd record is never tried, function never continues looping, it returns after validation of 1st record...

for record in records:
                if record['hostname'] == hostname:
                    LOG.info('Routers returned')
                    return jsonify(record), 200
                if record['hostname'] != hostname:
                    LOG.warning('No matching router')
                    return jsonify({"response": "No match"}), 200

I did modify your script to work:

@app.route('/routers', methods=['GET'])
def getRouter():
    try:
        hostname = request.args.get('hostname')
        if (hostname is None) or (hostname == ""):
            LOG.warning('No hostname specified')
            raise ValueError
        with open(f'{script_dir}/db.txt', 'r') as f:
            data = f.read()
            records = json.loads(data)
            for record in records:
                print("Hostname: ", record['hostname'])
                if record['hostname'] == hostname:
                    LOG.info('Routers returned')
                    return jsonify(record), 200
                if record['hostname'] != hostname:
                    continue    
                    
            LOG.warning('No matching router')
            return jsonify({"response": "No match"}), 200
    except ValueError:
        LOG.error("NO HOSTNAME SPECIFIED")
        return jsonify({"error": "NO_HOSTNAME_SPECIFIED"}), 400
    except Exception as err:
        LOG.error(f'Error during GET {err}')
        return jsonify({"error": err}), 500
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant