Skip to content

Commit 2f89e13

Browse files
authored
Call upload URL using Retrofit's @url annotation (#391)
* call upload URL using Retrofit's @url annotation * fix formatting Co-authored-by: David Menear <dmenear@users.noreply.github.com>
1 parent 5b9cf2e commit 2f89e13

4 files changed

Lines changed: 34 additions & 20 deletions

File tree

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.segment.analytics.http;
22

33
import com.segment.analytics.messages.Batch;
4+
import okhttp3.HttpUrl;
45
import retrofit2.Call;
56
import retrofit2.http.Body;
67
import retrofit2.http.POST;
8+
import retrofit2.http.Url;
79

810
/** REST interface for the Segment API. */
911
public interface SegmentService {
10-
@POST(".")
11-
Call<UploadResponse> upload(@Body Batch batch);
12+
@POST
13+
Call<UploadResponse> upload(@Url HttpUrl uploadUrl, @Body Batch batch);
1214
}

analytics/src/main/java/com/segment/analytics/Analytics.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,14 +431,15 @@ public void log(String message) {
431431
Retrofit restAdapter =
432432
new Retrofit.Builder()
433433
.addConverterFactory(GsonConverterFactory.create(gson))
434-
.baseUrl(endpoint)
434+
.baseUrl(DEFAULT_ENDPOINT)
435435
.client(client)
436436
.build();
437437

438438
SegmentService segmentService = restAdapter.create(SegmentService.class);
439439

440440
AnalyticsClient analyticsClient =
441441
AnalyticsClient.create(
442+
endpoint,
442443
segmentService,
443444
queueCapacity,
444445
flushQueueSize,

analytics/src/main/java/com/segment/analytics/internal/AnalyticsClient.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.concurrent.TimeUnit;
3131
import java.util.concurrent.atomic.AtomicBoolean;
3232
import java.util.concurrent.atomic.AtomicInteger;
33+
import okhttp3.HttpUrl;
3334
import retrofit2.Call;
3435
import retrofit2.Response;
3536

@@ -50,6 +51,7 @@ public class AnalyticsClient {
5051
}
5152

5253
private final BlockingQueue<Message> messageQueue;
54+
private final HttpUrl uploadUrl;
5355
private final SegmentService service;
5456
private final int size;
5557
private final int maximumRetries;
@@ -63,6 +65,7 @@ public class AnalyticsClient {
6365
private final AtomicBoolean isShutDown;
6466

6567
public static AnalyticsClient create(
68+
HttpUrl uploadUrl,
6669
SegmentService segmentService,
6770
int queueCapacity,
6871
int flushQueueSize,
@@ -75,6 +78,7 @@ public static AnalyticsClient create(
7578
List<Callback> callbacks) {
7679
return new AnalyticsClient(
7780
new LinkedBlockingQueue<Message>(queueCapacity),
81+
uploadUrl,
7882
segmentService,
7983
flushQueueSize,
8084
flushIntervalInMillis,
@@ -89,6 +93,7 @@ public static AnalyticsClient create(
8993

9094
AnalyticsClient(
9195
BlockingQueue<Message> messageQueue,
96+
HttpUrl uploadUrl,
9297
SegmentService service,
9398
int maxQueueSize,
9499
long flushIntervalInMillis,
@@ -100,6 +105,7 @@ public static AnalyticsClient create(
100105
List<Callback> callbacks,
101106
AtomicBoolean isShutDown) {
102107
this.messageQueue = messageQueue;
108+
this.uploadUrl = uploadUrl;
103109
this.service = service;
104110
this.size = maxQueueSize;
105111
this.maximumRetries = maximumRetries;
@@ -355,7 +361,7 @@ boolean upload() {
355361
client.log.print(VERBOSE, "Uploading batch %s.", batch.sequence());
356362

357363
try {
358-
Call<UploadResponse> call = client.service.upload(batch);
364+
Call<UploadResponse> call = client.service.upload(client.uploadUrl, batch);
359365
Response<UploadResponse> response = call.execute();
360366

361367
if (response.isSuccessful()) {

analytics/src/test/java/com/segment/analytics/internal/AnalyticsClientTest.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public void setUp() {
9393
AnalyticsClient newClient() {
9494
return new AnalyticsClient(
9595
messageQueue,
96+
null,
9697
segmentService,
9798
50,
9899
TimeUnit.HOURS.toMillis(1),
@@ -271,6 +272,7 @@ public void flushHowManyTimesNecessaryToStayWithinLimit() throws InterruptedExce
271272
AnalyticsClient client =
272273
new AnalyticsClient(
273274
messageQueue,
275+
null,
274276
segmentService,
275277
50,
276278
TimeUnit.HOURS.toMillis(1),
@@ -360,7 +362,7 @@ public void batchRetriesForNetworkErrors() {
360362
Response<UploadResponse> failureResponse = Response.error(429, ResponseBody.create(null, ""));
361363

362364
// Throw a network error 3 times.
363-
when(segmentService.upload(batch))
365+
when(segmentService.upload(null, batch))
364366
.thenReturn(Calls.response(failureResponse))
365367
.thenReturn(Calls.response(failureResponse))
366368
.thenReturn(Calls.response(failureResponse))
@@ -370,7 +372,7 @@ public void batchRetriesForNetworkErrors() {
370372
batchUploadTask.run();
371373

372374
// Verify that we tried to upload 4 times, 3 failed and 1 succeeded.
373-
verify(segmentService, times(4)).upload(batch);
375+
verify(segmentService, times(4)).upload(null, batch);
374376
verify(callback).success(trackMessage);
375377
}
376378

@@ -385,7 +387,7 @@ public void batchRetriesForHTTP5xxErrors() {
385387
Response<UploadResponse> successResponse = Response.success(200, response);
386388
Response<UploadResponse> failResponse =
387389
Response.error(500, ResponseBody.create(null, "Server Error"));
388-
when(segmentService.upload(batch))
390+
when(segmentService.upload(null, batch))
389391
.thenReturn(Calls.response(failResponse))
390392
.thenReturn(Calls.response(failResponse))
391393
.thenReturn(Calls.response(failResponse))
@@ -395,7 +397,7 @@ public void batchRetriesForHTTP5xxErrors() {
395397
batchUploadTask.run();
396398

397399
// Verify that we tried to upload 4 times, 3 failed and 1 succeeded.
398-
verify(segmentService, times(4)).upload(batch);
400+
verify(segmentService, times(4)).upload(null, batch);
399401
verify(callback).success(trackMessage);
400402
}
401403

@@ -409,7 +411,7 @@ public void batchRetriesForHTTP429Errors() {
409411
Response<UploadResponse> successResponse = Response.success(200, response);
410412
Response<UploadResponse> failResponse =
411413
Response.error(429, ResponseBody.create(null, "Rate Limited"));
412-
when(segmentService.upload(batch))
414+
when(segmentService.upload(null, batch))
413415
.thenReturn(Calls.response(failResponse))
414416
.thenReturn(Calls.response(failResponse))
415417
.thenReturn(Calls.response(failResponse))
@@ -419,7 +421,7 @@ public void batchRetriesForHTTP429Errors() {
419421
batchUploadTask.run();
420422

421423
// Verify that we tried to upload 4 times, 3 failed and 1 succeeded.
422-
verify(segmentService, times(4)).upload(batch);
424+
verify(segmentService, times(4)).upload(null, batch);
423425
verify(callback).success(trackMessage);
424426
}
425427

@@ -432,13 +434,13 @@ public void batchDoesNotRetryForNon5xxAndNon429HTTPErrors() {
432434
// Throw a HTTP error that should not be retried.
433435
Response<UploadResponse> failResponse =
434436
Response.error(404, ResponseBody.create(null, "Not Found"));
435-
when(segmentService.upload(batch)).thenReturn(Calls.response(failResponse));
437+
when(segmentService.upload(null, batch)).thenReturn(Calls.response(failResponse));
436438

437439
BatchUploadTask batchUploadTask = new BatchUploadTask(client, BACKO, batch, DEFAULT_RETRIES);
438440
batchUploadTask.run();
439441

440442
// Verify we only tried to upload once.
441-
verify(segmentService).upload(batch);
443+
verify(segmentService).upload(null, batch);
442444
verify(callback).failure(eq(trackMessage), any(IOException.class));
443445
}
444446

@@ -449,13 +451,13 @@ public void batchDoesNotRetryForNonNetworkErrors() {
449451
Batch batch = batchFor(trackMessage);
450452

451453
Call<UploadResponse> networkFailure = Calls.failure(new RuntimeException());
452-
when(segmentService.upload(batch)).thenReturn(networkFailure);
454+
when(segmentService.upload(null, batch)).thenReturn(networkFailure);
453455

454456
BatchUploadTask batchUploadTask = new BatchUploadTask(client, BACKO, batch, DEFAULT_RETRIES);
455457
batchUploadTask.run();
456458

457459
// Verify we only tried to upload once.
458-
verify(segmentService).upload(batch);
460+
verify(segmentService).upload(null, batch);
459461
verify(callback).failure(eq(trackMessage), any(RuntimeException.class));
460462
}
461463

@@ -465,7 +467,7 @@ public void givesUpAfterMaxRetries() {
465467
TrackMessage trackMessage = TrackMessage.builder("foo").userId("bar").build();
466468
Batch batch = batchFor(trackMessage);
467469

468-
when(segmentService.upload(batch))
470+
when(segmentService.upload(null, batch))
469471
.thenAnswer(
470472
new Answer<Call<UploadResponse>>() {
471473
public Call<UploadResponse> answer(InvocationOnMock invocation) {
@@ -480,7 +482,7 @@ public Call<UploadResponse> answer(InvocationOnMock invocation) {
480482

481483
// DEFAULT_RETRIES == maxRetries
482484
// tries 11(one normal run + 10 retries) even though default is 50 in AnalyticsClient.java
483-
verify(segmentService, times(11)).upload(batch);
485+
verify(segmentService, times(11)).upload(null, batch);
484486
verify(callback)
485487
.failure(
486488
eq(trackMessage),
@@ -499,7 +501,7 @@ public void hasDefaultRetriesSetTo3() {
499501
TrackMessage trackMessage = TrackMessage.builder("foo").userId("bar").build();
500502
Batch batch = batchFor(trackMessage);
501503

502-
when(segmentService.upload(batch))
504+
when(segmentService.upload(null, batch))
503505
.thenAnswer(
504506
new Answer<Call<UploadResponse>>() {
505507
public Call<UploadResponse> answer(InvocationOnMock invocation) {
@@ -514,7 +516,7 @@ public Call<UploadResponse> answer(InvocationOnMock invocation) {
514516

515517
// DEFAULT_RETRIES == maxRetries
516518
// tries 11(one normal run + 10 retries)
517-
verify(segmentService, times(4)).upload(batch);
519+
verify(segmentService, times(4)).upload(null, batch);
518520
verify(callback)
519521
.failure(
520522
eq(trackMessage),
@@ -619,7 +621,7 @@ public void neverRetries() {
619621
TrackMessage trackMessage = TrackMessage.builder("foo").userId("bar").build();
620622
Batch batch = batchFor(trackMessage);
621623

622-
when(segmentService.upload(batch))
624+
when(segmentService.upload(null, batch))
623625
.thenAnswer(
624626
new Answer<Call<UploadResponse>>() {
625627
public Call<UploadResponse> answer(InvocationOnMock invocation) {
@@ -633,7 +635,7 @@ public Call<UploadResponse> answer(InvocationOnMock invocation) {
633635
batchUploadTask.run();
634636

635637
// runs once but never retries
636-
verify(segmentService, times(1)).upload(batch);
638+
verify(segmentService, times(1)).upload(null, batch);
637639
verify(callback)
638640
.failure(
639641
eq(trackMessage),
@@ -830,6 +832,7 @@ public void submitBatchBelowThreshold() throws InterruptedException {
830832
AnalyticsClient client =
831833
new AnalyticsClient(
832834
messageQueue,
835+
null,
833836
segmentService,
834837
50,
835838
TimeUnit.HOURS.toMillis(1),
@@ -869,6 +872,7 @@ public void submitBatchAboveThreshold() throws InterruptedException {
869872
AnalyticsClient client =
870873
new AnalyticsClient(
871874
messageQueue,
875+
null,
872876
segmentService,
873877
50,
874878
TimeUnit.HOURS.toMillis(1),
@@ -907,6 +911,7 @@ public void submitManySmallMessagesBatchAboveThreshold() throws InterruptedExcep
907911
AnalyticsClient client =
908912
new AnalyticsClient(
909913
messageQueue,
914+
null,
910915
segmentService,
911916
50,
912917
TimeUnit.HOURS.toMillis(1),

0 commit comments

Comments
 (0)