-
Notifications
You must be signed in to change notification settings - Fork 174
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
Feature/build integer response #18
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* 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 com.cdancy.jenkins.rest.domain.common; | ||
|
||
import java.util.List; | ||
|
||
import org.jclouds.javax.annotation.Nullable; | ||
import org.jclouds.json.SerializedNames; | ||
|
||
import com.cdancy.jenkins.rest.JenkinsUtils; | ||
import com.google.auto.value.AutoValue; | ||
|
||
/** | ||
* Integer response to be returned when an endpoint returns | ||
* an integer. | ||
* | ||
* <p>When the HTTP response code is valid the `value` parameter will | ||
* be set to the integer value while a non-valid response has the `value` set to | ||
* null along with any potential `error` objects returned from Jenkins. | ||
*/ | ||
@AutoValue | ||
public abstract class IntegerResponse implements Value<Integer>, ErrorsHolder { | ||
|
||
@SerializedNames({ "value", "errors" }) | ||
public static IntegerResponse create(@Nullable final Integer value, | ||
final List<Error> errors) { | ||
|
||
return new AutoValue_IntegerResponse(value, | ||
JenkinsUtils.nullToEmpty(errors)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,33 +17,47 @@ | |
|
||
package com.cdancy.jenkins.rest.parsers; | ||
|
||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
|
||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import javax.inject.Singleton; | ||
|
||
import org.jclouds.http.HttpResponse; | ||
|
||
import com.google.common.base.Charsets; | ||
import com.google.common.base.Function; | ||
import com.google.common.base.Throwables; | ||
import com.google.common.collect.Lists; | ||
import com.google.common.io.CharStreams; | ||
|
||
import com.cdancy.jenkins.rest.domain.common.Error; | ||
import com.cdancy.jenkins.rest.domain.common.IntegerResponse; | ||
|
||
/** | ||
* Created by dancc on 3/11/16. | ||
*/ | ||
@Singleton | ||
public class LocationToQueueId implements Function<HttpResponse, Integer> { | ||
public class LocationToQueueId implements Function<HttpResponse, IntegerResponse> { | ||
|
||
private static final Pattern pattern = Pattern.compile("^.*/queue/item/(\\d+)/$"); | ||
|
||
public Integer apply(HttpResponse response) { | ||
public IntegerResponse apply(HttpResponse response) { | ||
|
||
String url = response.getFirstHeaderOrNull("Location"); | ||
List<Error> errors = Lists.newArrayList(); | ||
if (url != null) { | ||
Matcher matcher = pattern.matcher(url); | ||
if (matcher.find() && matcher.groupCount() == 1) { | ||
return Integer.valueOf(matcher.group(1)); | ||
return IntegerResponse.create(Integer.valueOf(matcher.group(1)), errors); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I was trying to apply the same approach as in the Bitbucket Rest Understanding Error objects where the error object is never null so it can always be easily checked. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's fair but lets let the underlying process take care of that instead of aggressively creating it up front. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now I get it, we let |
||
} | ||
} | ||
|
||
return 0; | ||
errors.add(Error.create("No context", | ||
"No queue item Location header could be found despite getting a valid HTTP response.", | ||
"None")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cdancy, in creating this error, is it possible to have the original context (the POST)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @martinda whatever is in the httpResponse is really all we have to work with. What were you thinknig? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cdancy I was thinking of printing the HTTP response, in case it might give a hint to the user. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets take the
I'd prefer to delay the creation of the list until the last possible minute as this is an exceptional case. In the code further above lets just pass a null like so:
|
||
return IntegerResponse.create(null, errors); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these imports needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will fix this.