Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use proper retrofit2 async execution #6

Open
wants to merge 4 commits into
base: master
from
Open
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -16,7 +16,6 @@
package com.github.tony19.loggly;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;
@@ -40,15 +39,6 @@
@POST("inputs/{token}")
Call<LogglyResponse> log(@Path("token") String token, @Header("X-LOGGLY-TAG") String tags, @Body String message);

/**
* Posts a single log event to Loggly's REST endpoint
* @param token Loggly customer token
* @param tags CSV of tags
* @param message log event to be posted
* @param callback callback to be invoked on completion of the post
*/
@POST("inputs/{token}")
Call<Void> log(@Path("token") String token, @Header("X-LOGGLY-TAG") String tags, @Body String message, Callback<LogglyResponse> callback);

/**
* Posts several log events at once to Loggly's bulk REST endpoint
@@ -62,15 +52,4 @@
@POST("bulk/{token}")
Call<LogglyResponse> logBulk(@Path("token") String token, @Header("X-LOGGLY-TAG") String tags, @Body String messages);

/**
* Posts several log events at once to Loggly's bulk REST endpoint
* @param token Loggly customer token
* @param tags CSV of tags
* @param messages log event messages, each delimited by new-line
* The text is parsed for a log event in each line.
* e.g., "Hello\nWorld" would create two log events.
* @param callback callback to be invoked on completion of the post
*/
@POST("bulk/{token}")
Call<Void> logBulk(@Path("token") String token, @Header("X-LOGGLY-TAG") String tags, @Body String messages, Callback<LogglyResponse> callback);
}
@@ -15,14 +15,14 @@
*/
package com.github.tony19.loggly;

import java.util.Arrays;
import java.util.Collection;

import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import java.util.Arrays;
import java.util.Collection;

/**
* Loggly client
*
@@ -80,7 +80,7 @@ public void setTags(String... tags) {
if (!first) {
builder.append(",");
}
builder.append(t);
builder.append(sanitizeTag(t));
}
first = false;
}
@@ -116,20 +116,18 @@ public boolean log(String message) {
public void log(String message, final Callback callback) {
if (message == null) return;

loggly.log(token,
tags,
message,
new retrofit2.Callback<LogglyResponse>() {
@Override
public void onResponse(Call<LogglyResponse> call, Response<LogglyResponse> response) {
callback.success();
}

@Override
public void onFailure(Call<LogglyResponse> call, Throwable throwable) {
callback.failure(throwable.getMessage());
}
});
Call call = loggly.log(token, tags, message);
call.enqueue(new retrofit2.Callback<LogglyResponse>() {
@Override
public void onResponse(Call<LogglyResponse> call, Response<LogglyResponse> response) {
callback.success();
}

@Override
public void onFailure(Call<LogglyResponse> call, Throwable throwable) {
callback.failure(throwable.getMessage());
}
});
}

/**
@@ -171,23 +169,23 @@ public boolean logBulk(Collection<String> messages) {
public void logBulk(Collection<String> messages, final Callback callback) {
if (messages == null) return;



String parcel = joinStrings(messages);
if (parcel.isEmpty()) return;

loggly.logBulk(token,
tags,
parcel,
new retrofit2.Callback<LogglyResponse>() {
@Override
public void onResponse(Call<LogglyResponse> call, Response<LogglyResponse> response) {
callback.success();
}

@Override
public void onFailure(Call<LogglyResponse> call, Throwable throwable) {
callback.failure(throwable.getMessage());
}
});
Call call = loggly.logBulk(token, tags, parcel);
call.enqueue(new retrofit2.Callback<LogglyResponse>() {
@Override
public void onResponse(Call<LogglyResponse> call, Response<LogglyResponse> response) {
callback.success();
}

@Override
public void onFailure(Call<LogglyResponse> call, Throwable throwable) {
callback.failure(throwable.getMessage());
}
});
}

/**
@@ -212,4 +210,24 @@ private String joinStrings(Collection<String> messages) {
}
return b.toString();
}

/**
* Sanitize the tag based on the restrictions described in
* <a href="https://www.loggly.com/docs/tags/">https://www.loggly.com/docs/tags/</a>.
* Sanitation works by replacing invalid characters with the _ (underscore) character.
*
* @param tag tag to be sanitized
* @return the tag without invalid characters
*/
private String sanitizeTag(String tag) {
// replace invalid characters with _
tag = tag.replaceAll("[^A-Za-z0-9_*,.\\-]", "_");

// don't allow non-alphanumeric values starting the tag
if (Character.isLetterOrDigit(tag.charAt(0))) {
return tag;
}

return tag.substring(1);
}
}
@@ -15,18 +15,19 @@
*/
package com.github.tony19.loggly;

import retrofit2.Call;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

import static org.junit.Assert.assertThat;
import retrofit2.Call;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.isNull;
import static org.mockito.Mockito.mock;
@@ -138,4 +139,18 @@ public void emptyTagsResultInNoTags() {
loggly.logBulk("event");
Mockito.verify(restApi).logBulk(TOKEN, NO_TAGS, "event\n");
}

@Test
public void invalidTagsResultInNoTags() {
loggly.setTags("", " ", " ,", ", , ,, ");
loggly.logBulk("event");
Mockito.verify(restApi).logBulk(TOKEN, NO_TAGS, "event\n");
}

@Test
public void invalidTagsAreSentToLogglySanitized() {
loggly.setTags("_startInvalid", "middle@invalid.com", "%how_many$*3");
loggly.logBulk("event");
Mockito.verify(restApi).logBulk(TOKEN, "startInvalid,middle_invalid.com,how_many_*3", "event\n");
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.