1

사용자 이메일로 엔드 포인트를 조회 할 수있는 메소드를 작성하려고합니다. 내가하는 일보다 더 나은 방법이 있습니까? 하나 또는 0 개의 레코드 만 반환하는 레코드 일 수 있습니다. 내가 User 있으리라 믿고있어이메일로 엔드 포인트 사용자를 조회하십시오.

@User.query_method(query_fields=('email',), 
         path='get_by_mail', 
         name='user.get_by_email') 
    def get_by_email(self, query): 
     return query 

답변

2

EndpointsModel에서 상속 일부 사용자 지정 모델입니다. 그렇지 않으면 실패합니다. 즉,이 같은 짓을했습니다

from google.appengine.ext import ndb 
from endpoints_proto_datastore.ndb import EndpointsModel 

class User(EndpointsModel): 
    email = ndb.StringProperty() 
    ... 

이 문제를 해결하는 두 가지 주요 방법이있다, 당신은 엔티티의 키로 email를 사용하거나 자신의 쿼리를 말아서 가져 오기 위해 시도 할 수 중 두 엔티티가 결과가 고유하고 존재하는지 확인합니다.

옵션 1 : 대신 전체 쿼리를하고, 당신이 simple get을 할 수있는 키

email를 사용합니다. custom alias properties sample에서 수행 될 때, 각 엔티티의 데이터 저장소 키와 이메일을 사용하여

from google.appengine.ext import endpoints 

@endpoints.api(...) 
class SomeClass(...): 

    @User.method(request_fields=('email',), 
       path='get_by_mail/{email}', 
       http_method='GET', name='user.get_by_email') 
    def get_by_email(self, user): 
     if not user.from_datastore: 
      raise endpoints.NotFoundException('User not found.') 
     return user 

.

from endpoints_proto_datastore.ndb import EndpointsAliasProperty 

class User(EndpointsModel): 
    # remove email here, as it will be an alias property 
    ... 

    def EmailSet(self, value): 
     # Validate the value any way you like 
     self.UpdateFromKey(ndb.Key(User, value)) 

    @EndpointsAliasProperty(setter=IdSet, required=True) 
    def email(self): 
     if self.key is not None: return self.key.string_id() 

옵션 2 : 예를 들어 롤 자신의 쿼리

@User.method(request_fields=('email',), 
       path='get_by_mail/{email}', 
       http_method='GET', name='user.get_by_email') 
    def get_by_email(self, user): 
     query = User.query(User.email == user.email) 
     # We fetch 2 to make sure we have 
     matched_users = query.fetch(2) 
     if len(matched_users == 0): 
      raise endpoints.NotFoundException('User not found.') 
     elif len(matched_users == 2): 
      raise endpoints.BadRequestException('User not unique.') 
     else: 
      return matched_users[0] 
+0

는 그러나 나중에 나는 그의 전화 번호로 사용자를 발견 할 수 있습니다. 나는 어떻게 그것에 대해 갈 것인가? – user672009

+0

어쩌면 내가 완전히 잘못하고있는 것일 수 있습니다. 나는 사용자의 Google 계정에 의해 auths 안드로이드 애플 리케이션을 만들고 다음 기본적으로 해당 사용자 및 전화 번호에 대한 참조를 가지고 끝점 모델을 가지고 ... 제안? – user672009

+0

답변이 업데이트되었습니다. – bossylobster