Skip to content

Added map with status texts #214

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

Merged
merged 3 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions core/src/main/scala/sttp/model/StatusCode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sttp.model

import internal.Validate._

// TODO: When moving to v2 add status text as field
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well this models a status code, why would it include a status text? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As if I think correctly status text is to a degree part of status code as it should be same as name of status code https://developer.mozilla.org/en-US/docs/Web/API/Response/statusText

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me it looks as if it's a different property on a Response class, which can be usually calculated from the status code, but definitely not part of it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, fixes incoming

class StatusCode(val code: Int) extends AnyVal {
def isInformational: Boolean = code / 100 == 1
def isSuccess: Boolean = code / 100 == 2
Expand Down
72 changes: 72 additions & 0 deletions core/src/main/scala/sttp/model/StatusTexts.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package sttp.model

import sttp.model.StatusCode._

object StatusTexts {

private val statusTexts: Map[StatusCode, String] = Map(
Continue -> "Continue",
SwitchingProtocols -> "Switching Protocols",
Processing -> "Processing",
EarlyHints -> "Early Hints",
Ok -> "Ok",
Created -> "Created",
Accepted -> "Accepted",
NonAuthoritativeInformation -> "Non Authoritative Information",
NoContent -> "No Content",
ResetContent -> "Reset Content",
PartialContent -> "Partial Content",
MultiStatus -> "Multi Status",
AlreadyReported -> "Already Reported",
ImUsed -> "Im Used",
MultipleChoices -> "Multiple Choices",
MovedPermanently -> "Moved Permanently",
Found -> "Found",
SeeOther -> "See Other",
NotModified -> "Not Modified",
UseProxy -> "Use Proxy",
TemporaryRedirect -> "Temporary Redirect",
PermanentRedirect -> "Permanent Redirect",
BadRequest -> "Bad Request",
Unauthorized -> "Unauthorized",
PaymentRequired -> "Payment Required",
Forbidden -> "Forbidden",
NotFound -> "Not Found",
MethodNotAllowed -> "Method Not Allowed",
NotAcceptable -> "Not Acceptable",
ProxyAuthenticationRequired -> "Proxy Authentication Required",
RequestTimeout -> "Request Timeout",
Conflict -> "Conflict",
Gone -> "Gone",
LengthRequired -> "Length Required",
PreconditionFailed -> "Precondition Failed",
PayloadTooLarge -> "Payload Too Large",
UriTooLong -> "Uri Too Long",
UnsupportedMediaType -> "Unsupported MediaType",
RangeNotSatisfiable -> "Range Not Satisfiable",
ExpectationFailed -> "Expectation Failed",
MisdirectedRequest -> "Misdirected Request",
UnprocessableEntity -> "Unprocessable Entity",
Locked -> "Locked",
FailedDependency -> "Failed Dependency",
UpgradeRequired -> "Upgrade Required",
PreconditionRequired -> "Precondition Required",
TooManyRequests -> "Too Many Requests",
RequestHeaderFieldsTooLarge -> "RequestHeader Fields Too Large",
UnavailableForLegalReasons -> "Unavailable For Legal Reasons",
InternalServerError -> "Internal Server Error",
NotImplemented -> "Not Implemented",
BadGateway -> "Bad Gateway",
ServiceUnavailable -> "Service Unavailable",
GatewayTimeout -> "Gateway Timeout",
HttpVersionNotSupported -> "Http Version Not Supported",
VariantAlsoNegotiates -> "Variant Also Negotiates",
InsufficientStorage -> "Insufficient Storage",
LoopDetected -> "Loop Detected",
NotExtended -> "Not Extended",
NetworkAuthenticationRequired -> "Network Authentication Required"
)

def get(statusCode: StatusCode): String =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to return an Option ale let the user decide what to do if it's a non-standard status code.

Are these texts "default" descriptions, that is, might they differ in actual server responses? Why would need this map in the first place? :)

Copy link
Contributor Author

@Pask423 Pask423 Aug 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 OK
2 Yes I intend to use it as default.
In sttp core in Response class we allow to set status text as in the screen.
I would like to write method, in sttp core, that will use one of these texts if sttp user does not provide one

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so maybe let's call this method StatusTexts.default(StatusCode)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

statusTexts.getOrElse(statusCode, "")
}