Github user zentol commented on a diff in the pull request:

    https://github.com/apache/flink/pull/3736#discussion_r112304098
  
    --- Diff: 
flink-metrics/flink-metrics-datadog/src/main/java/org/apache/flink/metrics/datadog/DatadogHttpClient.java
 ---
    @@ -0,0 +1,83 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.flink.metrics.datadog;
    +
    +import okhttp3.MediaType;
    +import okhttp3.OkHttpClient;
    +import okhttp3.Request;
    +import okhttp3.Response;
    +import okhttp3.RequestBody;
    +
    +import java.io.IOException;
    +import java.util.concurrent.TimeUnit;
    +
    +/**
    + * Http client talking to Datadog
    + * */
    +public class DatadogHttpClient{
    +   private static final String SERIES_URL_FORMAT = 
"https://app.datadoghq.com/api/v1/series?api_key=%s";;
    +   private static final String VALIDATE_URL_FORMAT = 
"https://app.datadoghq.com/api/v1/validate?api_key=%s";;
    +   private static final MediaType MEDIA_TYPE = 
MediaType.parse("application/json; charset=utf-8");
    +   private static final int TIMEOUT = 5;
    +
    +   private final String seriesUrl;
    +   private final String validateUrl;
    +   private final OkHttpClient client;
    +   private final String apiKey;
    +
    +   public DatadogHttpClient(String dgApiKey) {
    +           if(dgApiKey == null || dgApiKey.isEmpty()) {
    +                   throw new IllegalArgumentException(
    +                           "Invalid API key:" + dgApiKey);
    +           }
    +           apiKey = dgApiKey;
    +           client = new OkHttpClient.Builder()
    +                   .connectTimeout(TIMEOUT, TimeUnit.SECONDS)
    +                   .writeTimeout(TIMEOUT, TimeUnit.SECONDS)
    +                   .readTimeout(TIMEOUT, TimeUnit.SECONDS)
    +                   .build();
    +
    +           seriesUrl = String.format(SERIES_URL_FORMAT, apiKey);
    +           validateUrl = String.format(VALIDATE_URL_FORMAT, apiKey);
    +           validateApiKey();
    +   }
    +
    +   private void validateApiKey() {
    +           Request r = new 
Request.Builder().url(validateUrl).get().build();
    +
    +           try {
    +                   Response response = client.newCall(r).execute();
    +                   if(!response.isSuccessful()) {
    --- End diff --
    
    The response can fail for reasons other than the key being invalid, no? If 
so, the exception message should state "Failed to validate API key. <error 
code>".


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to