TypeError:__init __()恰好接受1个参数(给定6个)

时间:2019-04-01 12:19:27

标签: python google-app-engine google-cloud-endpoints

致力于在本地构建Google CloudEndpoints API,了解消息的构造方式。该API将使admin SDK报告API activity:list方法可用,但受OU限制(对于使用G Suite的用户)。在包含一组重复消息的消息上遇到构造错误:

ERROR    2019-04-01 12:01:58,861 service.py:191] Encountered unexpected error from ProtoRPC method implementation: TypeError (__init__() takes exactly 1 argument (6 given))
Traceback (most recent call last):
  File "/Users/me/google-cloud-sdk/platform/google_appengine/lib/protorpc-1.0/protorpc/wsgi/service.py", line 181, in protorpc_service_app
    response = method(instance, request)
  File "/Users/me/Documents/GitHub/activitylogs_api/lib/endpoints/api_config.py", line 1351, in invoke_remote
    return remote_method(service_instance, request)
  File "/Users/me/google-cloud-sdk/platform/google_appengine/lib/protorpc-1.0/protorpc/remote.py", line 414, in invoke_remote_method
    response = method(service_instance, request)
  File "/Users/me/Documents/GitHub/activitylogs_api/main.py", line 142, in list_activities
    events=[event1,event2]
TypeError: __init__() takes exactly 1 argument (6 given)
INFO     2019-04-01 12:01:58,873 module.py:861] default: "GET /_ah/api/activitylogs/v1/list HTTP/1.1" 503 196

代码如下:

# [START imports]
import endpoints
from endpoints import message_types
from endpoints import messages
from endpoints import remote
from datetime import datetime
# [END imports]


# [START messages]
class Parameter(messages.Message):
    """contains parameters related to an event"""
    name = messages.StringField(1)
    value = messages.StringField(2)
    intValue = messages.IntegerField(3)
    boolValue = messages.BooleanField(4)


class Event(messages.Message):
    """contains the events in an item"""
    type = messages.StringField(1)
    name = messages.StringField(2)
    parameters = messages.MessageField(Parameter, 3, repeated=True)


class Actor(messages.Message):
    """contains details about the actor of the event"""
    callerType = messages.StringField(1)
    email = messages.StringField(2)
    profileId = messages.IntegerField(3)
    key = messages.StringField(4)


class Id(messages.Message):
    """Identifying attributes of the item"""
    time = message_types.DateTimeField(1)
    uniqueQualifier = messages.IntegerField(2)
    applicationName = messages.StringField(3)
    customerId = messages.StringField(4)


class Activity(messages.Message):
    """Represents on activity from the logs"""
    kind = messages.StringField(1) #always "audit#activity" READ_ONLY
    id = messages.MessageField(Id, 2)
    actor = messages.MessageField(Actor, 3)
    ownerDomain = messages.StringField(4)
    ipAddress = messages.StringField(5)
    events = messages.MessageField(Event, 6, repeated=True)


class ActivitiesListResponse(messages.Message):
    """Main response"""
    kind = messages.StringField(1) #always "reports#activities"
    nextPageToken = messages.StringField(2)
    items = messages.MessageField(Activity, 3, repeated=True)    
# [END messages]


# [START activities_list_api_class]
@endpoints.api(name='activitylogs', version='v1')
class ActivitiesListApi(remote.Service):

    @endpoints.method(
        message_types.VoidMessage,  # don't expect message in request
        ActivitiesListResponse,  # return
        path='list',
        http_method='GET'
    )
    def list_activities(self, request):
        # just return a self constructed response for now
        parameter1 = Parameter(
            name="old_doc_id",
            value="12345"
        )
        parameter2 = Parameter(
            name="size_in_bytes",
            intValue=96
        )
        parameter3 = Parameter(
            name="is_writable",
            boolValue=False
        )
        event1 = Event(
            type="drive_type",
            name="rename_file",
            parameters=[parameter1, parameter2]
        )
        event2 = Event(
            type="drive_type",
            name="rename_file",
            parameters=[parameter2, parameter3]
        )
        actor = Actor(
            callerType="I don't know",
            email="me@domain.com",
            profileId=12345,
            key="asdf"
        )
        id = Id(
            time=datetime.now(),
            uniqueQualifier=9876,
            applicationName="drive",
            customerId="jahsdfgkjb"
        )
        activity1 = Activity(
                id,
                actor,
                ownerDomain="me.com",
                ipAddress="127.0.0.1",
                events=[event1,event2]
            )
        activity2 = Activity(
                id,
                actor,
                ownerDomain="me.com",
                ipAddress="127.0.0.1",
                events=[event1,event2]
            )
        items = [activity1, activity2]
        return ActivitiesListResponse(
            nextPageToken="kjbasdg",
            items=items
        )




# [END activities_list_api_class]# [END activities_list_api_class]


# [START api_server]
api = endpoints.api_server([ActivitiesListApi])
# [END api_server]

1 个答案:

答案 0 :(得分:2)

所有字段都必须作为关键字参数传递,包括idactor

activity1 = Activity(
    id=id,
    actor=actor,
    ownerDomain="me.com",
    ipAddress="127.0.0.1",
    events=[event1,event2]
)
activity2 = Activity(
    id=id,
    actor=actor,
    ownerDomain="me.com",
    ipAddress="127.0.0.1",
    events=[event1,event2]
)