REST API 스팸 보호 및 CAPTCHA 지원
모델을 REST API를 통해 수정할 수 있는 경우, 스팸 가능성이 있거나 스팸과 관련된 속성을 수정할 수 있는 모든 관련 API 엔드포인트에 지원을 추가해야 합니다. 이는 분명히 POST 및 PUT 변형을 포함하지만, 모델의 기밀/공 공개 플래그 변경과 관련된 다른 것들도 포함될 수 있습니다.
REST 엔드포인트에 지원 추가
주요 단계는 다음과 같습니다:
- 
resource에helpers SpammableActions::CaptchaCheck::RestApiActionsSupport추가합니다.
- Update Service 클래스 생성자에 perform_spam_check: true를 전달합니다. 기본적으로 Create Service에서는true로 설정되어 있습니다.
- 
Spammable모델 인스턴스를 생성하거나 업데이트한 후,#check_spam_action_response!를 호출하여 생성되거나 업데이트된 인스턴스를 변수에 저장합니다.
- 요청의 failure경우에 대한 오류 처리 논리를 식별합니다. 이는 생성 또는 업데이트가 성공하지 못했음을 나타냅니다. 이는Spammable인스턴스에 오류가 추가됩니다. 오류는 보통render_api_error!또는render_validation_error!와 유사합니다.
- 기존 오류 처리 논리를 
with_captcha_check_rest_api(spammable: my_spammable_instance)호출로 래핑하여, 변수에 저장한Spammable모델 인스턴스를spammable:명명된 인수로 전달합니다. 이 호출은:- 모델에 대한 필요한 스팸 검사를 수행합니다.
- 스팸이 감지되면:
        - 설명적인 스팸 전용 오류 메시지를 가진 Grape #error!예외를 발생시킵니다.
- 응답에 오류 필드로 추가된 관련 정보를 포함합니다. 이러한 필드에 대한 추가 세부정보는 REST API 문서의 스팸으로 감지된 요청 해결 섹션을 참조하십시오.
 
- 설명적인 스팸 전용 오류 메시지를 가진 Grape 
 주의: 위에서 설명한 표준 ApolloLink 또는 Axios 인터셉터 CAPTCHA 지원을 사용하는 경우 필드 세부정보는 자동으로 처리되므로 무시할 수 있습니다. 이는 GraphQL API를 직접 사용하여 잠재적인 스팸 검사를 처리하고 해결된 CAPTCHA 응답으로 요청을 재제출하려고 할 때 관련이 있습니다. 
다음은 snippets 리소스에 대한 post 및 put 작업의 예입니다:
module API
  class Snippets < ::API::Base
    #...
    resource :snippets do
      # 이 헬퍼는 `#with_captcha_check_rest_api`를 제공합니다
      helpers SpammableActions::CaptchaCheck::RestApiActionsSupport
      post do
        #...
        service_response = ::Snippets::CreateService.new(project: nil, current_user: current_user, params: attrs).execute
        snippet = service_response.payload[:snippet]
        if service_response.success?
          present snippet, with: Entities::PersonalSnippet, current_user: current_user
        else
          # 일반 오류 응답을 `with_captcha_check_rest_api(spammable: snippet)` 블록에 래핑합니다
          with_captcha_check_rest_api(spammable: snippet) do
            # 스팸이 감지된 경우, 
            # `#with_captcha_check_rest_api`로 인해 예외가 발생하여 
            # Grape가 `error!`로 처리하게 됩니다
            render_api_error!({ error: service_response.message }, service_response.http_status)
          end
        end
      end
      put ':id' do
        #...
        service_response = ::Snippets::UpdateService.new(project: nil, current_user: current_user, params: attrs, perform_spam_check: true).execute(snippet)
        snippet = service_response.payload[:snippet]
        if service_response.success?
          present snippet, with: Entities::PersonalSnippet, current_user: current_user
        else
          # 일반 오류 응답을 `with_captcha_check_rest_api(spammable: snippet)` 블록에 래핑합니다
          with_captcha_check_rest_api(spammable: snippet) do
            # 스팸이 감지된 경우, 
            # `#with_captcha_check_rest_api`로 인해 예외가 발생하여 
            # Grape가 `error!`로 처리하게 됩니다
            render_api_error!({ error: service_response.message }, service_response.http_status)
          end
        end
      end
 도움말
        도움말