Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Allow bean injection in @Rest #712

Description

@DayS

We should allow developer to inject beans in converter and interceptor fields of @Rest. Right now, even if a bean is set in one of these fields, the generated code will create a new instance of the base class.

This idea came from this thread on StackOverflow

Activity

  1. bperin commented on Sep 18, 2013

    @bperin

    thanks for opening the issue, that was my stack thread. I have some working code on how to do an authenticated rest client. I think the problem was that attaching the interceptor in the annotated @rest interface would just point to the plain old non annotated version, the solution is to add the @ebean interceptor programatically in your activity.

    Rest interface

    @Rest(rootUrl = "https://", converters = { GsonHttpMessageConverter.class })
    public interface ApiClient {
    
    @Post("force_login")
    JsonObject forceLogin(HashMap params);
    
    RestTemplate getRestTemplate();
    
    void setRestTemplate(RestTemplate restTemplate);
    
    }
    

    Rest interceptor

    @EBean(scope = Scope.Singleton)
    public class RestInterceptor implements ClientHttpRequestInterceptor {
    
    final String TAG = "rest";
    
    @Pref
    MyPrefs_ myPrefs;
    
    public ClientHttpResponse intercept(HttpRequest request, byte[] data, ClientHttpRequestExecution execution)
            throws IOException {
    
        HttpHeaders headers = request.getHeaders();
        if(myPrefs.sessionKey().exists()){
            headers.add("session_key",myPrefs.sessionKey().get());
        }
        if(myPrefs.apiKey().exists()){
            headers.add("api_key",myPrefs.apiKey().get());
        }
        return execution.execute(request, data);
    
    }
    
    }
    

    shared prefs that holds an api key and session key

    @SharedPref
    public interface MyPrefs {
    
    String sessionKey();
    
    @DefaultString("secretKey")
    String apiKey();
    
    }
    

    and then in my main activity

    @EActivity
    public abstract class MainActivity extends SherlockActivity {
        @RestService
    ApiClient apiClient;
    
    @Bean
    RestInterceptor restInterceptor;
    
        @AfterInject
    public void doWork() {
        RestTemplate template = apiClient.getRestTemplate();
        List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
        interceptors.add(restInterceptor);
        template.setInterceptors(interceptors);
    }
    
    
    }
    
  2. DayS commented on Jan 20, 2014

    @DayS
    ContributorAuthor

    This is a duplicate of #661

  3. yDelouis commented on Jan 20, 2014

    @yDelouis
    Contributor

    This is not fully a duplicate of #661 because #661 is only about interceptors and this one is also about converters.

  4. DayS commented on Jan 20, 2014

    @DayS
    ContributorAuthor

    Yep. And I wanted to ask you to add this on your PR :)

  5. yDelouis commented on Jan 20, 2014

    @yDelouis
    Contributor

    Done :)

  6. DayS commented on Apr 6, 2014

    @DayS
    ContributorAuthor

    Feature merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions