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

🐛 Bug Report: New functions are not created with the new structure 1.4.2. #6186

Closed
2 tasks done
Kriihz77 opened this issue Sep 7, 2023 · 5 comments
Closed
2 tasks done
Assignees
Labels
bug Something isn't working product / functions Fixes and upgrades for the Appwrite Functions.

Comments

@Kriihz77
Copy link

Kriihz77 commented Sep 7, 2023

👟 Reproduction steps

Through the CLI, a function was created in Python 3.10, and it created the old structure of the functions and not the new one proposed for version 1.4.x.

👍 Expected behavior

from appwrite.client import Client
import os


# This is your Appwrite function
# It's executed each time we get a request
def main(context):
    # Why not try the Appwrite SDK?
    #
    # client = (
    #     Client()
    #         .set_endpoint("https://cloud.appwrite.io/v1")
    #         .set_project(os.environ["APPWRITE_FUNCTION_PROJECT_ID"])
    #         .set_key(os.environ["APPWRITE_API_KEY"])
    # )

    # You can log messages to the console
    context.log("Hello, Logs!")

    # If something goes wrong, log an error
    context.error("Hello, Errors!")

    # The `context.req` object contains the request data
    if context.req.method == "GET":
        # Send a response with the res object helpers
        # `context.res.send()` dispatches a string back to the client
        return context.res.send("Hello, World!")

    # `context.res.json()` is a handy helper for sending JSON
    return context.res.json({
        "motto": "Build Fast. Scale Big. All in One Place.",
        "learn": "https://appwrite.io/docs",
        "connect": "https://appwrite.io/discord",
        "getInspired": "https://builtwith.appwrite.io",
    })

👎 Actual Behavior

from appwrite.client import Client

# You can remove imports of services you don't use
from appwrite.services.account import Account
from appwrite.services.avatars import Avatars
from appwrite.services.databases import Databases
from appwrite.services.functions import Functions
from appwrite.services.health import Health
from appwrite.services.locale import Locale
from appwrite.services.storage import Storage
from appwrite.services.teams import Teams
from appwrite.services.users import Users

"""
  'req' variable has:
    'headers' - object with request headers
    'payload' - request body data as a string
    'variables' - object with function variables

  'res' variable has:
    'send(text, status)' - function to return text response. Status code defaults to 200
    'json(obj, status)' - function to return JSON response. Status code defaults to 200

  If an error is thrown, a response with code 500 will be returned.
"""

def main(req, res):
  client = Client()

  # You can remove services you don't use
  account = Account(client)
  avatars = Avatars(client)
  database = Databases(client)
  functions = Functions(client)
  health = Health(client)
  locale = Locale(client)
  storage = Storage(client)
  teams = Teams(client)
  users = Users(client)

  if not req.variables.get('APPWRITE_FUNCTION_ENDPOINT') or not req.variables.get('APPWRITE_FUNCTION_API_KEY'):
    print('Environment variables are not set. Function cannot use Appwrite SDK.')
  else:
    (
    client
      .set_endpoint(req.variables.get('APPWRITE_FUNCTION_ENDPOINT', None))
      .set_project(req.variables.get('APPWRITE_FUNCTION_PROJECT_ID', None))
      .set_key(req.variables.get('APPWRITE_FUNCTION_API_KEY', None))
      .set_self_signed(True)
    )
  
  return res.json({
    "areDevelopersAwesome": True,
  })

additional, an error is being generated when consuming the import

Traceback (most recent call last):
  File "/usr/local/server/src/server.py", line 165, in handler
    output = await asyncio.wait_for(execute(context), timeout=safeTimeout)
  File "/usr/local/lib/python3.10/asyncio/tasks.py", line 445, in wait_for
    return fut.result()
  File "/usr/local/server/src/server.py", line 150, in execute
    userModule = importlib.import_module("function." + userPath)
  File "/usr/local/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/usr/local/server/src/function/src/index.py", line 1, in <module>
    from appwrite.client import Client
ModuleNotFoundError: No module named 'appwrite'

🎲 Appwrite version

Version 1.4.x

💻 Operating system

Linux

🧱 Your Environment

1.4.2 and CLI 3.0.0 Branch Master

👀 Have you spent some time to check if this issue has been raised before?

  • I checked and didn't find similar issue

🏢 Have you read the Code of Conduct?

@Kriihz77 Kriihz77 added the bug Something isn't working label Sep 7, 2023
@stnguyen90
Copy link
Contributor

@criihz, thanks for creating this issue! 🙏🏼 How exactly are you using the CLI? We don't have a working version of the CLI for Appwrite 1.4 yet 🧐

@stnguyen90 stnguyen90 self-assigned this Sep 7, 2023
@Kriihz77
Copy link
Author

Kriihz77 commented Sep 8, 2023

I am currently running the following:


appwrite init function
? What would you like to name your function? EquipoBase
? What ID would you like to have for your function? unique()
? What runtime would you like to use? Python (python-3.10)
✓ Success

And this I understand that the function creates for me with the previous version of the server 1.3.x.

But I am using the CLI version 3.0.0 with setting to create and deploy the function, I understand that this CLI is not generating the function with the new structure.

Take the example found in the documentation and update my function, add the compile command "pip install -r .....txt" and run the function, but it still gives me an error:


Traceback (most recent call last):
  File "/usr/local/server/src/server.py", line 165, in handler
    output = await asyncio.wait_for(execute(context), timeout=safeTimeout)
  File "/usr/local/lib/python3.10/asyncio/tasks.py", line 445, in wait_for
    return fut.result()
  File "/usr/local/server/src/server.py", line 150, in execute
    userModule = importlib.import_module("function." + userPath)
  File "/usr/local/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'function.index'

This is my code function:

from appwrite.client import Client
import os


# This is your Appwrite function
# It's executed each time we get a request
def main(context):
    # Why not try the Appwrite SDK?
    #
    # client = (
    #     Client()
    #         .set_endpoint("https://cloud.appwrite.io/v1")
    #         .set_project(os.environ["APPWRITE_FUNCTION_PROJECT_ID"])
    #         .set_key(os.environ["APPWRITE_API_KEY"])
    # )

    # You can log messages to the console
    context.log("Hello, Logs!")

    # If something goes wrong, log an error
    context.error("Hello, Errors!")

    # The `context.req` object contains the request data
    if context.req.method == "GET":
        # Send a response with the res object helpers
        # `context.res.send()` dispatches a string back to the client
        return context.res.send("Hello, World!")

    # `context.res.json()` is a handy helper for sending JSON
    return context.res.json({
        "motto": "Build Fast. Scale Big. All in One Place.",
        "learn": "https://appwrite.io/docs",
        "connect": "https://appwrite.io/discord",
        "getInspired": "https://builtwith.appwrite.io",
    })

@stnguyen90
Copy link
Contributor

The CLI doesn't work yet so I'm not sure if using it is a good idea.

Anyways, you might want to make sure the entrypoint in your function settings matches what the path of the code is.

@Kriihz77
Copy link
Author

Kriihz77 commented Sep 8, 2023

@stnguyen90 Thanks,

You are right, apparently the template that is used is not yet updated and the ones generated from the previous version do not work correctly, I tried with several entry points and I still cannot get the function to execute correctly.

@stnguyen90
Copy link
Contributor

@Kriihz77, btw, version 4.0.0 of the CLI has been released and should use the new templates correctly.

@stnguyen90 stnguyen90 added the product / functions Fixes and upgrades for the Appwrite Functions. label Sep 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working product / functions Fixes and upgrades for the Appwrite Functions.
Projects
None yet
Development

No branches or pull requests

2 participants