简体   繁体   中英

How can I pass a HashMap as parameters

I have a class API, where you set and get the info you need to later be used on a API call I want to make it easier to the next person who's gonna use this.

So instead of doing this:

api.addURL("urltorequesttoken");
api.addHeader("client_id","sdfsfsdfsd")
   .addHeader("client_secret","sdfsdfsfsfd")
   .addHeader("grant_type","client_credentials")
   .addHeader("scope","READ");
api.addBody("bodyToSend")

I want to do this:

String URL = "";
URL = "put your URL here";

So I pass the URL and other variables as a parameter to another method where I will be doing what I did in the first block of code,so they don't need to know about the API class and its methods, but I dont know how to handle the hashmap, how can I do that user friendly? and then pass that as a parameter, also, what type of parameters should the methods receiving this info have? (Map<String key, String value>) or (String key, String value)?

EDIT(ADD):

So there's a class that a DEV is going to create, let's call it CreateToken, so that class currently has:

api.addURL("urltorequesttoken");
    api.addHeader("client_id","sdfsfsdfsd")
       .addHeader("client_secret","sdfsdfsfsfd")
       .addHeader("grant_type","client_credentials")
       .addHeader("scope","READ");
    api.addBody("bodyToSend")

There's another class called BASE, where Im doing the core services, in order for this to be easier for the person when they create their class, I dont want to have that block of code on their class, but instead, on mine, so in their class all they have to do is set the URL, headers and body(for POST method), so instead of this:

api.addURL("urltorequesttoken");

they will do:

URL = "urltorequesttoken";

and there's a method on their class to send me this or for me to get it i,e.

fillAPICallInfo(URL, headers, body);

I will receive that on the BASE class, but I dont know how to handle the Map variables, don't know how to make it easy for the DEV so they just put the key and value, and how do I receive that on my class (as a Map or as Strings)?

So you simply can pass a Map<String, String> as parameter:

public void fillAPICallInfo(String url, Map<String, String> headers, String body) {
  // Assuming there is an instance of class DEV named api available
  api.addURL(url);
  headers.forEach((h, v) -> api.addHeader(h, v));
  api.addBody(body);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM