Skip to content
This repository has been archived by the owner on Sep 8, 2024. It is now read-only.

Feature/skill api #1822

Merged
merged 4 commits into from
Feb 23, 2021
Merged

Feature/skill api #1822

merged 4 commits into from
Feb 23, 2021

Commits on Feb 6, 2021

  1. Add skill api.

    The skill api allows skills to define a public api which can easily be
    accessed by other skills over the message bus just as easy as working
    with a normal object.
    
    The skill api object is generated from the skill's public_api property. It's a dict where each key is turned into a method on the api object. The method is defined as
      "api_method": {
        "type": message type string
        "func": handler method
        "help": help string for cli
      }
    
    Example skill:
    
    class Test2(MycroftSkill):
        def __init__(self):
            MycroftSkill.__init__(self)
            self.public_api = {
                'speak': {
                    'type': 't2.speak',
                    'func': self.handle_speak,
                    'help': 'speak the test sentence\nand another line\n\nlast'
                },
                'speak2': {
                    'type': 't2.speak2',
                    'func': self.handle_speak2,
                    'help': 'speak the other sentence'
                }
            }
    
        def handle_speak(self, message):
            self.speak('This is a test')
            self.bus.emit(message.response(data=None))
    
        def handle_speak2(self, message):
            self.speak('This is another test')
            self.bus.emit(message.response(data=None))
    forslund committed Feb 6, 2021
    Configuration menu
    Copy the full SHA
    df02bf2 View commit details
    Browse the repository at this point in the history
  2. Add skill_api_method decorator

    The api methods are now much easier to use, almost transparent. The
    current caveat is that only "standarad" python types are acceptable
    (int, float, str, list, bool, None) due to the json serialization.
    
    - api methods are now created with the skill_api_method decorator
    - both arguments and keyword arguments are sent to the api method
    instead of the message object
    - api methods now uses a normal return statement instead of having to
    handle creating response messages on the bus.
    
    For example if the datetime skill wants to make the datetime string
    fetchable simply add the skill_api_method decorator to the
    get_display_date method.
    
        @skill_api_method
        def get_display_date(self, day=None, location=None):
            """Returns the date and time as a string."""
            [...]
    
    The methods return value will be sent back to the caller and can be used
    from a skill through
    
            datetime = SkillApi.get('mycroft-date-time.mycroftai')
            self.log.info(datetime.get_display_date())
    forslund committed Feb 6, 2021
    Configuration menu
    Copy the full SHA
    7f3b4e1 View commit details
    Browse the repository at this point in the history
  3. Add skill api help to CLI

    forslund committed Feb 6, 2021
    Configuration menu
    Copy the full SHA
    1397659 View commit details
    Browse the repository at this point in the history

Commits on Feb 8, 2021

  1. Add test cases for the SkillApi

    - Tests for the MycroftSkill side implementation
    - Tests for the generated SkillApi objects
    forslund committed Feb 8, 2021
    Configuration menu
    Copy the full SHA
    5685259 View commit details
    Browse the repository at this point in the history