Instana U

 View Only
  • 1.  Java Calling API

    Posted 2 days ago

    Hello

    Im need to wrap instana Api with Java Spring boot.

    So I used: https://instana.github.io/openapi/

    java -jar openapi-generator-cli.jar generate -i https://instana.github.io/openapi/openapi.yaml -g java -o pkg/instana/openapi --skip-validate-spec  -p dateLibrary=java8,hideGenerationTimestamp=true --library resttemplate

    and now i need to start coding the framework.

    I cannot find any documents, any code examples on how execute the java ?

    In case you  have  some code examples how to execute api ?

    Thanks.



    ------------------------------
    Yaniv Levy
    ------------------------------


  • 2.  RE: Java Calling API

    Posted 2 days ago
    Did you have the chance to review the README.md file of the generated project ?. It has a quite complete spec of the library and the use into a spring-boot project.

    Regards,

    Luis







  • 3.  RE: Java Calling API

    Posted 2 days ago

    Dear Luis

    I have read the file and there is a short snippet of Java to try it out. it seems the some of the code is irrelevant.


    Here is the result, Im getting an error 401 - and i was generating an API TOKEN at : https://ibmdevsandbox-instanaibm.instana.io/

    private void postConstruct() {
    ApiClient client = new ApiClient();
    client.setBasePath("https://ibmdevsandbox-instanaibm.instana.io");
    client.setDebugging(true);
    client.setMaxAttemptsForRetry(5);
    ApiKeyAuth ApiKeyAuth = (ApiKeyAuth) client.getAuthentication("ApiKeyAuth");
    ApiKeyAuth.setApiKey("MY___OWN___KEY");
     ActionCatalogApi apiInstance = new ActionCatalogApi(client);
    ApiTokenApi ata = new ApiTokenApi(client);
     
    try {
    List<ApiToken> result = ata.getApiTokens();
    System.out.println(result);
    } catch (Exception e) {
    System.err.println("Exception when calling ActionCatalogApi#getActionByID");
    System.err.println("Status code: " + e.getMessage());
    e.printStackTrace();
    }
    }

     
    Exception when calling ActionCatalogApi#getActionByID
    Status code: 401 Unauthorized: "{"errors":["Unauthorized request"]}"
    org.springframework.web.client.HttpClientErrorException$Unauthorized: 401 Unauthorized: "{"errors":["Unauthorized request"]}"



    ------------------------------
    Yaniv Levy
    ------------------------------



  • 4.  RE: Java Calling API

    Posted 2 days ago
    Check the API token you're using. The ApiTokenClient class calls /api/settings/api-tokens, and the API Token you're using should have the permission "Configuration of API Tokens":
    Screenshot 2024-07-25 at 15.24.43.jpg

    Validate with curl:
    curl -v -H "authorization: apiToken My_API_Token" \
    https://ibmdevsandbox-instanaibm.instana.io/api/settings/api-tokens

    Regards,

    Luis








  • 5.  RE: Java Calling API

    Posted 17 hours ago

    Thank you Luis,

    This solved the issue in Java:
    client.addDefaultHeader("authorization", "apiToken XXXXXXXXXXXXXXXXXX")



    ------------------------------
    Yaniv Levy
    ------------------------------



  • 6.  RE: Java Calling API

    Posted 15 hours ago
    Well done :-).

    Reviewing the code again I could find one missing statement. After you set the API Key you should provide the prefix "apiToken":

    ApiKeyAuth ApiKeyAuth = (ApiKeyAuth) client.getAuthentication("ApiKeyAuth");
    ApiKeyAuth.setApiKey("MY___OWN___KEY");
    ApiKeyAuth.setApiKeyPrefix("apiToken");
    ActionCatalogApi apiInstance = new ActionCatalogApi(client);

    Regards,

    Luis