Skip to content

Commit

Permalink
Merge pull request #299 from microsoft/andrueastman/ResponseHeaders
Browse files Browse the repository at this point in the history
Adds reponseheaders to API excpetion
  • Loading branch information
andrueastman authored Apr 27, 2023
2 parents 838b498 + 831ead8 commit 4a437d1
Show file tree
Hide file tree
Showing 9 changed files with 312 additions and 162 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

## [0.4.3] - 2023-04-17

### Added

- Adds responseHeader to APIException class

## [0.4.2] - 2023-03-31

### Added
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.microsoft.kiota;

import java.util.Objects;

import javax.annotation.Nonnull;

/** Parent type for exceptions thrown by the client when receiving failed responses to its requests. */
Expand All @@ -23,4 +25,21 @@ public ApiException(@Nonnull final Throwable cause) {

/** The HTTP status code for the response*/
public int responseStatusCode;

/** The HTTP response headers for the error response*/
@Nonnull
private ResponseHeaders responseHeaders = new ResponseHeaders();

/** Gets the HTTP response headers for the error response */
@Nonnull
public ResponseHeaders getResponseHeaders() {
return new ResponseHeaders(responseHeaders);
}

/** Sets the HTTP response headers for the error response */
public void setResponseHeaders(@Nonnull ResponseHeaders responseHeaders) {
Objects.requireNonNull(responseHeaders);
this.responseHeaders = new ResponseHeaders(responseHeaders);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package com.microsoft.kiota;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

class CaseInsensitiveMap implements Map<String, Set<String>>{
private final HashMap<String, HashSet<String>> internalMap = new HashMap<>();

protected String normalizeKey(@Nonnull final String key) {
Objects.requireNonNull(key);
return key.toLowerCase(Locale.ROOT);
}

/** {@inheritDoc} */
@Override
public int size() {
return internalMap.size();
}

/** {@inheritDoc} */
@Override
public boolean isEmpty() {
return internalMap.isEmpty();
}

/** {@inheritDoc} */
@Override
public boolean containsKey(@Nonnull final Object key) {
Objects.requireNonNull(key);
if (key instanceof String) {
return internalMap.containsKey(normalizeKey((String) key));
} else {
return false;
}
}

/** {@inheritDoc} */
@Override
public boolean containsValue(@Nonnull final Object value) {
Objects.requireNonNull(value);
return internalMap.containsValue(value);
}

/** {@inheritDoc} */
@Override
@Nonnull
public Set<String> get(@Nonnull final Object key) {
Objects.requireNonNull(key);
if (key instanceof String) {
return internalMap.get(normalizeKey((String) key));
} else {
return Collections.emptySet();
}
}

/** {@inheritDoc} */
@Override
@Nonnull
public Set<String> put(@Nonnull final String key, @Nonnull final Set<String> value) {
Objects.requireNonNull(key);
Objects.requireNonNull(value);
return internalMap.put(normalizeKey(key), new HashSet<>(value));
}

/** {@inheritDoc} */
@Override
@Nonnull
public Set<String> remove(@Nonnull final Object key) {
Objects.requireNonNull(key);
if (key instanceof String) {
return internalMap.remove(normalizeKey((String) key));
} else {
return Collections.emptySet();
}
}

@Override
public void putAll(@Nullable final Map<? extends String, ? extends Set<String>> m) {
if (m == null) {
return;
}
for (final Entry<? extends String, ? extends Set<String>> entry : m.entrySet()) {
final String key = entry.getKey();
final Set<String> value = entry.getValue();
if (key != null && value != null) {
internalMap.put(normalizeKey(key), new HashSet<>(value));
}
}
}

@Override
public void clear() {
internalMap.clear();
}

@Override
@Nonnull
public Set<String> keySet() {
return internalMap.keySet();
}

@Override
@Nonnull
public Collection<Set<String>> values() {
return new ArrayList<>(internalMap.values());
}

@Override
@Nonnull
public Set<Entry<String, Set<String>>> entrySet() {
final HashSet<Entry<String, Set<String>>> result = new HashSet<>();
for (final Entry<String, HashSet<String>> entry : internalMap.entrySet()) {
result.add(new Entry<String, Set<String>>() {
@Override
public String getKey() {
return entry.getKey();
}

@Override
public Set<String> getValue() {
return entry.getValue();
}

@Override
public Set<String> setValue(Set<String> value) {
return entry.setValue(new HashSet<>(value));
}
});
}
return result;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.microsoft.kiota;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

import javax.annotation.Nonnull;

class Headers extends CaseInsensitiveMap {
/** Default constructor */
public Headers() {
super();
}

/** Copy constructor */
public Headers(@Nonnull Headers headers) {
super();
Objects.requireNonNull(headers);
putAll(headers);
}

/**
* Adds a header to the current request.
*
* @param key the key of the header to add.
* @param value the value of the header to add.
*/
public void add(@Nonnull final String key, @Nonnull final String value) {
Objects.requireNonNull(key);
Objects.requireNonNull(value);
final String normalizedKey = normalizeKey(key);
if (this.containsKey(normalizedKey)) {
final Set<String> values = this.get(normalizedKey);
values.add(value);
} else {
final Set<String> values = new HashSet<>(1);
values.add(value);
this.put(normalizedKey, values);
}
}

/**
* Removes a value from a header
*
* @param key the key of the header to remove the value from
* @param value the value to remove
* @return true if the value was removed, false otherwise
*/
public boolean remove(@Nonnull final String key, @Nonnull final String value) {
Objects.requireNonNull(key);
Objects.requireNonNull(value);
final String normalizedKey = normalizeKey(key);
if (this.containsKey(normalizedKey)) {
final Set<String> values = this.get(normalizedKey);
if (values.contains(value)) {
values.remove(value);
if (values.isEmpty()) {
this.remove(normalizedKey);
}
return true;
}
}
return false;
}
}
Loading

0 comments on commit 4a437d1

Please sign in to comment.