aws-amplify / aws-sdk-android

AWS SDK for Android. For more information, see our web site:
https://docs.amplify.aws
Other
1.03k stars 551 forks source link

HTTP Custom Headers #2502

Closed lolucosmin closed 2 years ago

lolucosmin commented 3 years ago

State your question There is a way to add custom headers before call: apiCLient.methodX . ?

Which AWS Services are you utilizing?

    implementation group: 'com.amazonaws', name: 'aws-android-sdk-apigateway-core', version: '2.24.0'
    implementation group: 'com.amazonaws', name: 'aws-android-sdk-s3', version: '2.24.0'
    implementation group: 'com.amazonaws', name: 'aws-android-sdk-sns', version: '2.24.0'
    implementation group: 'com.amazonaws', name: 'aws-android-sdk-mobile-client', version: '2.24.0'
    implementation group: 'com.amazonaws', name: 'aws-android-sdk-auth-userpools', version: '2.24.0'
    implementation group: 'com.amazonaws', name: 'aws-android-sdk-auth-ui', version: '2.24.0'
    implementation group: 'com.amazonaws', name: 'aws-android-sdk-cognitoauth', version: '2.24.0'
    implementation group: 'com.amazonaws', name: 'aws-android-sdk-auth-facebook', version: '2.24.0'
    implementation group: 'com.amazonaws', name: 'aws-android-sdk-auth-google', version: '2.24.0'

Provide code snippets (if applicable)

        ApiClientFactory apiClientFactory= new ApiClientFactory();
        apiClientFactory.region(AppConfig.REGION.getName());
        apiClientFactory.endpoint(AppConfig.SERVER_ENDPOINT);
        apiClientFactory.clientConfiguration(this.clientConfiguration);
        apiClientFactory.credentialsProvider(provider);
       ApiClient apiCLient=apiClientFactory.build(ApiClient .class);

and when I do a request I call:

apiCLient.methodX

Environment(please complete the following information):

Device Information (please complete the following information):

If you need help with understanding how to implement something in particular then we suggest that you first look into our developer guide. You can also simplify your process of creating an application, as well as the associated backend setup by using the Amplify CLI.

eeatonaws commented 3 years ago

Hi @lolucosmin, you can use custom HTTP headers by first adding them to Amazon API Gateway (please see the documentation here) and then adding the header to your request, similar to the code example here.

lolucosmin commented 3 years ago

@eeatonaws Thanks for the reply. Ok lets take this: apiClient = new ApiClientFactory() .credentialsProvider(AWSMobileClient.getInstance()) .build(YOUR_API_CLIENT_NAME.class);

in my YOUR_API_CLIENT_NAME.class I have already this function: @com.amazonaws.mobileconnectors.apigateway.annotation.Operation(path = "/avatars", method = "GET") AvatarList avatarsGet();

in code example there they create a new request: ApiRequest localRequest = new ApiRequest(apiClient.getClass().getSimpleName()) .withPath(path) .withHttpMethod(HttpMethodName.valueOf(method)) .withHeaders(headers) .addHeader("Content-Type", "application/json") .withParameters(parameters); So is not make sense for me to create new request when that request is already defined in my YOUR_API_CLIENT_NAME.class. For me is not a good idea to create a new request for each function from my "YOUR_API_CLIENT_NAME.class", there are a lot of functions.

My workflow: this.myClient= new ApiClientFactory() .region(AppConfig.REGION.getName()) .endpoint(AppConfig.SERVER_TICKETS_ENDPOINT) .clientConfiguration(this.clientConfiguration) .credentialsProvider(provider) .build(MyClient.class); and on async or observer I run: myClient.avatarsGet()

eeatonaws commented 3 years ago

Hi @lolucosmin, thanks for the additional information. The parameter annotation, with the location set to "header", can be used to specify headers that should be added to the request, but the value for each header would need to be passed as a parameter to the method. Are there any restrictions on your use case that would prevent you from passing the header values as parameters to the method?