-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
set args and data in builder, created UrlHelper, some refactor
- Loading branch information
Showing
6 changed files
with
101 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
module mars.UrlHelper; | ||
|
||
import std.algorithm.searching; | ||
import std.exception; | ||
|
||
version(unittest) { | ||
import std.stdio; | ||
} | ||
|
||
import mars.RequestParams; | ||
|
||
class UrlHelper { | ||
|
||
static string createUrl(string baseUrl, string requestUrl, RequestParams params) { | ||
enforce(requestUrl !is null, new Exception("Cannot get request Url!")); | ||
|
||
if (requestUrl.startsWith("http")) { | ||
return addArgsIfNeed(requestUrl, params); | ||
} | ||
|
||
enforce(baseUrl !is null, new Exception("Cannot get base Url!")); | ||
|
||
return baseUrl ~ addArgsIfNeed(requestUrl, params); | ||
} | ||
|
||
private static string addArgsIfNeed(string url, RequestParams params) { | ||
if (params is null || params.getParams.length == 0) { | ||
return url; | ||
} | ||
|
||
import std.string; | ||
import std.algorithm.iteration; | ||
|
||
string[string] args = params.getParams; | ||
|
||
string urlParams = args.keys.map!(k => k ~ "=" ~ args[k]).join("&"); | ||
if (canFind(url, "?")) { | ||
url = url ~ "&" ~ urlParams; | ||
} else { | ||
url = url ~ "?" ~ urlParams; | ||
} | ||
|
||
return url; | ||
} | ||
} | ||
|
||
unittest { | ||
try { | ||
UrlHelper.createUrl(null, null, null); | ||
assert(false); | ||
} catch (Exception e) { | ||
writeln(e.msg); | ||
assert(e !is null); | ||
assert(e.msg == "Cannot get request Url!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module mars; | ||
|
||
package import mars.AsyncHttpClient; | ||
package import mars.HttpClient; | ||
package import mars.HttpClientOptions; | ||
package import mars.HttpResponseHandler; | ||
package import mars.Request; | ||
package import mars.RequestParams; | ||
package import mars.ServerException; |