0

I'm getting missing 1 required positional argument on the code below. I tried different stuffs but no luck. I believe it is a small detail that I'm missing but since I'm a beginner on Python and I'm not being able to fix it myself.

import requests
from flask import Flask, jsonify, request, render_template
import os

app = Flask(__name__)

    @app.route('/outages')
    def map_status(data):
    
      url = "https://my-api.local.com/data"
    
      response = requests.get(url)
    
      result_data = json.loads(response.text)
      if(data['install_status']=='1'):
        data['install_status']='Installed'
      elif(data['install_status']=='3'):
            data['install_status']='Maintenance'
      return data
    
      result_data['result']=map(map_status,result_data['result'])
    
      return(list(result_data['result']))

This is the error I'm getting: [ERR] TypeError: map_status() missing 1 required positional argument: 'data'

If I change the return for print, it works, but on this case I need to use return to get the data.

rrudnicki
  • 13
  • 1
  • 9
  • 1
    you're calling a flask endpoint function. Are you sure that's what you want to do? – M Z Jan 05 '21 at 00:20
  • 1
    It's most definitely not the `return` that has the problem. – Charles Duffy Jan 05 '21 at 00:20
  • How are you calling this function `map_status`? – Hubert Grzeskowiak Jan 05 '21 at 00:21
  • 1
    ...well, _one_ call is the recursive one from the second-to-last line. The other way it's going to be called is from Flask when it's routing incoming requests. Mind, it's a bad idea to try to use a single function for both of those purposes; it really should be _either_ a web-service endpoint _or_ application logic, not both same things at the same time. – Charles Duffy Jan 05 '21 at 00:21
  • 2
    Ugh, this looks very wrong. You shouldn't call a function that is annotated with `app.route` unless you know exactly what you are doing. – Hubert Grzeskowiak Jan 05 '21 at 00:23
  • Sorry guys, I forgot to mention about flask.. I've updated the code – rrudnicki Jan 05 '21 at 00:27
  • It seems map_status recursively calling itself at map function where the map_status is not yet finished it block. Calling a unfinished function may cause of this error. – testuser Jan 05 '21 at 00:46
  • Does this answer your question? [How to get POSTed JSON in Flask?](https://stackoverflow.com/questions/20001229/how-to-get-posted-json-in-flask) – Vasili Syrakis Jan 05 '21 at 00:47

1 Answers1

1

When you call a function that has arguments, you must specify the value of them. The correct code would be:

result_data['result'] = map(map_status(insert_data), result_data['result'])

where (insert_data) is the place where you would insert the value of whatever needs to be put there.

If you don't want the argument to be required all the time you can specify an optional argument like so:

def map_status(data=None)

This makes the initial value of data a NoneType. When you want to call the function with the data argument, just do so like you would a normal argument, but with an equal sign, like so:

map_status(data="hello")

You can also just make the optional argument an empty literal:

def map_status(data="")

Shidouuu
  • 368
  • 4
  • 14