001package org.hl7.fhir.utilities.http;
002
003import lombok.AllArgsConstructor;
004import lombok.Getter;
005import lombok.With;
006
007import javax.annotation.Nonnull;
008import javax.annotation.Nullable;
009import java.net.MalformedURLException;
010import java.net.URL;
011import java.util.Collections;
012
013@AllArgsConstructor
014public class HTTPRequest {
015
016  public HTTPRequest() {
017    url = null;
018    method = HttpMethod.GET;
019    body = null;
020    contentType = null;
021    headers = Collections.emptyList();
022  }
023
024  public enum HttpMethod {
025    GET, POST, PUT, DELETE, OPTIONS, HEAD, PATCH
026  }
027
028  @Getter @Nullable
029  private final URL url;
030
031  public HTTPRequest withUrl(URL url) {
032    return new HTTPRequest(url, method, body, contentType, headers);
033  }
034
035  public HTTPRequest withUrl(String url)  {
036    try {
037      return withUrl(new URL(url));
038    } catch (MalformedURLException e) {
039      throw new IllegalArgumentException("Invalid URL: " + url, e);
040    }
041  }
042
043  @With @Getter
044  private final HttpMethod method;
045
046  @With @Getter @Nullable
047  private final byte[] body;
048
049  @With @Getter @Nullable
050  private final String contentType;
051
052  @With @Getter @Nonnull
053  private final Iterable<HTTPHeader> headers;
054}