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

Adds reponseheaders to API excpetion #299

Merged
merged 7 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,8 @@
package com.microsoft.kiota;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;

/** Parent type for exceptions thrown by the client when receiving failed responses to its requests. */
Expand All @@ -23,4 +26,8 @@ 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
public final ResponseHeaders responseHeaders = new 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,53 @@
package com.microsoft.kiota;

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

import javax.annotation.Nonnull;

class Headers extends CaseInsensitiveMap {
/**
* 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